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

Define specific object when multiple instances of same object exist

J

jaakko meyn

Guest
I am working on an enemy that I want to shoot a laser beam from itself to the player, which im doing using the draw event. I have the laser with the draw event as a different object that is created in the enemy object. The laser object draws a line from the enemy.x/y to the player x/y, but I need to define which enemy object it draws it from, so how do I do that?

(I dont see any reason to include the code for this sinze its scattered around a few events and objects and its only a couple of lines, but if someone wants it I can show it)

This image may help
d9876aa2b28cbae7bfc99f85ab698cbc.png

(image) So lets say the eye on the right has reached the point where it is supposed to create the laser object, and the line is drawn from that eye. But now the second eye also reaches the specific point and shoots the laser, but the laser is still drawn from the first eye, which I obviously dont want
 

Tornado

Member
Maybe you should post the code which draws the laser.
I assume when creating laser you are using obj_Eye.x and obj_Eye.y. That would be wrong as you noticed.

Just use instance variables x and y instead for the starting point of laser, then it will be drawn exactly from that eye instance.
 
Last edited:

DukeSoft

Member
Objects are something else than instances.

An instance is a spawned object. The weird thing in Game Maker is that if you use an objects name (obj_enemy.x) it will use the first spawned instance of that object. In my opinion it should throw an error, but well.

Anyway, when creating an object, you get its instance handle returned. So if you need to make an object that is "bound" to another one (e.g. the eye of the enemy) you can solve it like this;

Enemy create:
Code:
eyeInstance = instance_create_depth(x, y, depth, obj_enemy_eye);
eyeInstance.parent = id; //Note that parent isn't the regular child/parent setup - but rahter a variable that holds the ID (Instance ID) of the enemy that spawned the eyes
Then in the eye code you could do;

Step:
Code:
x = parent.x;
y = parent.y; //Parent now is a reference to the enemy that this eye belongs to
Hope this helps :)
 
J

jaakko meyn

Guest
Objects are something else than instances.

An instance is a spawned object. The weird thing in Game Maker is that if you use an objects name (obj_enemy.x) it will use the first spawned instance of that object. In my opinion it should throw an error, but well.

Anyway, when creating an object, you get its instance handle returned. So if you need to make an object that is "bound" to another one (e.g. the eye of the enemy) you can solve it like this;

Enemy create:
Code:
eyeInstance = instance_create_depth(x, y, depth, obj_enemy_eye);
eyeInstance.parent = id; //Note that parent isn't the regular child/parent setup - but rahter a variable that holds the ID (Instance ID) of the enemy that spawned the eyes
Then in the eye code you could do;

Step:
Code:
x = parent.x;
y = parent.y; //Parent now is a reference to the enemy that this eye belongs to
Hope this helps :)
Oh ok thank you :D I think you misunderstood me a little since the eye is the enemy and the laser is the object it creates, but im pretty sure I can figure this out with this :)
 

Tornado

Member
put this into objEye where laser is created
instance_create(x, y, objLaser);
then in the draw event of objLaser you draw from x, y to objPlayer. x, objPlayer. y
 
J

jaakko meyn

Guest
put this into objEye where laser is created
instance_create(x, y, objLaser);
then in the draw event of objLaser you draw from x, y to objPlayer. x, objPlayer. y
but when the player moves the eyes move too and I want the laser beam to be continious so it should also move with the eyes, which I cant do by using the same movement code I use for the eyes because the y and x where the eyes basically lock on to the player and shoot the laser are randomly generated (in a small range) for each eye :/
 
J

jaakko meyn

Guest
Objects are something else than instances.

An instance is a spawned object. The weird thing in Game Maker is that if you use an objects name (obj_enemy.x) it will use the first spawned instance of that object. In my opinion it should throw an error, but well.

Anyway, when creating an object, you get its instance handle returned. So if you need to make an object that is "bound" to another one (e.g. the eye of the enemy) you can solve it like this;

Enemy create:
Code:
eyeInstance = instance_create_depth(x, y, depth, obj_enemy_eye);
eyeInstance.parent = id; //Note that parent isn't the regular child/parent setup - but rahter a variable that holds the ID (Instance ID) of the enemy that spawned the eyes
Then in the eye code you could do;

Step:
Code:
x = parent.x;
y = parent.y; //Parent now is a reference to the enemy that this eye belongs to
Hope this helps :)
Also if I create the variable "eyeinstance" in the create event but want to call the function asigned to it (in an alarm event) through the variable how can I do that?
 

Tornado

Member
do you mean the laser going out of the eye is not going from the center of the eye, but randomly around the center of the eye?

if so, then is not a problem either, you still can use eyes x, y as a base for creating a laser but add some random offset to it like

instance_create(x+random_range(-30,30), y+random_range(-30,30), objLaser);

don't forget to call randomize() at the start of the game, otherwise the random numbers will be the same on every start of the game.
 
J

jaakko meyn

Guest
do you mean the laser going out of the eye is not going from the center of the eye, but randomly around the center of the eye?

if so, then is not a problem either, you still can use eyes x, y as a base for creating a laser but add some random offset to it like

instance_create(x+random_range(-30,30), y+random_range(-30,30), objLaser);

don't forget to call randomize() at the start of the game, otherwise the random numbers will be the same on every start of the game.
What I meant was the eyes move towards the player and at a randomly generated point above the player they stop and shoot... But I figured it out already :) still thanks :D
 
Top