• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

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

eyupp

Member
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?
 

Ephemeral

Member
Ah, okay I think I see the problem you're having.

First of all,
I don't want to add movement code to the obstacle itself because that would necessitate a new object for every variation of movement.
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.

Is there a better way to go about this?
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:

eyupp

Member
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?
 

Ephemeral

Member
How would you suggest going about more complex movements?
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?

Can it be setup to read multiple arrays in succession?
I don't understand the question. That is literally what it is already doing.

does GameMaker have inbuilt sin values?
I'm not sure what you mean by this either but it sounds like a question that the manual is for.
 
Top