Looping Levels

iBack

Member
Hi, I'm continuing from the tutorial that shaun created in "your first game" and i have added more features to this tutorial, such as an highscore board and pause menu. What i'm trying to do now is loop levels (fixed number of times, 10 or 20) and have different waves.

This is what I want. Once the player clears the stage, the game will use the same level (since i don't want it to look any different) but the enemy number will change, for example, stage 1 will have 3 enemies, once cleared, the game will move onto stage 2, which will then have 5 enemies. I want to be able to control how many i want on each stage, so not random.

I'm not sure how to go about doing this. Create, 20 rooms and manually place the enemies i want on the stages or use same stage and somehow do what i explained above.

this is what i have so far.

Code:
if (hp<=0)
{

    with (obj_score) thescore = thescore + 5;
    with (obj_highscoreboard) global.highscoreboard = global.highscoreboard + 5;
   
    if(instance_number(obj_enemy)==1)
   
    {
   
    instance_destroy();
    highscore_add(string(""), global.highscoreboard);
   
    room_goto(rm_game1);
   
   
   
   
    }
    else
   
    {
   
    instance_destroy();
   
    }



}
what this code does, inside the obj_enemy step event, once the player clears room 1 it will repeat, i'm unsure if this is a genuine loop. can someone please help me, how do i do what i explained above?
 
Last edited:

Ido-f

Member
You definitely don't want to create 20 levels. If I were you I'd just keep a variable somewhere that keeps track on how many level restarts there were so far, then have the enemy spawning script adapt to that variable the way you want it. For example, have a switch statement that checks that variable and spawn a different number of enemies for each value. Or use a ds_list to store the number of enemies for each room restart, and use the room restart variable as an index to that list.
 

Phil Strahl

Member
You could create an object, let's call it obj_wave_controller, that takes care of spawning the enemies for each wave. In principle it should have a variable that counts which wave the game is currently in, then spawns a number of enemies accordingly and keeps track of those, that is by storing their instance ids in a ds_list or an array. Each step it checks if any of those enemies exists. If all are destroyed, it increments the wave number, and does is all again.

Let us know if and where you are having problems with that approach, we're happy to help :)
 
well on the one hand if you want to keep it as close as you are now I would create a persistent object in the menu room, persistent objects stay into every room, simply before restarting the room add +=1 to a variable, then when the room starts read its variable and add enemies as necessary, that said make sure you don't add the persistent object in the room, or everytime you restart the room you will get another persistent object, add it in the menu or the game start screen. That said you don't have to restart your room to have waves, you could instead use xstart and ystart on anything that moves and simply add in new enemies.
 

iBack

Member
hi, took a break form this and now i'm back. i'm using shauns "Game Maker Studio 2: Enemy Wave Spawner" part of his new tutorial that he has out there, to learn ds_list. I'm unsure about the trigger part of the tutorial i've never used it before in game maker. I was wondering if setting a trigger as the size of the stage since i'm not using any other level and having the waves work just how he did it. will this work?

please watch this video to understand what i'm trying to say.
 

iBack

Member
You could create an object, let's call it obj_wave_controller, that takes care of spawning the enemies for each wave. In principle it should have a variable that counts which wave the game is currently in, then spawns a number of enemies accordingly and keeps track of those, that is by storing their instance ids in a ds_list or an array. Each step it checks if any of those enemies exists. If all are destroyed, it increments the wave number, and does is all again.

Let us know if and where you are having problems with that approach, we're happy to help :)
Code:
/// @description start waves

if(triggered  == false)

{

    triggered = true;
    
    total_waves = -1
    for (var i = 0; i < ds_list_size(waves); i++)
    {
    
     var thisentry = ds_list_find_value(waves,i);
     if (thisentry[_WAVE] > total_waves)
     {
     total_waves++;
     remianing[total_waves] = 0;
    
     }
    
        remaining[total_waves]++;
    }

}
This is inside the obj_spawner, i'm following the tutorial that shaun made but when i run the code i get the following,
"
action number 1
of Step Eventobj_player
for object obj_spawner:

trying to index a variable which is not an array
at gml_Object_obj_spawner_Collision_1faeac26_a149_4530_bb8c_756d277215e4 (line 21) - remaining[total_waves]++;
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_spawner_Collision_1faeac26_a149_4530_bb8c_756d277215e4 (line 2"

i'm unsure what is wrong with the code shaun wrote since im trying to learn ds_lists. i'm basically trying to implement his spawner waves code into what he did with the "your first game" tutorial.
 
Top