Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Variable has no value
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CyberStorm




PostPosted: Fri Jun 10, 2011 1:32 pm   Post subject: Variable has no value

What is the problem you are having?
sometimes, after killing an enemy, a "Variable has no value" error appears on the targethp (r - 1) := targethp (r)
line. This is all of the code that uses the flexible array variables (this is not all of the code in the program and the for loops do not come right after each other).

Post any relevant code

Turing:


var x1, y1, xtar, ytar, targethp, enshootcount, miss : flexible array 1 .. 0 of int

for i : 1 .. numtarg
                new x1, upper (x1) + 1
                new xtar, upper (xtar) + 1
                new y1, upper (y1) + 1
                new ytar, upper (ytar) + 1
                new targethp, upper (targethp) + 1
                new miss, upper (miss) + 1
                new enshootcount, upper (enshootcount) + 1
                randint (x1 (i), 10, maxx - 50)
                randint (y1 (i), 10, maxy - 100)
                xtar (i) := maxx + 30
                ytar (i) := y1 (i)
                targethp (i) := 100
                enshootcount (i) := 0
            end for

                        for i : 1 .. numtarg
                            if xshot >= x1 (i) and xshot <= x1 (i) + 30 and yshot >= y1 (i) and yshot <= y1 (i) + 30 then
                                targethp (i) := targethp (i) - dmg
                                if targethp (i) <= 0 then
                                    for r : i + 1 .. upper (targethp)
                                        targethp (r - 1) := targethp (r)
                                        x1 (r - 1) := x1 (r)
                                        xtar (r - 1) := xtar (r)
                                        y1 (r - 1) := y1 (r)
                                        ytar (r - 1) := ytar (r)
                                        enshootcount (r - 1) := enshootcount (r)
                                        miss (r - 1) := miss (r)
                                    end for
                                    new targethp, upper (targethp) - 1
                                    new x1, upper (x1) - 1
                                    new xtar, upper (xtar) - 1
                                    new y1, upper (y1) - 1
                                    new ytar, upper (ytar) - 1
                                    new enshootcount, upper (enshootcount) - 1
                                    new miss, upper (miss) - 1
                                    numtarg := numtarg - 1
                                    shoot := false
                                    targets := targets + 1
                                    exit
                                end if
                            end if
                        end for
                        shoot := false
                    end if
                end if
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Fri Jun 10, 2011 1:41 pm   Post subject: RE:Variable has no value

There are cases when targethp (r) has no value. It seems like a pretty straight-forward error. The way to have no value is to create that variable (either new array, or change the size of a flexible array), but not write any value to it.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
DemonWasp




PostPosted: Fri Jun 10, 2011 3:43 pm   Post subject: RE:Variable has no value

The bit of code that expands those arrays (the one with new all over the place)...how many times does it get run?

I ask because if it runs more than once, the flexible arrays will be twice as long as you expect, and the upper halves of each will not have any values assigned.
CyberStorm




PostPosted: Sat Jun 11, 2011 5:12 am   Post subject: Re: RE:Variable has no value

DemonWasp @ Fri Jun 10, 2011 3:43 pm wrote:
The bit of code that expands those arrays (the one with new all over the place)...how many times does it get run?

I ask because if it runs more than once, the flexible arrays will be twice as long as you expect, and the upper halves of each will not have any values assigned.


well, they get run as many times as there are enemies, so each enemy has their own slot in every one of those arrays, so quite a few times. are you saying that by adding + 1 to the flexible array creates 2 slots?
CyberStorm




PostPosted: Sat Jun 11, 2011 7:45 am   Post subject: Re: RE:Variable has no value

Tony @ Fri Jun 10, 2011 1:41 pm wrote:
There are cases when targethp (r) has no value. It seems like a pretty straight-forward error. The way to have no value is to create that variable (either new array, or change the size of a flexible array), but not write any value to it.


but it does get assigned a value: new targethp, upper (targethp) + 1; targethp (i) := 100. or would this not work?
Raknarg




PostPosted: Sat Jun 11, 2011 10:47 am   Post subject: RE:Variable has no value

No, because it's only setting the variable it's on at the moment. You need to use instead:

new targethp, upper (targethp) + 1
targethp (upper (targethp)) := 100

I think that's what you're looking for.
DemonWasp




PostPosted: Sat Jun 11, 2011 2:16 pm   Post subject: RE:Variable has no value

No. What I was saying was that if the array is "initialized" twice (for example, you play once, then play again), each array will have 2 * numtarg elements, the upper half of which will have no values defined.
Raknarg




PostPosted: Sat Jun 11, 2011 2:21 pm   Post subject: RE:Variable has no value

Whoops, i misread the code -.-' nevermind then
Sponsor
Sponsor
Sponsor
sponsor
CyberStorm




PostPosted: Sat Jun 11, 2011 8:28 pm   Post subject: Re: RE:Variable has no value

DemonWasp @ Sat Jun 11, 2011 2:16 pm wrote:
No. What I was saying was that if the array is "initialized" twice (for example, you play once, then play again), each array will have 2 * numtarg elements, the upper half of which will have no values defined.


the (new + 1) code is executed at the beginning of a stage, while you kill enemies the array decreases (new - 1), so technically at the end of a stage there should be no values in the array. next stage starts, the array gets refilled, then gets depleted by the end.
my speculations were that its not the logic, its something i may have missed in the flexible array system, since i only recently learned it. is it correct that i should have a 1..0 flexible array for this situation?
Tony




PostPosted: Sat Jun 11, 2011 9:09 pm   Post subject: RE:Variable has no value

Take the resizing out of the loop. Instead of starting at 0 and incrementing the size by 1 over and over again, resize it to the final size once. Two benefits:
- you'll be more confident in the size of the array
- it's faster (array reallocation is _expensive_)
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
CyberStorm




PostPosted: Sun Jun 12, 2011 7:20 am   Post subject: Re: Variable has no value

my first impression about this error was that if i kill an enemy that has its values at the 'upper' of the array, there will be nothing to shift down. but according to the code, it still has to go + 1 over the array boundary to look for things to shift down, but it finds nothing because there are no values outside of the array boundaries.
could this be the problem or if it was the case then it would give the "out of range" error instead of "variable has no value"?
2F_Zelda




PostPosted: Sun Jun 12, 2011 10:53 am   Post subject: Re: Variable has no value

That would throw an "out of range" error.
DemonWasp




PostPosted: Sun Jun 12, 2011 11:49 am   Post subject: RE:Variable has no value

Since you haven't given us the entire file, I can't really debug this properly. My next suggestion would be to put some assert statements in to make sure everything is the right size, right after you shrink the arrays:

Turing:

assert numtargs = upper (targethp)
assert numtargs = upper (x1)
assert numtargs = upper (xtar)
...


Properly though, you shouldn't be resizing arrays, because that's both messy an very slow. Marking records as "inactive" is a much better strategy.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: