1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GMS 2 Why some people use variables like "hsp" and "vsp"?

Discussion in 'Programming' started by Suzaku, Mar 12, 2019.

Tags:
  1. Suzaku

    Suzaku Member

    Joined:
    Jan 27, 2019
    Posts:
    50
    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?
     
  2. BaBiA Game Studio

    BaBiA Game Studio Member

    Joined:
    Jun 20, 2016
    Posts:
    828
    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.
     
    Suzaku likes this.
  3. Coded Games

    Coded Games Member

    Joined:
    Jun 20, 2016
    Posts:
    400
    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.
     
    Suzaku and nacho_chicken like this.
  4. TrunX

    TrunX Member

    Joined:
    Jun 30, 2016
    Posts:
    142
    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.
     
    Suzaku likes this.
  5. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    949
    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 likes this.
  6. Suzaku

    Suzaku Member

    Joined:
    Jan 27, 2019
    Posts:
    50
    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?
     
  7. Danei

    Danei Member

    Joined:
    Mar 23, 2018
    Posts:
    278
    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 likes this.
  8. Suzaku

    Suzaku Member

    Joined:
    Jan 27, 2019
    Posts:
    50
    Yes I understood your example perfectly, and I hadnt seen this way of doing keyboard check before, very interesting!
     
    Danei likes this.
  9. Suzaku

    Suzaku Member

    Joined:
    Jan 27, 2019
    Posts:
    50
    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?
     
  10. Danei

    Danei Member

    Joined:
    Mar 23, 2018
    Posts:
    278
    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.
     
    Suzaku likes this.
  11. TheouAegis

    TheouAegis Member

    Joined:
    Jul 3, 2016
    Posts:
    6,653
    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.
     
    Suzaku likes this.
  12. RefresherTowel

    RefresherTowel Member

    Joined:
    Jul 13, 2016
    Posts:
    1,188
    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).
     
    Bentley and Suzaku like this.
  13. TheSly

    TheSly Member

    Joined:
    Jan 16, 2017
    Posts:
    949
    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.
     
    Bentley and Suzaku like this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice