• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

TFS 1.X+ Global.lua variables

mackerel

Well-Known Member
Joined
Apr 26, 2017
Messages
398
Solutions
18
Reaction score
72
Hi,

just a quick question that I was curious about. Let's say we have a fixed value specified in our lua spells file like that:

Lua:
local myVariable = 5

local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, 68)
setCombatFormula(combat, COMBAT_FORMULA_LEVELMAGIC, -4.0, 1, -4.0, 1)

function onCastSpell(cid, var)
    print(myVariable)

    return doCombat(cid, combat, var)
end

if myVariable will be the same for all players, wouldn't it be pointless to allocate the same variable for each player? So instead I was thinking that we can just simply specify that in global.lua as

Lua:
MY_VARIABLE = 5

and then in spell.lua

Lua:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, 68)
setCombatFormula(combat, COMBAT_FORMULA_LEVELMAGIC, -4.0, 1, -4.0, 1)

function onCastSpell(cid, var)
    print(MY_VARIABLE)

    return doCombat(cid, combat, var)
end

Let me know what you think, thanks
 
Solution
Hi,

just a quick question that I was curious about. Let's say we have a fixed value specified in our lua spells file like that:

Lua:
local myVariable = 5

local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, 68)
setCombatFormula(combat, COMBAT_FORMULA_LEVELMAGIC, -4.0, 1, -4.0, 1)

function onCastSpell(cid, var)
    print(myVariable)

    return doCombat(cid, combat, var)
end

if myVariable will be the same for all players, wouldn't it be pointless to allocate the same variable for each player? So instead I was thinking that we can just simply specify that in global.lua as

Lua:
MY_VARIABLE = 5

and then in spell.lua

Lua:
local combat =...
Hi,

just a quick question that I was curious about. Let's say we have a fixed value specified in our lua spells file like that:

Lua:
local myVariable = 5

local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, 68)
setCombatFormula(combat, COMBAT_FORMULA_LEVELMAGIC, -4.0, 1, -4.0, 1)

function onCastSpell(cid, var)
    print(myVariable)

    return doCombat(cid, combat, var)
end

if myVariable will be the same for all players, wouldn't it be pointless to allocate the same variable for each player? So instead I was thinking that we can just simply specify that in global.lua as

Lua:
MY_VARIABLE = 5

and then in spell.lua

Lua:
local combat = createCombatObject()
setCombatParam(combat, COMBAT_PARAM_TYPE, COMBAT_PHYSICALDAMAGE)
setCombatParam(combat, COMBAT_PARAM_EFFECT, 68)
setCombatFormula(combat, COMBAT_FORMULA_LEVELMAGIC, -4.0, 1, -4.0, 1)

function onCastSpell(cid, var)
    print(MY_VARIABLE)

    return doCombat(cid, combat, var)
end

Let me know what you think, thanks
When your server load a file such a spell file, whatever variables isn't in a function, becomes global within the limited range, and only loaded at startup.
After that, the only loaded things are within the functions themselves.
So variables outside a function, is never reloaded after startup.

And having a global variable make it accessible from any script/function in the server.
however state a variable as "local" within a script, the variable will ONLY exist within that script.
 
Solution
When your server load a file such a spell file, whatever variables isn't in a function, becomes global within the limited range, and only loaded at startup.
After that, the only loaded things are within the functions themselves.
So variables outside a function, is never reloaded after startup.

And having a global variable make it accessible from any script/function in the server.
however state a variable as "local" within a script, the variable will ONLY exist within that script.

Ah that makes sense, so even if I declare these in the file itself it will become global but will be limited to that file by using local. Oh well, that's all I wanted to know and no need to put it in the global.lua then. Thanks
 
Back
Top