Changing Variables in multiple instances of the same Object

H

Huy

Guest
Hi,

In my game, whenever the day changes, every instance( human) 'hp' variable goes down by 1. Once hp hits 0 then the instance is destroyed.

These instances are created from the same object at different times. BUT when the game runs and the day changes, only one instance's hp, the oldest one, goes down. After its hp hits 0 and gets destroyed, then the next oldest instance starts counting down, and then repeat.

When i duplicated the object and created 2 instances of 2 different but identical object, it works fine.
how do i make it so all the instances of the same object run together instead of one at a time?

I'm mostly using drag and drop feature.
 
D

Dark

Guest
To be more specific about what the above post is saying, you can execute code across all instances of an object remotely in a code action like this
Code:
with (objExample)
{
    //code here
}
For example:
Code:
with (human)
{
    hp -= 1;
}
 
  • Like
Reactions: Huy

M. Idrees

Member
Make A Global Variable In the Create Event of day & night controller object
global.current_time = "day";


Whenever The Day Changes Make The Variable In The Step Event like

Code:
if ( day_time_passed && global.current_time == "day" ) {  // Check If The Day Is Passed
     obj_human.hp -= 1;                                                       // Decrease The Object Human Health By 1
     global.current_time = "night";                                        // Set Current Time To Night
} else 
if ( night_time_passed && global.current_time == "night" ) { // Check If The Night Is Passed
     global.current_time = "day";                                             //  Set Current Time To Day
}
 
  • Like
Reactions: Huy
H

Huy

Guest
To be more specific about what the above post is saying, you can execute code across all instances of an object remotely in a code action like this
Code:
with (objExample)
{
    //code here
}
For example:
Code:
with (human)
{
    hp -= 1;
}
Works like a charm, thanks.
 
P

ParodyKnaveBob

Guest
Fwiw, Huy, you were probably doing something like telling human.hp to subtract 1 using the Set Variable action, right? I don't know if GMS2 still does this, but GM and GMS1 let you tell the action it Applies To something: the calling instance, the "other" instance (in Collision Events in this context), or "all instances of object ____." In your case, you could use the Set Variable action, subtract 1 from hp, and tell it that it Applies To all instances of the human object. (This is the same of course as using the with() construct in formal GML like CMAllen and Dark showed.)

I still use Applies To, even in the Execute Code action, if I know the whole thing will just be wrapped in a simple with() construct anyway. $:^ }
 
H

Huy

Guest
Fwiw, Huy, you were probably doing something like telling human.hp to subtract 1 using the Set Variable action, right? I don't know if GMS2 still does this, but GM and GMS1 let you tell the action it Applies To something: the calling instance, the "other" instance (in Collision Events in this context), or "all instances of object ____." In your case, you could use the Set Variable action, subtract 1 from hp, and tell it that it Applies To all instances of the human object. (This is the same of course as using the with() construct in formal GML like CMAllen and Dark showed.)

I still use Applies To, even in the Execute Code action, if I know the whole thing will just be wrapped in a simple with() construct anyway. $:^ }
Thanks for the info!
 
M

Muddykat

Guest
Make A Global Variable In the Create Event of day & night controller object
global.current_time = "day";


Whenever The Day Changes Make The Variable In The Step Event like

Code:
if ( day_time_passed && global.current_time == "day" ) {  // Check If The Day Is Passed
     obj_human.hp -= 1;                                                       // Decrease The Object Human Health By 1
     global.current_time = "night";                                        // Set Current Time To Night
} else
if ( night_time_passed && global.current_time == "night" ) { // Check If The Night Is Passed
     global.current_time = "day";                                             //  Set Current Time To Day
}
 
Top