• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Variables on multiple instances are not counted

M

Matteo

Guest
Hello everyone!

This is my first post I hope I'm following the guidelines correctly.

Thanks to Tom Francis "Make a game with no experience" playlist on Youtube I started studying Gamemaker and I've come to an impasse.

I want to place multiple instances of an object in a room and have that object count a variable when a collision happens but the variable changes on collision only for the first instance ("original "instance) present in the room (I made a Draw DnD event for the variable so I can see it goes up only when the "original" instance collides)

The object oChalice has two Events with the variable OfferingS:

Create:

Code:
OfferingS = 0
Collision with oBlood:

Code:
if OfferingS = 0 {

        OfferingS = 1

    sprite_index = sChaliceFull

}
The sprite change works as intended for every instance and the variable stays at 0 also if the collision happens to an instance that's not the "original" one before any collision happened to the "original" one. (Meaning it's not that the variable reaches 1 and then stays there, it just doesn't change for any instance other than the original one)

I hope this is clear but please let me know if it isn't.

Thank you!
 

Nidoking

Member
You aren't checking to see whether the instance is the original instance. If I'm reading you correctly (and I'm probably not), you only want one specific instance to perform those steps. You'll need to set something (possibly global) with the id of that instance and then check self against that variable, or set something in the object to be false and set it to be true only for the instance that should react.

Or just make another object with the same sprite that doesn't have those events.
 
M

Matteo

Guest
You aren't checking to see whether the instance is the original instance. If I'm reading you correctly (and I'm probably not), you only want one specific instance to perform those steps. You'll need to set something (possibly global) with the id of that instance and then check self against that variable, or set something in the object to be false and set it to be true only for the instance that should react.

Or just make another object with the same sprite that doesn't have those events.
Hi Nidoking,

thank you for replying.

I want all the instances to perform all the steps on collision. At the moment only the original instance reacts correctly: on collision the sprite changes and the variable goes from 0 to 1. For all the instances that aren't the original one on collision they change sprite but the variable remains at 0.

The code is the same for all the instances so why are they reacting differently to the same collision event?
 

Nidoking

Member
How are you creating the instances? Did you create them in the Room Editor, or are you creating them dynamically?
Are you sure there's nothing else in your game that might be affecting this value or these objects?
Is there anywhere, at all, in your entire game, where you use the name of the object when you should be using an id?
What you have here looks correct, which means that there's probably something else you haven't posted that's the problem. I'm just taking guesses until you get around to posting that part.
 
M

Matteo

Guest
How are you creating the instances? Did you create them in the Room Editor, or are you creating them dynamically?
Are you sure there's nothing else in your game that might be affecting this value or these objects?
Is there anywhere, at all, in your entire game, where you use the name of the object when you should be using an id?
What you have here looks correct, which means that there's probably something else you haven't posted that's the problem. I'm just taking guesses until you get around to posting that part.
I'm creating the instances in the room editor by drag and drop.

In the enemy(oSlammer) code I have this Destroy event:

Code:
if oChalice.OfferingS < instance_number(oChalice)  {
  
    instance_create_depth(960,540,0,oSlammer)
  
}
The idea is that the enemy keeps respawning until all the Chalices are full which works if I have one Chalice in the room.

There is also a collision event for oChalice when hit by oSlammer:

Code:
if sprite_index = sChaliceFull {
  
    sprite_index =sChaliceEmpty
    OfferingS = 0
  
    with oSlammer {
      
        image_xscale = image_xscale + DamagE_FroM_OHarpoon
        image_yscale = image_xscale
        speed = speed * 0.8
    }

}
For testing purposes the enemy isn't moving so this collision isn't taking place.

There is no other code that mentions the variable OfferingS or the object oChalice.

Thank you so much for helping out!
 

Nidoking

Member
if oChalice.OfferingS < instance_number(oChalice)
This won't work the way you seem to expect. This will choose one oChalice (presumably the first one created) and compare its OfferingS value (0 or 1) against the number of oChalices in the room. I'm going to assume that you haven't done something like use the debugger to inspect the value of the OfferingS variables and have been assuming that they weren't incrementing because this check always failed, and that you stated your problem incorrectly. It happens.

What you probably want to do is check to see if there's at least one oChalice that doesn't have OfferingS equal to one, and if so, spawn a new enemy. Consider this:
GML:
with (oChalice)
{
  if (OfferingS == 0)
  {
    instance_create_depth(960,540,0,oSlammer);
    break; // so you don't create more than one
  }
}
The other way to do it would be to sum the values of all of the oChalice OfferingS values, but the with is cleaner. You could also use a global variable instead of the instance variable, and have each oChalice increment or decrement that global value. In any case, if you want the sum of values, you have to actually sum the values.
 
M

Matteo

Guest
The other way to do it would be to sum the values of all of the oChalice OfferingS values, but the with is cleaner. You could also use a global variable instead of the instance variable, and have each oChalice increment or decrement that global value. In any case, if you want the sum of values, you have to actually sum the values.
Thank you! It works perfectly!
 
Top