GameMaker Help with collision

Hey there comrades. Now that I got into GML coding it feels like I'm back to 2018 (spamming threads because I don't understand jacksh*t).

Anyway, got another thing for y'all:

Code:
STEP EVENT - anim

sinistra = keyboard_check(ord("A"))
destra = keyboard_check(ord("D"))
su = keyboard_check(ord("W"))
giu = keyboard_check(ord("S"))

var mov1 = destra - sinistra
var mov2 = giu - su

hsp_x = mov1 * wlk_spd
hsp_y = mov2 * wlk_spd/2

x = x + hsp_x
y = y + hsp_y

if place_meeting (x+hsp_x,y,bar)
{
    hsp_x = 0
}
if place_meeting (x,y+hsp_y,bar)
{
    hsp_y = 0
}
with (corp)
{
    x = x + anim.hsp_x
    y = y + anim.hsp_y
}
So this is a basic code for movement and collision. What I don't get is why the collision does work for the "corp" object, but not for "anim" (which is the object in which this thing is written).
After all, they both take 'hsp_x' and 'hsp_y' as variable for movement. Yet, it does not work.
Even checking the 'hsp_x' and 'hsp_y' variables via a debugger they keep being 0 once they reach that point.

Can someone X-PLAIN this crap? Thanke, much love comrades.
 
Last edited:

Slyddar

Member
Not sure about your actual question, but your code will have issues, as you are adding hsp (hsp stands for horizontal speed, so it's confusing to apply it to y via hsp_y, but your variable names, so meh) to x and y, and then checking if adding another hsp will make it collide with bar. That means the first movement of hsp could place it inside bar as that first movement has no check associated with it.

It's better to check the x hsp place_meeting first, then apply hsp if it's ok to do so, then check the y place_meeting and apply hsp_y to y if it's ok to do so.
 
Oh my Raktineous, what a moron...
Thanks comrade. Sometimes I forget that there's an ORDER when you code.

By the way, I divided the hsp in x and y because I'm coding an "isometric" platformer. So you can move in all directions and jump.
 
You need to use other here instead of anim if there can ever be more than 1 anim. Even if there is only 1 anim, if you change the object name, the code will break, so you should still use other.
A'ight, I'll keep that in mind comrade.
I use anim (the name of the object) just to make sure that GMS does not say the usual boring message:
"variable referenced once" or something along the line of that, bringing up a fatal error.
 

TheouAegis

Member
"Variable referenced once" isn't a fatal error, it's a warning you can ignore; you can even disable those errors completely. "Variable not set" will cause a fatal error, even if that variable is referenced multiple times, but at least that error tells you what's wrong. If you keep using object-indexed dot notation like that, however, you may run into a situation where you have more than 1 anim (or similarly coded object) and the game will not throw any descriptive errors when it tries to read variables from the wrong instance of anim (or similarly coded object).
 
Top