Using object in code, only works for one instance

L

LawH

Guest
I'm not sure if posting my code will help, but here:


{if obj_spear.image_index > 6 && obj_spear.image_index < 8{

if place_meeting(x,y,obj_spear) == true {
obj_p1.att_ID = ID

if obj_p1.total_av > dv + ini_enum.hit.sure && obj_p1.att_ID = ID {
instance_create(x,y,obj_bloodsmall)
dead = 1;
obj_p1.att_ID = 0;
}
else if obj_p1.total_av > dv + ini_enum.hit.easy && obj_p1.att_ID = ID {
instance_create(x,y,obj_bloodsmall)
wound += 1;
obj_p1.att_ID = 0;
}
else if obj_p1.total_av > dv + ini_enum.hit.average && obj_p1.att_ID = ID {
instance_create(x,y,obj_bloodsmall)
cut += 1;
obj_p1.att_ID = 0;
}
else if obj_p1.total_av > dv + ini_enum.hit.difficult && obj_p1.att_ID = ID {
// instance_create(x,y,obj_bloodsmall)
cut += 1;
obj_p1.att_ID = 0;
}
}
}}


So what is happening here should be, that once I pick up a spear and have it in my hand, and attack this object, this code will run. The problem is, that luckily I placed two spears from earlier tests, and for some reason, the other spear works, but the other does not. I have not yet added a generalized attack for all weapons, because I am still testing this out, so that is why I specify the spear as the object that checks the collision event. I will make it a parent thing once I get this to work.

I don't understand why attacks only work with the other spear, but not the other one. Even if I pick the other spear up first, it still does not work. Any ideas on why this could be?
 

Felbar

Member
you need to store the instance id of which obj_spear you picked up, possibly?
is the above code running in the player object?

Code:
var which_spear = instance_place(x,y, obj_spear);
 
Last edited:
L

LawH

Guest
I have that stored as well, as "obj_p1.pri_ID = ID", where ID is id, I just use the variable since I automatically always capitalize ID. The ID clears to 0 once you drop the weapon.

So, once I pick up a weapon, the weapon ID is stored for as long as I hold the item. Once I drop it, the ID becomes 0.

I have tested it out by drawing the primary weapon ID, and it changes as intended. There's something I'm missing here but have no idea what it is.

With that single spear I can stab different objects just fine, so everything works as intended with that single spear.
 

Felbar

Member
I don't understand what you mean about ID, GML is case sensitive so ID and id are no the same, I guess I need to know what object is running this code ?
 
L

LawH

Guest
In my create event I have ID = id. I did this because when I type, I automatically type ID, instead of id. This was my way to avoid little mistakes. So in my case ID is id.

The first instance of the spear that I place into the room works, but any extra instances do not work.
 
L

LawH

Guest
The object running this code is the parent of the target of the attack. It would also seem that the one that I pick up first, even after changing the instance order in the room works, but whichever I pick up second does not.
 
Last edited by a moderator:

TheouAegis

Member
The object running this code is the parent of the target of the attack.
What event is this in? If it is in a collision event, you should use "other" to refer to the spear colliding with the attack target. If it's in the Step event, then you would probably be better off using "with instance_place(x,y,obj_spear)" to target the spear actually colliding with the enemy.
 
Last edited:
L

LawH

Guest
It's in a step event. I tried to use other but it didn't work at all. I also tried this with statement or something similar, but I will try that again to see if I made a mistake somewhere before.

Could you change a piece of my code and show me what exactly you would change, just in case?
 
L

LawH

Guest
Now when I attack with the second spear, and then attack with the first spear, the damage from the second spear is registered once I attack with the first spear. I'm at a loss.

EDIT: No, now all the targets are attacked when one is. I'm at even a further loss now.
 
L

LawH

Guest
Ok, so I did it the old fashioned way by creating an object that handles the collision at exactly the right frame. I thought it might mess up some systems but now I can finally attack, and give it a nice effect as well so one can see where the hitbox is etc.

Thanks guys, sorry I was a bit vague about the problem, since a lot of different objects affect each other and there was not a simple way to show exactly what the problem was without posting tons of code.
 

TheouAegis

Member
Yeah, you should avoid having multiple objects affect other objects. my rule of thumb is always think about things and practical real-world terms. For example, if somebody stabs you with a knife, do you make the knife penetrate your skin and wound you? No, then I've does that on its own. Or, you could argue the person say avenue does it. Okay, bad example. If somebody shoots you, do you make the bullet wound you? No, the bullet itself wounds you. Play argument can't be mad sometimes it's easier to have the enemy detect collision with a bullet rather than the bullet detect collision with the enemy, which is a valid argument, but one that can be countered by using parent objects for the enemies.

But as long as you have something that works now, that's all you need to worry about.
 
L

LawH

Guest
I am very new to this, only been doing my game for a week, so I sometimes make conscious decisions that I know I will have to change at some point when I am more familiar with gms.

Right now my problem is, that persistent items will spawn in every room, and not stay only in the room they are designated to, and, if I carry an object, that object will be dropped whenever I enter a new room.

I have to redo my inventory stuff definitely, but so long as I get something functional at least as a place holder I can move on and keep learning, rather than keep working on one thing for days on end.
 
L

LawH

Guest
I have no idea what happened. In my opinion I changed nothing in this code, which ran PERFECTLY until now.

if alarm[0] == -1 {
if instance_exists(obj_p1) {


if (obj_p1.attackB == true && spear_oid == true && obj_p1.pri_ID == ID && alarm[8] == -1) {
alarm[8] = room_speed/2
image_index = 0 // Make sure attack sprite starts from first image
image_speed = 1 //set image speed of weapon, can be different with different weapons later
obj_p1.oxy -= 1 }

if image_index == 7 {
instance_create(obj_p1.x,obj_p1.y,att_p1_long); }

if image_index >= 9 {
image_index = 0;
image_speed = 0; }

_____
Room speed is at 30, image speed does go to 1 and stay as 1, but the sprite won't animate no matter what. I am at a loss, since like said, I changed or added nothing, and now suddenly it just doesn't work. I might have accidentally hit something with my laptop mousepad while moving my actual mouse, which does happen sometimes, but I can't find what is wrong. The problem is, that the image index is not increasing even though image speed is 1.
If I set image speed to 2, it does increase, but it never hit's the 7th or 9th image I don't think, and even then the sprite does not animate, even though the image index goes up.

____________

EDIT: Image speed 2 increases the image index, creates the damage object, and after it reaches 9, it resets to 1. However, the sprite will still not animate.

So yeah, why does image speed not increase the index number, and why does the sprite not animate?
 

chamaeleon

Member
I have no idea what happened. In my opinion I changed nothing in this code, which ran PERFECTLY until now.

if alarm[0] == -1 {
if instance_exists(obj_p1) {


if (obj_p1.attackB == true && spear_oid == true && obj_p1.pri_ID == ID && alarm[8] == -1) {
alarm[8] = room_speed/2
image_index = 0 // Make sure attack sprite starts from first image
image_speed = 1 //set image speed of weapon, can be different with different weapons later
obj_p1.oxy -= 1 }

if image_index == 7 {
instance_create(obj_p1.x,obj_p1.y,att_p1_long); }

if image_index >= 9 {
image_index = 0;
image_speed = 0; }

_____
Room speed is at 30, image speed does go to 1 and stay as 1, but the sprite won't animate no matter what. I am at a loss, since like said, I changed or added nothing, and now suddenly it just doesn't work. I might have accidentally hit something with my laptop mousepad while moving my actual mouse, which does happen sometimes, but I can't find what is wrong. The problem is, that the image index is not increasing even though image speed is 1.
If I set image speed to 2, it does increase, but it never hit's the 7th or 9th image I don't think, and even then the sprite does not animate, even though the image index goes up.

____________

EDIT: Image speed 2 increases the image index, creates the damage object, and after it reaches 9, it resets to 1. However, the sprite will still not animate.

So yeah, why does image speed not increase the index number, and why does the sprite not animate?
Just use the debugger and a breakpoint at an if statement of interest and inspect variable content and see which one does not look right to you in order to create a true result value. Or use show_debug_message() before and inside if statements with sufficient content to give you the same information.
 
Top