[Tutorial] Adding teams to your games |
2 | ||
Welcome to this short tutorial on adding Teams to your mini-game! For this, I'll be taking the finished version of Shamousey's Free for All tutorial and we'll work from this script to add teams. This is the script we have initially; Code Lua 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 tfm.exec.disableAutoNewGame(true) To start off, we need to determine a way to store the players and other team-related data into the different teams. In this example, we'll have only two teams Blue and Red, but we'll make it so it'll easy to expand the script to add more teams. A simple way to store players / data into different teams would be to make a table for each team (Blue and Red). Additionally, we'll put these into another "teams" table to allow for easier access later on. Inside the Blue and Red tables, we'll have another table called "playerList" to store the players in that team, and a "color" variable to store the team's respective color. The color variable will store a 0xhex code, which will allow us to determine whether a player is in Team Red or Team Blue in-game later when we use it to set each player's nametag. Code Lua 1 2 3 4 5 6 7 8 9 10 11 local teams = { Now, let's actually start putting the players into different teams by randomising them. To do this, let's actually put the code to implement this into its' own seperate function, since we might want to call this in different places. We'll call this function "seperatePlayers" since we'll be seperating the players into different teams. To implement the actual seperation of the players, we need to put conveniently put all the players into a local table (a table that we can't access outside of the function) so we can check who hasn't been selected into a team when we seperate the players. Let's do this first by looping through tfm.get.room.playerList and putting them into a "playersLeft" table. Code Lua 1 2 3 4 5 6 7 8 function seperatePlayers() Okay, this next bit is slightly harder since we're going to have to do actually seperate the players now. There's plenty of ways to do this, it just requires some thinking. The algorithm I'll be implementing will involve putting the players left in the playersLeft table to the team with the least amount of members. Let's see how we can do this.. First, we have to find the team with the least amount of players. We'll do this by looping through each team's table and compare their playerList with the playerList of the current team with the least amount of players. The current team with the least amount of players can be stored into a local table called "smallestTeam". Code Lua 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 function seperatePlayers() Now that we've got the team with the least amount of players, what do we need to do? We have to put a random player from the playersLeft table to the team! However, note that we'll have to do this process again and again until there's nobody left in the playersLeft table, so we need to put all of the code we written into a while loop. This while loop should end whenever the playersLeft is empty, indicating that all players have been randomized into a team. Code Lua 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 function seperatePlayers() We can consider this function pretty much done now, since whenever we call the function, it'll put all the players in the room into a random team from the teams table. However, we should reset the playerList for each team whenever this function is called, so players don't end up being in the two teams at the same time. Let's quickly do this. Code Lua 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 function seperatePlayers() It's time to actually call the function. If we want to have the teams be chosen only once at the start of the script, we can just call it before we load a new map for the first time. However, if we want to have different teams every round (which is how the Team mode works in official FFA), we put it inside the eventNewGame function, which gets called whenever a new round begins. Code Lua 1 2 3 4 5 function eventNewGame() Teams are finally implemented into the game! Buuut.. we don't really know who's in what team. Well actually, we have a corresponding color variable which contains a 0xhex value for each team. Let's actually use this to distinguish between players! We can loop through each team's playerList and then give every player in that playerList the color of that respective team. We'll enclose the code to implement this into another seperate function "manageTeams" so we can easily add more features to it if we want to in the future. But to be honest I just want to keep eventNewGame clean. Code Lua 1 2 3 4 5 6 function eventNewGame() Code Lua 1 2 3 4 5 6 7 function manageTeams() Woot! We're getting close to done. The only thing we've really got to do is to determine when to end a round. What we have currently is from Shamo's FFA tutorial, which lowers the time down to 5s (since a new map will load when 0s is left, done in eventLoop) and gives a win to the player alive when there's only one person left. Code Lua 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 function eventPlayerDied(name) We want to change this so all of the players in a team wins if the other team(s) are dead. We can achieve this by creating an empty table called "aliveTeams" that'll contain a list of teams that are alive, so we can check if there's only one team alive left later. Now, to fill the table we loop through the teams table, and through each team's playerList and try to find if someone is alive from that team. To check if someone is alive, we use the tfm.get.room.playerList table which contains data about the player - and access the isDead value which returns either true or false (true if the player is dead). In-case a player from the team has left the room, we'll also check if the data about the player exists. Code Lua 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 function eventPlayerDied(name) Last thing to do is to give victory to all the players in the only team alive. We can easily do this since we can access the only team alive by doing "teams[aliveTeams[1]]". To give victory to everyone in that team, we can just do the for looping method again. Code Lua 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 function eventPlayerDied(name) And that concludes this tutorial! We've implemented a Teams system into FFA, simillar to that from the actual Team Mode from the official Deathmatch module! Obviously, you should improve this script so you can add scores and more things to it, but hopefully you've learned how to implement a Teams system into all your mini-games. The manageTeams and seperatePlayers functions we made should be mostly applicable to any mini-game you want to implement teams in! If you want to add more teams, you can simply follow the template I used to make the Blue or the Red team. Here's the full code we now have; Code Lua 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 --[[ Dernière modification le 1496663460000 |
Honorabilis « Consul » 1495641840000
| 1 | ||
Good job^^ Thanks. |
Soulzone5257 « Citoyen » 1495643520000
| 0 | ||
Good tutorial |