Pathing Question

Discussion in 'Programming' started by Paul Thayer, May 31, 2017.

  1. Paul Thayer

    Paul Thayer Guest

    When creating an instance of an object from another object (e.g. new_instance=instance_create(x,y,new_object) how would you also set a path for that other object without setting the path within code of the other object?

    Also, when using paths for movement how can you change animation based on direction the object is "moving"?
     
  2. redbeard990

    redbeard990 Guest

    a couple of ways. with (new_object) path_start(path, speed, endaction, absolute); or new_object.path_start(path, speed, endaction, absolute);

    if (x > 0 )
    {
    sprite_index = sprPlayerRight;
    }

    if (x < 0)
    {
    sprite_index = sprPlayerLeft
    }

    if (y < 0)
    {
    sprite_index = sprPlayerUp
    }

    if (y > 0)
    {
    sprite_index = sprPlayerDown
    }
     
    Paul Thayer likes this.
  3. Paul Thayer

    Paul Thayer Guest

    I tried the second way and got this error.
    upload_2017-5-31_15-13-52.png
     
  4. Paul Thayer

    Paul Thayer Guest

    Oops. I can't post links yet, bummer. PATH_ERROR.PNG
     
  5. Drenathor

    Drenathor Member

    Joined:
    Dec 1, 2016
    Posts:
    54
    @Paul Thayer, to answer your first question about spawning an object and assigning it to a path, you need to use the with() function as that will allow you to run code as if you were running it inside that object's create event (in this case).

    Code:
    new_instance = instance_create( x, y, new_object );
    with( new_instance ) {
          path_start( path, 4, path_action_reverse, 0 );  // I just copied the sample function from the manual, you'll want to replace those values with what makes sense in your game.
    }
    
    As far as your second question, that's a bit more complex to answer without knowing what you're working on. For instance, should the object rotate to face wherever it's going? Should it swap out images so that you see the front, back or sides of a character or simply flip the animation left or right (to name a few possibilities)
     
    Paul Thayer likes this.
  6. Paul Thayer

    Paul Thayer Guest

    Like this?
    upload_2017-5-31_15-28-1.png
     
  7. Paul Thayer

    Paul Thayer Guest

    Question 1, resolved! Thanks Drenathor and redbeard990.
     
  8. Drenathor

    Drenathor Member

    Joined:
    Dec 1, 2016
    Posts:
    54
    @Paul Thayer Looks like you got it working :)

    Feel free to post more info on part 2 of your question as well.

    For instance in a platformer game where you really only need to flip the sprite left or right you can do something like this:
    Code:
    spd = x - xprevious;
    if( spd != 0 )
        image_xscale = sign( spd );
    
    In a top down game like asteroids if you're using direction and speed to control your object you could do something as simple as:
    Code:
    image_angle = direction;
    
    (BTW, both these examples are expecting that your image axis is centered and the sprite is facing to the right.)

    If you want any explanations as to how either of those examples work let me know and I can explain them in more detail. I was just trying to illustrate how varied the solutions are depending on what you're trying to do.
     
  9. Paul Thayer

    Paul Thayer Guest

    I lt turns out I was actually doing that correctly it was just happening so fast. I store x at the end of each frame in variable old_x. Then I compare it and change the sprite_index as appropriate. Thanks again.
     
  10. Drenathor

    Drenathor Member

    Joined:
    Dec 1, 2016
    Posts:
    54
    Oh cool! Glad to hear you got everything working then :)

    One final thought on using old_x, game maker is already tracking the current and previous values of x in x and xprevious. So unless you're doing some special calculations on it you might want to swap out old_x for xprevious since you're just having GMS 2 track it twice by storing that value yourself.
     
  11. Paul Thayer

    Paul Thayer Guest

    Sweet! Good to know. Is there a list of GMS default variables?
     
  12. Drenathor

    Drenathor Member

    Joined:
    Dec 1, 2016
    Posts:
    54
    Hmmm... I'm not sure that the documentation for GMS 1.4 has such a list but the documentation for GMS 2 has a good starting list at https://docs2.yoyogames.com/source/...rence/instances/instance_variables/index.html

    You could always look through that if you're curious. Odds are most if not all of them will work with GMS 1.4 as well and if you have any questions you can always write in the variable name and middle mouse click on it inside game maker studio to pull up the documentation on it.
     
  13. Paul Thayer

    Paul Thayer Guest

    @Drenathor, I tried using the "xprevious" variable for a compare with X in the "Step" action to determine whether to flip the sprite or not, but it did not function correctly. I went back to setting an "old_x" variable to X at the end of the step (after everything moved) for comparing on the next step. It works, but I'm not sure why "xprevious" does not. Perhaps I am using the wrong "Step" action?

    Also, when creating the instances I am setting the "health" variable for each separately. For some reason the "health" variable is being set the same for ALL instances created even though I use a unique name for each. In the code below the health variable for "ship_1" and "ship_2" are set to 2. As you can see I tried to set the health of "ship_1" to 4.

    upload_2017-6-1_21-15-46.png
     
  14. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,341
    "health" is a built in global variable. So changing it affects all ships. Instead, try using "hp" as the variable name for your ships health.
     
  15. Paul Thayer

    Paul Thayer Guest

    I see, so I do have to make my own variable. That's a bummer. Seems like it should work differently as I am applying it to a specific instance number, not all. Oh well, thanks for the help.
     
  16. IndianaBones

    IndianaBones Member

    Joined:
    Jul 5, 2016
    Posts:
    2,341
    Yes, I get that it can seem that way. However, when something is a global variable, it doesn't belong to any instance at all.

    Globals just float around, accessible from anywhere. And you can only have one global variable with the same name at a time.

    As opposed to instance variables, if you set an "hp" for an instance, it only belongs to that instance.

    Just so you know, "health" in particular as I said is a "built-in" global variable. They are planning to remove it eventually from GameMaker I think, because it says in the GMS 2 manual it is only included for backwards compatibility.

    So it's better to get used to using your own variables for instances.

    And finally, "health" is not that useful if you have more than one thing that needs to track health, like if you have a two-player game, each player will need their own health variable (not called "health" of course).
     
  17. KaiXtr

    KaiXtr Guest

    For Animation,To change the sprite depending on the direction, you can choose to rotate the sprite:

    Step Event of Created Instance
    Code:
    image_angle=direction
    Or you can choose to put other sprites:
    Code:
    if direction>0 and direction<46{sprite_index=sprite}
    if direction>45 and direction<91{sprite_index=sprite}
    if direction>90 and direction<136{sprite_index=sprite}
    [...]
     
  18. Drenathor

    Drenathor Member

    Joined:
    Dec 1, 2016
    Posts:
    54
    @Paul Thayer,

    Sorry about the delay in getting back, it's been a busy few days. It sounds like you got things working and some good advice from IndianaBones so this comment adds little to the discussion other than minor points of clairification. So if you're interested in a bit more info on the topic of variable types feel free to read on, otherwise feel free to ignore this response :p

    --------------------------------------

    That's interesting that xprevious didn't work for you, and to be honest I'm not quite sure why that would be. Game maker is supposed to track three things for both x and y on all objects. When the object is created it stores the initial value of x in xstart, the current value of x in x and the last value of x in xprevious (which is supposed to be set before the begin step action). And you can see the documentation for it at https://docs.yoyogames.com/source/d...vement and collisions/movement/xprevious.html if you wanted to know more.

    But if your x_old works and xprevious doesn't, it's not the end of the world as it's an admittedly minor optimization. As far as the health question, IndianaBones is correct. If you want to know a bit more about this, there are actually 3 types of variables in Game Maker. The first (and most common) is an instance variable. This is just made by picking a keyword and assigning a value to it. (myvar = 3) And as long as you're working in an object or referencing that object you can access that variable (obj_player.myvar).

    The second type of variable is called a local variable and is created by adding the keyword var in front of it. (var myvar = 3) Essentially this tells game maker to forget about this variable after the given task has run. So unlike the instance variable if you run a local variable in a script that variable will be forgotten after the script is run and can't be accessed again which is great for keeping memory free when some variables are only used to temporarily hold a calculation.

    The last type of variable is a global variable and they are set by appending them to the "global." prefix. (global.myvar = 3) These variables are unique in that once created they can be accessed from anywhere and they never refer to any particular object. This is useful for instance if you had a difficulty setting that you wanted to be accessible to all objects but only needs to be set once. Health is a global variable that game maker studio registers for you when the game starts, so setting health affects health globally and not just for the object you were currently accessing. Now admittedly game maker breaks it's own rules a bit here and uses "globalvar health" to set it up so that health becomes a global var without the global. prefix. Not only is the advised against because it's going to eventually be depreciated, but it creates the exact issue you had. Namely, a variable that isn't recognizable as a global without prior knowledge.
     

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