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

GML OK guys i'm new and i'm having issues this might be simple

E

Evilspike

Guest
I'm playing around trying to learn I've searched and read around, but i'm not saying i didn't miss something super simple that would fix this so first things first thank you for your time!!

So i have a player object and 2 enemy objects(same object just drug into room twice). What i want to happen is when the enemy runs into the player i want there to be a knock back mechanic. The knock back mechanic works however it applies to both enemy's in the room instead of just the one i hit. I will post the code i'm sure i'm just making a stupid mistake.

//every frame

//gravity
if(!place_meeting(x,y+2,oWall))
{
vspd = vspd + grav;
}
else
{
vspd = 0;
}
y = y + vspd;

//movement
if(!place_meeting(x + sign(hspd), y, oWall))
{
hspd = sign(hspd) * walkspd;
}
else
{
hspd = sign(hspd) * -1;
}
x = x + hspd;



//animation
if(hspd != 0)
{
image_speed = 1;
sprite_index = sEnemyR;

}

//collision with player
if(i💩💩💩💩)
{
if(oPlayer.hspd > 0)
{
oPlayer.y = oPlayer.y + (-knockbackforce);
oPlayer.x = oPlayer.x + (-knockbackforce * sign(oPlayer.hspd));
self.x = self.x + (knockbackforce * sign(oPlayer.hspd));
oPlayer.walkspd = 0;
alarm[1] = 15;
}
else if(oPlayer.hspd < 0)
{ if(hspd < 0)
{
oPlayer.y = oPlayer.y + (-knockbackforce);
oPlayer.x = oPlayer.x + (-knockbackforce * sign(hspd));
self.x = self.x +(knockbackforce * sign(hspd));
oPlayer.walkspd = 0;
alarm[1] = 15;
}
else
{
oPlayer.y = oPlayer.y + (-knockbackforce);
oPlayer.x = oPlayer.x + (knockbackforce * sign(hspd));
self.x = self.x + (-knockbackforce * sign(hspd));
oPlayer.walkspd = 0;
alarm[1] = 15;
}
}
else
{
oPlayer.y = oPlayer.y + (-knockbackforce);
oPlayer.x = oPlayer.x + (knockbackforce * sign(hspd));
self.x = self.x + (-knockbackforce * sign(hspd));
oPlayer.walkspd = 0;
alarm[1] = 15;
}
//clamp the player and enemy so they dont get stuck in walls
oPlayer.x = clamp(oPlayer.x, 31, 782);
oEnemy.x = clamp(oEnemy.x, 31, 768);


oPlayer.hp--;
i💩💩💩💩 = false;
}

as i said it works as intended just on all enemy's at the same time instead of the individual enemy hit.
 

Alexx

Member
From a quick look, the issue is here:
Code:
oEnemy.x = clamp(oEnemy.x, 31, 768);
Which will apply to all oEnemy

Store the id of the colliding enemy, for example. enemy_id and use something like
Code:
enemy_id.x = clamp(enemy_id.x, 31, 768);
This will then apply to enemy with that stored id value.

Edited, updated code.
 
Last edited:
E

Evilspike

Guest
From a quick look, the issue is here:
Code:
oEnemy.x = clamp(oEnemy.x, 31, 768);
Which will apply to all oEnemy

Store the id of the colliding enemy, for example. enemy_id and use something like
Code:
enemy_id.x = clamp(oEnemy.x, 31, 768);
This will then apply to enemy with that stored id value.
Thank you so much!!! your amazing lol, that was the problem i commented that section of the code out to test and it worked normally, but the problem of the sprites getting stuck in the wall is happening. I will need to wrap my head around how to store the ID of the colliding enemy. Thank you!!
 

DukeSoft

Member
When you use object identifiers (the names, oEnemy) - the game will internally pick the first created instance of that object.

When dealing with instance-specific events, you will need to use the instance identifier (id). I don't know whats in your if statement, but I suppose there's a collision function that will return either noone or the instance ID. Use that one.

Protip: Wrap your code on the forums in
Code:
 [code]
blocks. Makes it much more readable!
 

TheouAegis

Member
Just don't reference oEnemy at all. The code is in oEnemy, so there is absolutely no point in even referencing oEnemy in oEnemy's code. The variable grammar in that block of code is all over the place: You refer to just x and y in the first section, self.x and self.y in the second section, and oEnemy.x in the last section.
 
E

Evilspike

Guest
Just don't reference oEnemy at all. The code is in oEnemy, so there is absolutely no point in even referencing oEnemy in oEnemy's code. The variable grammar in that block of code is all over the place: You refer to just x and y in the first section, self.x and self.y in the second section, and oEnemy.x in the last section.
Yeah sorry for the eye pain lol. i actually changed a lot of that trying to figure the issue out x.x(grasping at straws). Thank you for replying! i have seen you helping people in so many of the posts i searched through trying to figure this out.

Read this over and over again: https://forum.yoyogames.com/index.php?threads/whats-the-difference-objects-and-instances.29005/

While you are very new it is probably one of the most important parts of GM to grasp.
Thank you this looks to be a good read i will study it when i get off work thank you very much for the help!!!
 
Last edited by a moderator:
Part of the problem when you're new and trying to do original coding is you don't know which direction to aim when there's something going wrong, so very often you take something that was right and make it wrong in the hopes that it'll fix the original problem and then you're bugs start growing like hydra heads. When you're bug fixing, change one thing, test, then revert the change if it didn't fix it, then change another thing, test, revert, etc. It can be an annoying process, but it'll at least let you know when you're actually going in the right direction (and as you get more experienced you'll be able to bug fix in your head without the testing a lot of the time).
 
Top