Global variables (Lua)

Status
Not open for further replies.

amperbee

thunderdome denizen
Retired Staff
I was writing around then I wondered about something I haven't tested yet.

Imagine a command (i.e. "password") that takes two arguments:
The first only takes "set" and "login" and the second will be explained now.
When you write "password set", after that you can write any string (i.e. "potato", for the sake of explanation).
But when another client writes that down, he must use "login" and then write "potato", to be told that the password is correct and set a variable to allow this user to mess with whatever was allowed.

If I set this password to "potato" before any client connects, wouldn't the client-side variable of the script be set to something else, the default, when it connects instead of the server-side variable?
If yes and only if yes, is there any way to prevent that without CVars? Like, using some sort of global variables?


Note: I'm aware I can use CVars but whatever I'm trying to do doesn't need CVars. I mean, I can prevent the users from editing the CVar but not from showing it's value to them.
 
Last edited:
For the purpose you're thinking of in general, use Terminal. As for the question itself, Lua does not support setting global variables so you're out of luck here. An easier way would be to create a host-controlled player variable that sets itself on other clients by using a PlayerJoin hook.
 
Lua does not support setting global variables
Let me be pedantic and mention that Terminal itself (as of the recent refactor) uses global variables, Wolfy >:U

That said, the problem with global/local variables (anything unattached to an object) is that they aren't synced on join. The NetVars hook that's supposed to handle that is, unfortunately, broken. What you can do is set the variable to something in the server global (which is a pointer to the player hosting the game), since those values are synced on join. Just make sure not to override any default player_t variables!

(If you use a bunch of variables, the smart thing to do is to make a table in server that holds them all, like server.scriptname = {} or something.)
 
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top