1. Hello Guest! It's with a heavy heart that we must announce the removal of the Legacy GMC Archive. If you wish to save anything from it, now's the time! Please see this topic for more information.
    Dismiss Notice

GMS 2 [Solved] How to run a large number of repetitive step events in a timeline?

Discussion in 'Programming' started by eyupp, Jan 22, 2019.

  1. eyupp

    eyupp Member

    Joined:
    Jan 22, 2019
    Posts:
    3
    I want to set up a timeline in my shmup that will:
    - Spawn an obstacle
    - Move that obstacle around the screen for a few seconds
    - De-spawn the obstacle and announce that a new obstacle can now be spawned

    I know how to get everything but the second point working. I don't want to add movement code to the obstacle itself because that would necessitate a new object for every variation of movement. Rather I want the timeline to move it. I was hoping there would be some moment range thing I could do (do X for moments 3 through 93 or something) but I haven't been able to find something like that, and making 90 individual moments all with exactly the same code seems tedious and cluttered. Is there a better way to go about this?
     
  2. Ephemeral

    Ephemeral Member

    Joined:
    Mar 1, 2017
    Posts:
    185
    Ah, okay I think I see the problem you're having.

    First of all,
    this certainly isn't true. You just have to get a little creative, and do something that'll seem basic in retrospect but so many GML coders never seem to think of for some reason... You separate the movement code and the information used by that code.

    Many. Here's how I would do it.

    Code:
    /// Timeline Moment
    
    // movement_info[INDEX] = [SPEED, DIRECTION, TIME];
    
    var movement_info;
    movement_info[0] = [2, 90, 60]; // move up at two pixels per step for sixty steps
    movement_info[1] = [3, 180, 90]; // move left at three pixels per step for ninety steps
    movement_info[2] = [2, 270, 120]; // move down at two pixels per step for one-hundred-twenty steps
    
    obstacle_create(movement_info);
    Code:
     /// obstacle_create script
    var move_info = argument0;
    
    var obstacle = instance_create_layer(xx, yy, lyr, obj_obstacle);
    with (obstacle)
    {
        move_data = move_info;
     
        var first_move = move_data[0];
        move_counter = first_move[2];
    }
    Code:
    /// Obstacle Create Event
    move_data = noone;
    move_index = 0;
    move_counter = 0;
    Code:
    /// Obstacle Step Event
    
    if (!is_array(move_data))
    {
        instance_destroy();
        exit;
    }
    
    var this_move = move_data[move_index];
    
    if (move_counter > 0)
    {
        x += lengthdir_x(this_move[0], this_move[1]);
        y += lengthdir_y(this_move[0], this_move[1]);
    
        move_counter -= 1;
    }
    else
    {
        move_index += 1;
        if (move_index >= array_length_1d(move_data))
        {
            instance_create_layer(x, y, layer, obj_announcer);
            instance_destroy();
            exit;
        }
        var next_move = move_data[move_index];
        move_counter = next_move[2];
    }
    To summarize, what's happening is that the timeline moment creates an instance of the obstacle, then passes an array to it containing the list of movements that obstacle should make. The obstacle instance then follows the info in the array in sequence, all on its own.

    EDIT: I'm assuming you want more control than you get from just, setting the object's built-in speed and direction variables from the timeline, but you could do that also.
     
    Last edited: Jan 22, 2019
  3. eyupp

    eyupp Member

    Joined:
    Jan 22, 2019
    Posts:
    3
    You're right, that does seem obvious now you bring it up. How would you suggest going about more complex movements? Can it be setup to read multiple arrays in succession, does GameMaker have inbuilt sin values?
     
  4. Ephemeral

    Ephemeral Member

    Joined:
    Mar 1, 2017
    Posts:
    185
    Just... add more entries / more granular entries into movement_info?
    And/or add additional terms to each entry, like maybe for rotation speed or like that?

    I don't understand the question. That is literally what it is already doing.

    I'm not sure what you mean by this either but it sounds like a question that the manual is for.
     
  5. eyupp

    eyupp Member

    Joined:
    Jan 22, 2019
    Posts:
    3
    Thanks for taking the time to help, I appreciate it.
     

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