Reputation: 11
I am currently in the process of making a game in gamemaker. The player attacks work by getting the players stamina and storing that in a damage variable, then flying out of the player and hitting the enemy, taking twice the amount of health off of the enemy as the damage variable had.
if the player had a stamina of 45 when performing the attack, the attack sprite will fly out with a damage of 45. When hitting an enemy this will deal 90 Damage to the enemy, leaving them with just 10 health.
The issue is the game doesn't appear to know which attack sprite has hit the enemy since you can perform an essentially unlimited amount of attacks, and therefore doesn't apply the correct amount of damage to the enemy.
How would I get an instance id of the object that collided with the enemy so I can use that to access the damage variable?
Thanks in advance
Upvotes: 1
Views: 5922
Reputation: 4987
Here's a code example of what Jeremy ment:
Event that triggers the attack (e.g. global mouse left in player object):
var attackInstance = instance_create(x, y, obj_attack); //Create an instance
attackInstance.damage = 45; //Set the damage of this _instance_ to 45
attackInstance.speed = 4; //Make it move
attackInstance.direction = point_direction(x, y, mouse_x, mouse_y); //in the right direction
In the collision event with obj_attack in obj_enemy:
hp -= other.attack*2; //My HP gets down by the amount of the attack variable in the collided instance.
with(other) {
instance_destroy(); //Destroy the attack object
}
That should basically do the trick :)
Upvotes: 1
Reputation: 2257
I'm assuming you're using a collision event with the enemy, correct? Inside of that, you should just be able to use other
. So, something like this:
var instance_id = other.id
Here's some more info from YoYoGames for other:
It's the second one on the list :)
Upvotes: 1