Game Maker not recognizing variable

5.9k views Asked by At

Attempting to set up a targeting array for a MMO-style game in GameMaker8 Pro, I have this code in the create event for the player's character, which is and has been running perfectly fine:

j = 0
i = 0
g = 0
for (i=100000; i<1000000; i+=1) if instance_exists(i) {if i.object_index = enemy         {global.ttarget[j] = i j+=1}  if i.object_index = rk or i.object_index = sage    {global.etarget[g] = i g += 1}}
global.rmtargets = j
global.etargets = g

Then running this code in the step event for the player character:

h = 0
g = 0
i = 0
for (i=0; i<global.rmtargets; i+=1) global.target[i] = 0
global.target[0]=101139
for (h = 0; h<global.rmtargets; h+=1){hv = -1
for (g = 0; g<global.rmtargets; g+=1){if global.ttarget[g].range > hv {hv =    global.ttarget[g].range}}
global.target[h] = hv
global.ttarget[h] = -1}

Returns this error message:

ERROR in
action number 1
of  Step Event
for object rk:

Error in code at line 8: for (g = 0; g<global.rmtargets; g+=1){if global.ttarget[g].range > hv {hv = global.ttarget[g].range}}
at position 61: Unknown variable range

Even though I have this in the create event for the enemy:

range = 0
range = distance_to_object(rk)

And I've used this sort of syntax all over:

global.target[target].threat[s] += damage

Help? Any ideas why Game Maker won't recognize the variable?

4

There are 4 answers

0
Brennan Hatton On

try putting brackets around the object variable. I have had trouble references from a reference extension before.

(global.ttarget[g]).range

or even save it to a new variable

for (g = 0; g<global.rmtargets; g+=1)
{
    curr_target = global.ttarget[g]
    curr_target.range
}
0
AudioBubble On

First of all, put round brackets in if conditions.

Second you should give more informations about your environment and programming logic and IMO stop using all these global variables.

Anyway, from what i understood of what you're doing, try to use the with keyword:

with(global.ttarget[g]) {

other.hv = range;

}
0
Timtech On

Instead of using global. before each instance of the variable in the code, you could also initialize it with the command:

globalvar (variable), (variable2);

Then you would be able to use the variable without global. in front of it :)

If object rk is not the enemy then there is no global range variable detectable by object rk. Variables initialized without var or globalvar only apply for the object it was defined in.

0
Medo42 On

My best guess is that one or more of the enemy instances have been destroyed between the player create event and the step event where the error happens. Maybe a better solution would be to iterate over all the enemies using the with() construct, that is faster and you can be sure that all the instances you are working with actually exist.