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

SOLVED Collision checks in local variables executes slower?

Hello! I found something interesting while messing around with my code.
Here it is, clearly following one of Shaun Spaulding's tutorials:

In the Step Event:
GML:
#region//set controls
key_left = (keyboard_check(vk_left)) || (keyboard_check(ord("A")));
key_right = (keyboard_check(vk_right)) || (keyboard_check(ord("D")));
#endregion

#region //set variables
var onwall = place_meeting(x+hsp,y,obj_wall);
#endregion

#region //movement code
var move = key_right - key_left;
hsp = move * maxspd;
#endregion

//Horizontal Collision
if (place_meeting(x+hsp,y,obj_wall)) {
    while (!place_meeting(x+(sign(hsp)),y,obj_wall)) {
        x = x+(sign(hsp));
    }
    hsp = 0;
}
x = x + hsp;
All it does is just move the player left and right, and it stops when it collides with a wall. This works just fine! But then I remembered that collision checks are expensive and that storing them in a variable is cheaper.
However when I do this:

GML:
//Horizontal Collision
if (onwall) {
    while (!place_meeting(x+(sign(hsp)),y,obj_wall)) {
        x = x+(sign(hsp));
    }
    hsp = 0;
}
The player gets stuck. Presumably because it has gone 4 pixels in to the wall, thus making the "onwall" condition always true.
So my question is why does storing collision checks in a variable cause it to have some sort of delay? Should I even store them in a variable? I'm also a beginner to coding in general, so it wouldn't surprise me if the problem lies within my own logic :p
 

Simon Gust

Member
It's a matter of code order.
Keep track of when you set and use "hsp". In your original code you always use the most up to date data.
In your second code, onwall uses hsp from last frame as hsp has been reevaluated this frame (see hsp = move * maxspd is below var onwall = place_meeting(x+hsp,y,obj_wall))

If you copied this code from the tutorial, it is not your fault. The tutorial-giver should have done a better job and put the onwall declaration below the hsp decleration.
 

FrostyCat

Redemption Seeker
But then I remembered that collision checks are expensive and that storing them in a variable is cheaper.
It's cheaper only when the result is used more than once. In your code I see it used only once, so there's nothing to save. Even worse, by putting it in a different position relative to the line setting up hsp, you've accidentally changed the logic of the code.
 
Ah I see! I would never have spotted that. So basically the reason why I couldn't get it working was because var hsp = move * maxspd; was below var onwall = place_meeting(x+hsp,y,obj_wall); which basically just turned it in to var onwall = place_meeting(x+0,y,obj_wall); thus making it check for a collision with a wall on the very same frame it was colliding with it instead of 4 pixels ahead. Hopefully, I'm understanding this correctly.

Also to be fair to the tutorial-giver, I was the one who put the collision check in to a variable, not him. I just couldn't understand why HIS code worked but not MINE... now I know.

Thank you so much for the quick replies :D
 
Top