Im new to Lua and the gmod gamemode creation, and I'm having a bit of trouble. I want to deactivate a HUD when the game starts. I have 2 files, one the init.lua file, where a function is called that the game starts (there I want to change the value of HUD.lua) and a HUD.lua file, where the HUD is drawn and it contains the variable I want to change.
I tried multiple approaches, like referencing the script like:
hud.gameBegan = true
, but that didn't worked, so I tried also this putting into my init.lua:
SetNWBool("gameBegan", true)
and then I put this into the HUD.lua:
gameBegan = GetNWBool("gameBegan")
Lastly I tried this:
hud = AddCSLuaFile("hud.lua")
hud:gameChanged(true)
Unfortunatly, neither of these approaches worked for me, can somebody help me?
I would suggest keeping a table on the GM table for your gamemode to hold game states. This would then be synced between the server and the client using network messages.
Essentially how it will work is once the game starts, the server will send a network message to every player telling them that game started is true. When a new player joins, they will also need to know whether the game has started yet or not, and so we will also have to send a network message from the server to any new player that joins, telling them whether game started is true or false.
Once the game ends we need to also inform every player that the game has ended.
To start we need to store the states somewhere, and since whether a game has started or not relates to the gamemode it may be best to store it on the GAMEMODE table, and it also needs to be defined for the server and each player, so we should use the GAMEMODE table in
gamemode/shared.lua:In your
gamemode/init.luafile (which runs on the server) you may then add something like this:If you already have a
GM:PlayerInitialSpawn(ply)function then simply merge the contents of that one with yours.(Note you will need
DEFINE_BASECLASS("gamemode_base")at the top of yourinit.luafile to makeBaseClassavailable if you don't already.)In you
gamemode/cl_init.luafile you need to now write the code on the player's client that can receive the network message:You can then set the sate of whether the game has started using
GAMEMODE:SetGameStarted(true)orGAMEMODE:SetGameStarted(false)in any serverside script. And its value can be used withGAMEMODE.States.GameStartedon both the client and the server.e.g.