开始修改

In this section we will be creating our custom gamemode. this is more complicated than creating setting mods however also allows for more freedom at the cost of being required clientside as well

the gamemode itself

We are going to make a simplified version of the "infection" gamemode.

Lets break it down into steps before we make the gamemode itself

  1. Set the gamerules such as player count, team layout and score system

  2. force all players on to 1 team at the start of the game

  3. randomly select 1 player as being the zombie

    • equip this player with the correct weapons

    • modify their health

    • provide them a speedboost

  4. have players become zombies on death

  5. have players joining mid way become zombies

  6. end the game when the last player dies.

Now lets breakdown the callbacks we will need

for steps 2 and 5 we will need AddCallback_OnClientConnected( PlayerJoined ) to trigger on join

for step 3 we will need to AddCallback_GameStateEnter( eGameState.Playing, StartGame )

for step 4 we need AddCallback_OnPlayerRespawned( RespawnInfected )

and finally to decide the winner we need a new function we havent seen yet. SetTimeoutWinnerDecisionFunc( DecideWinners )

this function is used to decide the winner when the game runs out of time by calling a function.

now that we know our steps its time to start our initialisation function

Initialisation function

first, lets declare our game settings

Next lets setup our callbacks:

great, now we have our gamemodes initialisation function we need a way for our callbacks to know some things, such as whether the game has started or not, you will see why later

Structs like this allow us to store Values that can be accessed elsewhere in the script, without having to pass them as arguments in all our functions. we place them in a struct instead of just defining them as a global variable cannot be modified by a function in squirrel, wheras a struct like this one can.

to access a variable in a struct you must format it using the name of the stuct followed by the variable itself, in this case file.HasStarted

Main functions

Now it time to start writing our callbacks

This manages what happens when a player joins, but it calls a function we dont have yet InfectPlayer so lets define it now

This function simply changes the players team, then checks if there are any survivors left, and if not sets the zombies as the winners.

Now for the script that chooses the first zombie, in order to allow more time for players to join lets put a delay on it using a thread

This function changes the file.HasStarted value to true and randomly selects a player from the player array as a target for the infectplayer function.

Next lets create the function that provides the zombies the correct equipment

Finally lets define what occurs when the match ends

Now we can save this file as _gamemode_simpleinf.gnut and place it in "ourmodsname"/mod/scripts/vscripts/gamemodes

最后更新于