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

motion_set seems to add speed?

hi, I'm using motion_set() in my top down-viewed game to make enemies get slightly pushed backwards by a certain collision.

collision event for enemies:
GML:
pdir = point_direction(other.x, other.y, x, y);
motion_set(pdir, spd);
'spd' is always a characters base speed variable.

motion_set() is not supposed to add to the existing speed of the object, unlike motion_add(), right?
Anyway, it still seems to occasionally add to the enemy's speed, since sometimes the enemy just suddenly gains crazy speed, most of the time not tho.
anything wrong with the code or the event executing it?
 
Based on what you've shown, there shouldn't be any variances in the enemy's speed. motion_set() will move the enemy at the speed you specify - no more, no less. It seems that at the exact moment of collision, your spd variable is sometimes much higher than other times. There has to be a line of code somewhere that changes the spd variable. If you find and eliminate that, you're set.

Or just set the motion to an exact, unchangeable speed. For example...
GML:
pdir = point_direction(other.x, other.y, x, y);
motion_set(pdir, 10);
 

kburkhart84

Firehammer Games
Just to make it clear...assuming that your spd variable is the same the whole time...be aware that motion_set() will indeed increase the speed to that value if somehow the thing is moving slower than that beforehand. If you have friction being applied, this happens. I would also be sure that the spd variable isn't changing either, unless that is your intention(seems not to be the case since you are complaining about increased speed).

Or just set the motion to an exact, unchangeable speed. For example...
I have to say this specific thing is actually a bad idea. One, you may want to change it later, and its easier to find a variable at the beginning of the create event(or the instance/object dialog if you use that), than it is to find at a minimum one place(although easily more than one place) this "magic number" is being used. Its basically always better to have variable define things like this instead of magic numbers, even if those variables are local variables declared at the top of the event/function/whatever.
 

Roldy

Member
hi, I'm using motion_set() in my top down-viewed game to make enemies get slightly pushed backwards by a certain collision.

collision event for enemies:
GML:
pdir = point_direction(other.x, other.y, x, y);
motion_set(pdir, spd);
'spd' is always a characters base speed variable.

motion_set() is not supposed to add to the existing speed of the object, unlike motion_add(), right?
Anyway, it still seems to occasionally add to the enemy's speed, since sometimes the enemy just suddenly gains crazy speed, most of the time not tho.
anything wrong with the code or the event executing it?
You haven't given enough information to really help you.

My first suggestion would simply be to either show_debug_message the spd variable. So you can see it and know if it is changing before or after this motion_set.

Also try to dependably reproduce the issue. If you can reproduce it then you can fix it. An intermittent problem is much more difficult to solve. Once you can reproduce it then the debugger will help you.
 
thanks for answers.
yeah at first I realized I was using motion_add, I changed it to motion_set, but still (seemingly at random) objects would get suddenly faster than normal. It must be with something in their Step event that sets the 'spd' variable into something, but nothing accumulates it to my knowledge (only changes it, doens't add to it) which bugged me...
i came up with a sort of workaround tho: the Collision event sets speed=2 and at the end of the collision event, an alarm is set to 1; then in that alarm Event speed is set back to 0 for that object.
 

Yal

šŸ§ *penguin noises*
GMC Elder
It's easy to create really weird bugs like this if you reuse variable names a lot and don't bother making variables vars (temporary variables), so they stick around and can be altered in different events. If spd is only meant to be used in the collision event, for instance, it might be better to make it a var so it's removed at the end of the event and the step event can't interfere (and vice versa), and if it's meant to stick around and be used everywhere, it could be worth giving it a longer name like movement_speed so you won't accidentally reuse the name in some script.
 

kburkhart84

Firehammer Games
i came up with a sort of workaround tho: the Collision event sets speed=2 and at the end of the collision event, an alarm is set to 1; then in that alarm Event speed is set back to 0 for that object.
This reeks of what Yal is talking about then, especially if it somehow fixes the problem.
 
Top