Computer Science Canada Variable has no value |
Author: | CyberStorm [ 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
|
Author: | Tony [ 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. |
Author: | DemonWasp [ 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. |
Author: | CyberStorm [ 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? |
Author: | CyberStorm [ 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? |
Author: | Raknarg [ 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. |
Author: | DemonWasp [ 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. |
Author: | Raknarg [ Sat Jun 11, 2011 2:21 pm ] |
Post subject: | RE:Variable has no value |
Whoops, i misread the code -.-' nevermind then |
Author: | CyberStorm [ 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? |
Author: | Tony [ 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_) |
Author: | CyberStorm [ 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"? |
Author: | 2F_Zelda [ Sun Jun 12, 2011 10:53 am ] |
Post subject: | Re: Variable has no value |
That would throw an "out of range" error. |
Author: | DemonWasp [ 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:
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. |