Game Lobby Script
Game Lobby Script
Learn IF statements, Operators, List, & While Looping
In this article, we will be using IF statements, Operators, Lists, While Loops, and String Concatenation to build our game lobby script.
- when trigger is entered by player
- when trigger is exited by player
- when event is received
- add to list
- remove from list
- send event to object
- send event with delay
- set to
- respawn player
- get item from list
- clear list
- display text
- Spawn Point
- Trigger Zone
- Text Object
- Variable List:
This is a simple script that will start and end your game based on a timer/countdown. This is an All-in-One script meant to show you the basics.
We are using a Trigger Zone gizmo to detect when a player is ready to start the game. Then we use a countdown to start the game and respawn the players in the game area. We start a second countdown to determine when to end the game and respawn the players back into the lobby area.
This might not fit perfectly with your game or idea but should be enough to get you started. Remember with all my scripts, I don't aim to hand you the solution, I teach you just enough to write your code.
Event: when trigger is entered by player
The first thing we want to do is handle what happens when a player steps into our Trigger Zone gizmo. This event will give us a player variable to tell us who stepped into the Trigger Zone gizmo. We immediately use the IF statement to determine if the game has started, if the player already exists in the playerList variable, and make sure we don't go over any predefined player count limits.
Notice how I use the NOT operator here. The NOT operator will flip a True to False and a False to True. Also, notice how I used the AND operator. This requires all 3 of these conditions to evaluate to True(or NOT False) before we can execute the code inside our IF statement.
Once all 3 conditions are found True, the first thing we do is add that player to our playerList variable then we'll run a second IF statement. Here we use another AND operator to determine if both our CountdownHasStarted boolean variable is NOT True and the number of players in our playerList variable is equal to our playerMin number variable. If we find both of those conditions True then we will set the CountdownHasStarted boolean variable to True and send the StartCountdown event to ourselves.
Event: when trigger is exited by player
Our second event will handle what happens when a player decides to leave our Trigger Zone gizmo. This event will also give us the player that left the Trigger Zone gizmo. We will use an IF statement to determine if the game has already started by checking the gameHasStarted boolean variable.
By using the NOT operator again we check if the gameHasStarted boolean is False, which will return True because of the NOT operator and allow us to execute the contents of our IF statement here which removes the player from our playerList variable.
Then we'll use another IF statement to check if there are any players left in our playerList variable. If the playerList variable is empty we'll make sure our countdown is stopped by setting the CountdownHasStarted boolean variable to False, canceling the StartCountdown event loop, setting the countdownTime number variable back to 30 seconds, and finally updating our screen by sending the PrintScreen event to ourselves.
Event: when event(StartCountdown) is received
When our script receives the StartCountdown event we start to subtract 1 second from our countdownTime number variable on line 16. Then we check to see if our countdownTime number variable is equal to 0, meaning the countdown is finished. When that happens we need to know if the game was running so we check the gameHasStarted boolean variable to determine this.
If the gameHasStarted boolean variable is True then we will set the countdownTime number variable back to 30 seconds and send the EndGame event to ourselves, but if the gameHasStarted boolean variable is False then we'll set the countdownTime number variable to 120 seconds and send the StartGame event to ourselves. Notice how the use of the ELSE statement here allows us to execute code when our conditional is False.
Likewise, if our countdownTime number variable was not equal to 0, then we'll use another ELSE to update our screen again by sending PrintScreen event to ourselves and sending the StartCountdown event to ourselves after 1 second has passed, effectively creating an event loop as long as the countdownTime number variable is not 0 or we are not canceled(like on line 12).
Event: when event(StartGame) is received
When we receive the StartGame event the first thing we will do is set our gameHasStarted boolean variable to True and respawn all of the players in our playerList variable to the Spawn Point gizmo that we linked to our GameSpawn object variable.
Using a WHILE loop, we can iterate through our players in the playerList variable by doing a check just like with our IF statements. This check will see if our playerIndex number variable is less than the length(or count) of our playerList variable. The playerIndex number variable will start out as 0, so as long as our playerList variable has more than 0 players, the code inside of our WHILE loop will execute.
We will reference the first player in our playerList variable by using the playerIndex number variable and the get item from list codeblock and respawn them to the game area then increment the playerIndex number variable by 1. The WHILE loop will execute again as long as our condition remains true.
Once our playerIndex number variable is greater than the number of players in our playerList variable we will exit the WHILE loop and make sure our playerIndex number variable is set back to 0 so that it is ready to use again. Then we will call our StartCountdown event again after a 1 second delay.
Event: when event(EndGame) is received
When our EndGame event is called, we will essentially do the opposite of our StartGame event by setting the gameHasStarted boolean variable to False, then iterating through our players in the playerList variable and respawning them to our LobbySpawn object variable that we linked to our Spawn Point gizmo. We'll set our playerIndex number variable back to 0 again so it will be ready for the next loop. Then we'll take two additional steps to clear our playerList variable of any players and call our PrintScreen event again.
Event: when event(PrintScreen) is received
Each time we receive the PrintScreen event we first clear the playerListText boolean variable so it can be used to store the text we want to display. We use another WHILE loop here to iterate through all the players in our playerList. With each loop, we take the player's name and add it to our playerListText variable along with our delimiter text variable, which is a line break in this case. Keep in mind the playerListText variable and delimiter text variable are empty on the first loop.
Once the WHILE loop exits we set the playerIndex number variable to 0 and clear the delimiter text variable for reuse. We use another IF ELSE statement to determine which message we display based on whether the playerList variable has any players in it.
That's it! I hope this was helpful. While I understand this might not solve your exact situation, the goal here was to introduce you to new concepts and show how they can be used to write your own code. If you have any questions or need any help, please stop by our DISCORD server and ask questions!