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

GameMaker Why some people use variables like "hsp" and "vsp"?

Suzaku

Member
I ve seen some people using those variables like that to store hspeed and vspeed, or "grav" to store gravity value and so on. Why they use those variables?How much is that helpful?
 
It is entirely down to personal preference. I never put anything into the builtin speed, hspeed, vspeed, gravity variables. I always use my own (hsp, _hsp, whatever) just so that I know that I have complete control over these without anything being affected by the inbuilt movement variables. It's like everyone tends to use their own variables in place of things like score or health, because they want to handle everything themselves without GM getting in the way of things.
 

Coded Games

Member
A good example to not use hspeed or vspeed is if you want your game to support delta_time. In that case these built in variables would not work. In these cases it's just easier to program your own movement system rather than trying to work around GMs.
 

TrunX

Member
The build in movement variables are great for quick prototyping but for bigger projects they will always cause a ton of problems you will not have if you use your own varaibles and movement scripts.
 

Slyddar

Member
It is entirely down to personal preference. I never put anything into the builtin speed, hspeed, vspeed, gravity variables. I always use my own (hsp, _hsp, whatever) just so that I know that I have complete control over these without anything being affected by the inbuilt movement variables.
It's more then just personal preference.

To answer the OP, the built in variables like hspeed, vpseed and speed are applied to the object every step. This limits how you can use them in your coding, as you may not want them applied that way. For example, if you want to stop the player and camera pan, or display dialogue, or something where the player shouldn't move, using in built variables you would have to store them pre pause, and then re apply them after wards. Using hsp and vsp you can simply not apply them while the sequence is taking place. Speed variables also need a direction, where as hsp and vsp are easier to manage as they are simply x/y movements.

In general, they give you much better control over you code.
 

Suzaku

Member
Good to know, but where should I tell Gamemaker that my variables hsp and vsp are references to hspeed and vspeed for example? Create Event?
 
D

Danei

Guest
You wouldn't reference hspeed and vspeed, and most likely wouldn't use hspeed and vspeed at all. You'd apply changes to the x and y values directly from hsp and vsp. For example, in the step event:

Code:
if (can_move) {

hsp = keyboard_check(vk_right) - keyboard_check(vk_left);
vsp = keyboard_check(vk_down) - keyboard_check(vk_up);

x += hsp;
y += vsp;

}
In my example, hsp and vsp aren't speeds so much as directions (which could be multiplied by a movement speed variable if you like), but hopefully it makes sense what I did. hspeed and vspeed are basically doing this behind the scenes in the step event, but like others have said, not letting you control the specifics of when and how it happens.
 

Suzaku

Member
You wouldn't reference hspeed and vspeed, and most likely wouldn't use hspeed and vspeed at all. You'd apply changes to the x and y values directly from hsp and vsp. For example, in the step event:

Code:
if (can_move) {

hsp = keyboard_check(vk_right) - keyboard_check(vk_left);
vsp = keyboard_check(vk_down) - keyboard_check(vk_up);

x += hsp;
y += vsp;

}
In my example, hsp and vsp aren't speeds so much as directions (which could be multiplied by a movement speed variable if you like), but hopefully it makes sense what I did. hspeed and vspeed are basically doing this behind the scenes in the step event, but like others have said, not letting you control the specifics of when and how it happens.
Yes I understood your example perfectly, and I hadnt seen this way of doing keyboard check before, very interesting!
 

Suzaku

Member
You wouldn't reference hspeed and vspeed, and most likely wouldn't use hspeed and vspeed at all. You'd apply changes to the x and y values directly from hsp and vsp. For example, in the step event:

Code:
if (can_move) {

hsp = keyboard_check(vk_right) - keyboard_check(vk_left);
vsp = keyboard_check(vk_down) - keyboard_check(vk_up);

x += hsp;
y += vsp;

}
In my example, hsp and vsp aren't speeds so much as directions (which could be multiplied by a movement speed variable if you like), but hopefully it makes sense what I did. hspeed and vspeed are basically doing this behind the scenes in the step event, but like others have said, not letting you control the specifics of when and how it happens.
What do you say about the gravity variable? Should I do something similar to your previous answer, like
grav=4; // in the create event
y+=grav;// in the step event

instead of working directly with the gravity variable?
 
D

Danei

Guest
What do you say about the gravity variable? Should I do something similar to your previous answer, like
grav=4; // in the create event
y+=grav;// in the step event

instead of working directly with the gravity variable?
Basically, yes and for the same reasons. There's nothing inherently wrong with using the built-in movement variables if they're meeting your needs, but many users find that they become more of a hindrance than a help for projects of any real complexity.
 

TheouAegis

Member
What do you say about the gravity variable? Should I do something similar to your previous answer, like
grav=4; // in the create event
y+=grav;// in the step event

instead of working directly with the gravity variable?
Gravity doesn't work that way. You add it to vspeed, not y. I mean, you could do it that way, but that will not give you the results you want.
 
Another specific reason is the running order of the code. For instance, you might want movement to be applied AFTER a certain check is made, but using built-in variables, you have no control over when the movement is applied, it happens automatically according to GM's internal code order. So you create your own variables and that way you can position movement/checks/grav/whatever in any order you want (this is a similar reason to why most people recommend NOT using the Solid variable, it can lead to weird outcomes that you have no direct control over).
 

Slyddar

Member
You can also mix and match depending on your needs. If you are creating a quick bullet object for example, it is sometimes better to just set a direction and speed, and use a collision event to stop it, rather then mess with lengthdir_x/y and hsp/vsp. No problem in using it in your project when it suits the need.
 
Top