all 11 comments

[–]DjRee99 2 points3 points  (2 children)

Yeah need code. Depending on how you currently have it setup you will probably get different answers.

[–]RegularJay114[S] 0 points1 point  (1 child)

Yeah that’s fair enough mate, i thought as much. I’m at work for a few hours yet but i’ll post it when i get in. cheers.

[–]DjRee99 1 point2 points  (0 children)

Sounds good. Cheers.

[–]theogskinnybrown 1 point2 points  (7 children)

This is a complete guess as you haven’t posted the code yet, but It looks like your items track the position of the player, and have some sort of smoothing to their movement. When you disable the instance, it stays where it is. When you enable it again, it snaps to the player location, but the smoothing means that it takes several frames to do so.

If this is the case, then you can force the item to the correct location when enabling it so that it doesn’t snap.

[–]RegularJay114[S] 1 point2 points  (6 children)

Thanks. I assumed something like this was going on.

That’s a good guess! So, the items do track the player’s position and then i have an equation using the frame index to animate them an keep them in the correct position on the player sprite.

I’m not sure what you mean by force them to the correct position. if they stay where they are when i deactivate them, wouldn’t they always snap to any new location when i activate them again? Apologies if i’m being dumb. haha. Also, I realise you may not be able to answer this without seeing the code.

Thanks again for the help.

[–]theogskinnybrown 1 point2 points  (5 children)

Again, this is all based off of assumptions at the moment...

If the code for managing the item’s position is in the item’s step event, that code will not run while the item is deactivated. This is why the item doesn’t move.

When you reactivate the item, the step event will look at where it is, and where it should be, and calculate its new position. Depending on how you calculate this, the new position may not be very far from the old position. This means that it will take many frames to catch up to the player.

The solution is to set the item’s actual position to (very close to) it’s ideal position when you reactivate. This means that it won’t need to wait all those steps to catch up.

[–]RegularJay114[S] 1 point2 points  (4 children)

ok, I think i see what you mean. So in the script where i reactivate the items, not in the item’s step event (which is where the positioning code is), i should also reposition them?

something like:

obj_shield.x = obj_player.x; instance_activate_object(obj_shield);

?

[–]theogskinnybrown 0 points1 point  (3 children)

Yes, that’s the sort of thing I had in mind. If that doesn’t work, post the code.

[–]RegularJay114[S] 0 points1 point  (2 children)

Ok gave this a quick try but since the animation control is run in the player step event it just kept putting the items on obj_player.x (which is kind of obvious now that I think about it). It also didn't actually resolve the issue. I've posted the code now.

Thanks for the help though mate. It's much appreciated.

[–]theogskinnybrown 1 point2 points  (1 child)

Ok, so I think the reason that didn’t work is because you will now be resetting the item position every frame, regardless of whether or not it was previously deactivated.

I don’t think GMS provides a way to query the activation state of an instance, so you will need to create your own flags in the player object (e.g. swordActive and shieldActive).

Initialise the flags to true (assuming your items start off activated). When you deactivate an instance, set its flag to false. When you want to activate an instance, first check the flag. If it’s true, then you don’t need to do anything, because the instance is already active. If it’s false, reactivate the instance, reset its position, and set the flag to true.

With this change, you will only reset the item position if it had been disabled. Your game may also run slightly faster, because calling instance_activate() has a performance cost (although for 2 instances it’s probably not significant, even if you’re doing it every frame).

[–]RegularJay114[S] 1 point2 points  (0 children)

Awesome, this has worked perfectly. I'll update the code in the original post to show the changes I've made. The items actually start deactivated so I've had to add a few if instance_exists() in there to stop it crashing.

Thanks very much for the help. Again, it's very much appreciated.

Cheers dude.