• Do not use Works in Progress as a way of avoiding the releases system! Works in Progress can be used for sharing early betas and for getting suggestions for improvement. Releases of finished content are not allowed in this forum! If you would like to submit a finished addon, click here for instructions on how to do so.

Lua: Global variables and local variables

Status
Not open for further replies.

amperbee

thunderdome denizen
Retired Staff
So I cannot make global variables (they won't work, everyone gets synched out), nor I can use the NetVars hook because the wiki says it's broken it is untested. It scares me and I don't know how to use it properly.

Is it okay if I were to use the server player as the one holding and handling global variables, more like server.var?
And, talking about variables, it would be okay if I declared a local variable outside a hook so it gets initialized with a integer, or everyone would synch out while on a netgame?

I don't have any examples right now to explain what I'm asking, I lost all my Lua projects and after a few days I forgot most of the Lua I learnt for the game.
 
Last edited:
Using the server player to synchronize global variables is fine except for the fact that, in current builds, server == nil on dedicated servers. So either you'd have to use a hack like Terminal does, or you can just say that dedicated servers are unsupported cus nobody uses them anyway lol

As far as local variables outside of hooks, the thing to keep in mind is that the game doesn't attempt to synchronize them when a player joins. If you're just using a local variable to, say, store a pointer to a specific object in the map, and you have code to find said object if the variable isn't set, that should work fine.

IIRC we tested the NetVars hook recently to see what was up and the issue was that the variable index was one lower than it should be for the receiving end. That would make it possible to work around, I believe, if you did something like this: (note that I'm just writing this from memory and it may not actually work - test it if you want!)

Code:
local sonic, tails, knuckles

addHook("NetVars", function(net)
    -- Variables to sync
    sonic = net(sonic)
    tails = net(tails)
    knuckles = net(knuckles)
    
    -- WORKAROUND TIME - network a nil variable on the end, that will be non-nil if the offset is still happening
    local memes = net(nil)
    
    if memes then
        -- Offset all variables network and this /should/ make it work?
        sonic, tails, knuckles = tails, knuckles, memes
    end
end)
I don't know how well that will hold up with multiple NetVars hooks? But it should work fine.

Also, you can't network functions stored in objects (it wouldn't be practical to add in, before you ask, because you'd basically have to send over the whole scope it's stored in due to how closures work), so make sure you aren't holding functions in player/mobj structs unless your code can stay synchronized when only some clients have it available. A better solution might be to put those function pointers in a lookup table and store the index of said function in the object instead.
 
Last edited:
Status
Not open for further replies.

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

Back
Top