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

Enemy Problems [SOLVED]

A

Amoses

Guest
So, I am trying to make it so everytime my player left clicks the enemy, it does damage, and thats working. except for the fact that everytime i click, it does damage to every enemy spawned, and im not sure how to fix that. if somebody could help please let me know!
 
A

Amoses

Guest
The problem is that while you are checking for one collision between the mouse position and the Enemy object you are applying the hp reduction code to all enemy objects within your "with" statement.
Read the following tutorial, it will help you get it right:
https://forum.yoyogames.com/index.php?threads/whats-the-difference-objects-and-instances.29005/
If you have any problem let us know.
this still makes no sense. i would just like a line of code(s) that would help my issue.
 
T

TinyGamesLab

Guest
If you read the tutorial you'll find exactly what you need while learning more about objects x instances, which is why you could not get the code to work in the first place.
Read this part:
Collision-checking functions:instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ all return instance IDs. Save that instance ID into a variable (e.g. var inst_enemy = instance_place(x, y, obj_enemy);), then use that as the subject to work with (e.g. inst_enemy.hp -= 10;). Note that these functions return noone upon not finding a collision. Always account for this special case whenever you handle collisions this way.
 
A

Amoses

Guest
If you read the tutorial you'll find exactly what you need while learning more about objects x instances, which is why you could not get the code to work in the first place.
Read this part:
that was the exact part i did read. but i still do not know how and where to use these things.
 
A

Amoses

Guest
If you read the tutorial you'll find exactly what you need while learning more about objects x instances, which is why you could not get the code to work in the first place.
Read this part:
i now have
if instance_exists(oEnemy)
var inst_enemy = instance_place(oEnemy.x,oEnemy.y, oEnemy)
if mouse_check_button_pressed(mb_left) && position_meeting(mouse_x, mouse_y, oEnemy)
inst_enemy.hp -= 10
and its making it so not several enemys get attacked at once, but only one is able to be attacked and even if i click on a different one, the first one gets attacked
 
T

TinyGamesLab

Guest
Here you go:
Code:
var inst;
inst = instance_place(mouse_x, mouse_y, oEnemy));
if (mouse_check_button_pressed(mb_left) && inst != noone)
   {   
   inst.hp -= 20;
   }
 

woods

Member
something like this....?

in your original post...
with (oEnemy)
// this makes all instances of oEnemy tahe damage
should be
with(other)
// this makes the one you are clicking on take damage
 

Bentley

Member
@Amoses
You were close here:
its just, ```if mouse_check_button_pressed(mb_left) && position_meeting(mouse_x,mouse_y,oEnemy)
with (oEnemy)
hp -= 20;```
Change it to...
Code:
if (mouse_check_button_pressed(mb_left))
{
    var inst = instance_position(mouse_x, mouse_y, o_enemy);
    if (instance_exists(inst))
    {
        inst.hp -= something;
    }
}
 
Last edited:
Top