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?
CodePudding user response:
I am not familiar with GMod but maybe this can work?
In your init.lua file, you can use the include function to include the HUD.lua file and then set the variable to deactivate the HUD.
Here is an example:
include("HUD.lua") -- include the HUD file
HUDEnabled = false -- set the variable to false
In your HUD.lua file, you can then check the value of the variable before drawing the HUD:
if HUDEnabled then
-- code to draw HUD
end
You can also move the variable in the HUD.lua file so you can use it in the HUD.lua file and init.lua file.
-- HUD.lua
local HUDEnabled = true
if HUDEnabled then
-- code to draw HUD
end
-- init.lua
include("HUD.lua")
HUDEnabled = false
CodePudding user response:
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
:
GAMEMODE.States = GAMEMODE.States or {}
GAMEMODE.States.GameStarted = false
In your gamemode/init.lua
file (which runs on the server) you may then add something like this:
util.AddNetworkString("MyGamemode.GameStartedSync")
function GM:SetGameStarted(hasStarted)
GAMEMODE.States.GameStarted = hasStarted
-- We have updated the state on the server, now update it
-- for each player on their client
net.Start("MyGamemode.GameStartedSync")
net.WriteBool(hasStarted)
net.Broadcast()
end
function GM:PlayerInitialSpawn(ply, transition)
BaseClass.PlayerInitialSpawn(self, ply, transition)
-- Player has joined, so send them the current game state
net.Start("MyGamemode.GameStartedSync")
net.WriteBool(GAMEMODE.States.GameStarted)
net.Send(ply)
end
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 your init.lua
file to make BaseClass
available if you don't already.)
In you gamemode/cl_init.lua
file you need to now write the code on the player's client that can receive the network message:
net.Receive("MyGamemode.GameStartedSync", function()
local hasStarted = net.ReadBool()
GAMEMODE.States.GameStarted = hasStarted
end)
You can then set the sate of whether the game has started using GAMEMODE:SetGameStarted(true)
or GAMEMODE:SetGameStarted(false)
in any serverside script. And its value can be used with GAMEMODE.States.GameStarted
on both the client and the server.
e.g.
if GAMEMODE.States.GameStarted then
DrawMyHud()
end