• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

Enemy Tank Problem

A

Ajay Warden

Guest
Totally new to Game Maker.

I'm making a tank game. The enemy tank is made up of two objects,

obj_enemytank and obj_enemytankturret

It seems so simple but I'm having trouble making the enemy tank turret move with the enemy tank, as in keeping the turret on top of the tank while moving.

There are multiple enemy tanks on screen so the simple,

Jump To Position
obj_enemytank.x
obj_enemytank.y

doesn't work as it makes all the enemy turrets jump to one enemy tank instead of the tank it should be "connected" to.

I have it so each obj_enemytank creates an instance of obj_enemytankturret on top of it when the game starts, but as soon as the tank moves, the turret it created does not.
 
A

Aura

Guest
Assign the turret instance the id of the tank instance that created it.

Code:
// Create event (obj_enemytank)
with (instance_create(x, y, obj_enemytankturret)) {
   my_tank = other.id;
}
Now, move the turret instance.

Code:
// End Step (obj_enemytankturret)
x = my_tank.x;
y = my_tank.y;
 
A

Ajay Warden

Guest
That works but what about when the tank is killed. I get a crash when an enemy tank is killed. How do I make the tank at its corresponding turret disappear when the variable enemyhealth is less than or equal to 0?

I have a mix of D&D and code and this is my current enemy killed event.

Step Event
If enemyhealth is less than or equal to 0
Start of a block
Play sound enemykilled
Destroy the instance
end of block

When the enemy is killed, this crash report happens

FATAL ERROR in
action number 1
of Step Event0
for object obj_enemyturret:

Unable to find any instance for object index '100003' name '<undefined>'
at gml_Object_obj_enemyturret_StepNormalEvent_1 (line 1) - x = my_tank.x;

How would I destroy both the obj_enemytank and the obj_enemyturret obviously I need to replace the Destroy the instance action with something.
 
Last edited by a moderator:
A

Ajay Warden

Guest
Oh, sorry. What about when the tank is killed. I get a crash when an enemy tank is killed. How do I make the tank at its corresponding turret disappear when the variable enemyhealth is less than or equal to 0?
 
A

Ajay Warden

Guest
Check if it exists using instance_exists(my_tank)
Sorry, really new to Game Maker. I have a mix of D&D and code and this is my current enemy killed event.

Step Event
If enemyhealth is less than or equal to 0
Start of a block
Play sound enemykilled
Destroy the instance
end of block

When the enemy is killed, this crash report happens

FATAL ERROR in
action number 1
of Step Event0
for object obj_enemyturret:

Unable to find any instance for object index '100003' name '<undefined>'
at gml_Object_obj_enemyturret_StepNormalEvent_1 (line 1) - x = my_tank.x;

How would I destroy both the obj_enemytank and the obj_enemyturret obviously I need to replace the Destroy the instance action with something.
 

DukeSoft

Member
The method I use for "sub"-instances is like so;

obj_tank
Code:
///create
turret = instance_create(x, y, obj_tank_turret); //turret is now a reference to our turret's instance

///step event of tank (after any movement code - if you do it the wrong way around, the turret will "hover" behind the tank's movement because its delayed 1 step)
turret.x = x; //Update child X with our X
turret.y = y; 

///destroy event
with (turret) { //When we get destroyed, destroy our "child" instance as well
    instance_destroy();
}
Now the turret has no knowledge about the tank. If you need the turret to know about our tank, you can do it like so in the tank create event;
obj_tank
Code:
///create
turret = instance_create(x, y, obj_tank_turret); //turret is now a reference to our turret's instance
turret.my_tank = id; // id is a reference to our current instance (so the instance of the tank). The turret object can now always use "my_tank" to get to his  "parent" instance.
 
A

Aura

Guest
Change the Step event code to this:

Code:
if (!instance_exists(my_tank)) {
   instance_destroy();
}
else {
   x = my_tank.x;
   y = my_tank.y;
}
 
A

Aura

Guest
We have events for a reason This is useless CPU overhead for every step..
Perhaps. But I was completing what I had suggested in the first place. The method I posted requires you to do that. It's not like your post doesn't have potential or something. ^^'
 

DukeSoft

Member
Perhaps. But I was completing what I had suggested in the first place. The method I posted requires you to do that. It's not like your post doesn't have potential or something. ^^'
Agreed! But you should always be very cautious with using resources. Its little things like calling "instance_exists" in a step event that will eventually drop your game's FPS, and they are a real pain to fix afterwards.
 
Top