• 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!

GameMaker [SOLVED ]Linking objects together

L

Lobos

Guest
I am trying to create something similar to a building in a classic RTS game (like Starcraft, Age of Empire, etc.).

When I place a "building" object, it create "button" objects (like "upgrade building" button, "create unit" button, etc.) for each building (in x,y-50, x,y+50, like a flower, not on the GUI layer).


My goal is: when the player clicks on one of the buttons, it affects the building (for example with a variable_instance_set() ), but only that one building and also, this has to work backwards too: when the building is destroyed, it must destroy the buttons assigned to it, but not any other building's same buttons.


Please suggest a short code to work this out (my first idea was with the "with", but that only help when creating the buttons).
 

FoufaDjo

Member
u need to target the object id by ur events ill show u in an example :
attaching object to object by ids:

object1 step event:
if stuff{
var inst = instance_create_depth(x,y,0,object2);
inst.ownerid = pid;
}
object2 step event:
x=ownerid.x;
y=ownerid.y;
 
L

Lobos

Guest
Thanks, but am I understanding it correctly? (It looks like it working, but just to be on the safe side):


Building's create event:
Code:
upgrade = false;
var inst = instance_create_depth(x-20,y-50,0,obj_upgrade);
inst.ownerid = id;

Building's step event:
Code:
if (upgrade = true)
{
   show_message("Upgrading");  
}

Upgrade's create event:
Code:
ownerid = 0;

Upgrade's Left pressed event:
Code:
ownerid.upgrade = true;

And if I want to do something (like destroy the first object throu the second, I just use the "ownerid"? For example with putting one more line to the Upgrade's Left pressed event:
Code:
instance_destroy(ownerid);
 

FoufaDjo

Member
yes thats how it works use the ownerid insted of your objupgrade (objupgrade will target all objupgrade) for targting that specific instant (ownerid only the objupgrade that have the ownerid of it)
but one thing idk if its will effect or not because it will change is on the upgrade creat event you dont need to set the ownerid because you allready put in when been created on the building step event
 
L

Lobos

Guest
yes thats how it works use the ownerid insted of your objupgrade (objupgrade will target all objupgrade) for targting that specific instant (ownerid only the objupgrade that have the ownerid of it)
but one thing idk if its will effect or not because it will change is on the upgrade creat event you dont need to set the ownerid because you allready put in when been created on the building step event
Ok. Thanks for helping.
 
Top