• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Very basic help with loops

P

PetrifiedPenguin

Guest
So I'm brand new to programming and even newer to GML language. I'm making a game in which the player dodges traffic as the player speeds down a 4 lane highway in a small car.

I have a step event that is supposed to check if a vehicle has passed the bottom of the room (being a y value of 800) and place it back at the top of the room, then run a loop that picks a random lane, checks that the lane isn't occupied by another vehicle already and then place that vehicle in the chosen lane (essentially picking and assigning the x value).

If the lane IS occupied it should continue the loop until it finds a free lane. This is my script so far:

Code:
if y >= 800
    {
    rep = true
    y = -48
    while rep = true
        {
            {
            lanePick = random_range(1,5)
            }
        if lanePick == 1 and !collision_line(lane1x, -96, lane1x, 0, obj_veh, false, false)
            {
            x = lane1x
            rep = false
            show_debug_message("1")
            }
        if lanePick == 2 and !collision_line(lane2x, -96, lane2x, 0, obj_veh, false, false)
            {
            x = lane2x
            rep = false
            show_debug_message("2")
            }
        if lanePick == 3 and !collision_line(lane3x, -96, lane3x, 0, obj_veh, false, false)
            {
            x = lane3x
            rep = false
            show_debug_message("3")
            }
        if lanePick == 4 and !collision_line(lane1x, -96, lane1x, 0, obj_veh, false, false)
            {
            x = lane4x
            rep = false
            show_debug_message("4")
            }
         }
    }
So this KIND OF works. It finds a lane and assigns it but every time it runs, the game stutters as though it freezes for a second, occasionally a vehicle will be assigned to a lane that isn't free so 2 vehicles will share the same space and eventually the game will freeze.

So the question is, is there an easier way to do this? And why am I having issues? Keeping in mind I'm VERY new to programming so you might have to be patient and break it down for me.

Thanks very much for any help in advance :)
 
P

PetrifiedPenguin

Guest
Well that was an embarrassing oversight :\ thanks very much. Unfortunately it's still stuttering and freezing though :(
 

DesArts

Member
Might not be the issue, but use parentheses to contain and organise your if's and other checks!

Example:
Code:
if (y >= 800)
   {
   rep = true;
   y = -48;
   while (rep = true)
       {
           {
           lanePick = random_range(1,5);
           }
       if ((lanePick == 1) and (!collision_line(lane1x, -96, lane1x, 0, obj_veh, false, false)))
           {
...
 

chamaeleon

Member
Instead of a while loop, perhaps put the four numbers, 1, 2, 3 and 4, in a ds_list and use ds_list_shuffle() to scramble it, then iterate over index value 0 to 3 with ds_list_find_index() to find an open lane. If the first entry is free, pick it, other pick entry number two if it is free, until you're out of lanes. This will ensure you don't end up with very long loops as you're capped at trying to find an empty lane to 4. If all lanes are full (not sure if you ensure this cannot happen or not), you'll just have to handle that as a case when you are done going through the list without finding an empty one. Couple this with storing your lane objects (lane1x, lane2x, ...) in another list and do something like

Code:
if (y >= 800)
{
    ds_list_shuffle(mylane_indices);
    var i = 0;
    var empty = false;
    var lane = noone;
    while (i < 4 && !empty)
    {
        lane = ds_list_find_index(mylanes, ds_list_find_index(mylane_indices, i));
        empty = !collision_line(lane, -96, lane, obj_veh, false, false);
    }

    if (empty)
    {
        // Do something with the empty lane
    }
}
Somewhere else (maybe in the create event for the object running this code), you need to create the mylanes and mylane_indices with lane1x, ..., lane4x and 1 to 4, respectively.
 
P

PetrifiedPenguin

Guest
Tried using parentheses to no avail but it does neaten everything up and its a good practice to get into so thanks for that!

Using the ds_list looks like a WAY cleaner way to do what I'm trying to do but it's bit late here atm and I'm going to have to read up on how that works as I'm still wrapping my head around that basic, so I'll have to give it a crack tomorrow and I'll let you know how I go. Thanks very much for the help all!
 

Alexx

Member
Try replacing this:
Code:
lanePick = random_range(1,5);
with this:
Code:
lanePick = irandom_range(1,5);
irandom will choose a whole integer, ie 1 2 3 4 5
random will spit out things like 1.4733 4.3893

As you're testing for a whole number, you need a whole number to check against it.
 

chamaeleon

Member
Somewhere else (maybe in the create event for the object running this code), you need to create the mylanes and mylane_indices with lane1x, ..., lane4x and 1 to 4, respectively.
As I use mylane_indices content as index into mylanes, it should contain the numbers 0, 1, 2 and 3, of course.. Also, code untested and not compiled, etc. Just written in the forum. And of course, it should be ds_list_find_value(), not ds_list_find_index() in my code above..

Or, use the list accessors ... mylanes[| mylane_indices[| i]] .. somewhat easier on the eyes...
 
P

PetrifiedPenguin

Guest
Changing random_range to irandom_range fixed 80% of the issue! Thanks for that. For some reason though, I still occasionally get a vehicle occupying the same lane as another vehicle and the script will eventually infinitely loop again, never pick a lane, crash the game and even gameMaker studio and I have NO IDEA why. I might try re-write the script using lists.

Thanks everyone for getting me this far!
 
Top