Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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 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?

share|improve this question
2  
for (i=100000; i<1000000; i+=1) if (instance_exists(i)) if (i.object_index == enemy) // ... is a bad way to iterate over all the instances of a type- just use with (enemy) // ... –  Rusky Aug 11 '11 at 21:56

2 Answers 2

The enemy at that point in the array has probably been destroyed. Instead of looping through an array full of potentially-gone instance ids, you should use with:

var hv;
hv = -1
with (enemy) {
    if (range > hv)
        hv = range
}

Replace the inner for loop of your step event with that and then you can create and destroy enemies all you like without worrying about updating the now-unnecessary array.

share|improve this answer

I think the problem is that variable can not have "under variable", in GM is this possible even with the objects.

share|improve this answer
    
But looking at the last section of code I posted, I don't see why that can run but why it won't recognize the variable I'm having trouble with... Or is the threat accumulating syntax I posted not what you're calling an "under variable?" –  user6629 Apr 10 '11 at 16:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.