Finn Montaner
Finn Montaner

Reputation: 1

How to Loop a script in gamemaker

cantSee = collision_line(x,y,obj_player.x,obj_player.y,obj_corner,false,true)
canSee = !(collision_line(x,y,obj_player.x,obj_player.y,obj_corner,false,true))

Define the loop as the following:

    if cantSee {
        cantSeeTimer = cantSeeTimer +1
    }

    if cantSeeTimer >60 {
        speed=0
        stopped=true
    } else {
        mp_potential_step(obj_player.x,obj_player.y,5,false)
    }

}

if stopped=true && canSee {
    mp_potential_step(obj_player.x,obj_player.y,5,false)
    loop()
}

I know the language is bad, but I just want to create a loop command to summon at will.

Thanks, Finn.

Upvotes: 0

Views: 2742

Answers (1)

user6061114
user6061114

Reputation:

so you haven't specified which object in your game currently has this code but it shouldn't matter too much.

So in Game Maker or Game Maker Studio there are a series of events an object can have and one of them is called a "Step" event. A step event is basically a loop that will cycle the amount of times the room speed is per second. Eg: If the room speed of a room is 30 the step event will loop 30 times per second.

I think I can see what you are trying to do and I think I have a solution for you. Since you can write GML code I am going to assume you understand how to use the GMS or GM IDE.

  1. We want to create a new object called obj_control (or you can choose a custom name). Also don't give this object a sprite as we don't want the player to see it.

  2. Now we want to add an event to our new object so make sure you still have the windows for obj_control (or whatever u called it open). and click on the 'Add Event button' shown in this image: https://i.sstatic.net/zeKQb.jpg

  3. Once you click on it, click on 'Step'. https://i.sstatic.net/DMPeW.jpg

  4. Now select 'Step' again. ('Begin Step' and 'End Step' don't do what we want so let's just ignore them)
  5. Now we need to add your code to the step event we just created. So make sure you are on the 'Control' tab and find the script editor (you should know where to find it) and drag one into the 'Actions' for the step event. https://i.sstatic.net/fyqTG.jpg
  6. Now a script editor should pop up automatically but if it doesn't just double click the "Execute piece of code". Now we just need to copy and paste all of your code into the script editor. https://i.sstatic.net/CDrzo.jpg
  7. Now click on the green tick on the top left corner of the window to save the code.
  8. Now before we are done let's make sure we define the variables in a create event. So make a create event and add this code:

    cantSee = collision_line(x,y,obj_player.x,obj_player.y,obj_corner,false,true)

    canSee = !(collision_line(x,y,obj_player.x,obj_player.y,obj_corner,false,true))

  9. After you have added that create event and inserted that code into it save all changes to the object.

  10. All that is left is to add this object we created to every room of the game so it can function.

Hopefully this helped and if it didn't just let me know and we can get it sorted.

Upvotes: 1

Related Questions