• 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!

Lua Passing Variables to\from another file

mackerel

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

this is the function I have created in server\lua folder called passTarget.lua. It should enable you to pass the player target table from spells for example, and access it from another folder.

Lua:
local value = {}

local function saveTarget(originalTarget)

   return originalTarget
end

value.saveTarget = saveTarget
value.getTarget = value.saveTarget

return value

This is the code I have my in my spells:

Lua:
local file = require("passTarget")

function onCastSpell(cid, var)
    local player = Player(cid)
    local target = player:getTarget()

    file.saveTarget(target)

    return combat:execute(cid, var)
end

This is the code I have in my talkactions:

Lua:
local file = require("passTarget")

function onSay(cid, words, param)
    print(file.getTarget)

    return false
end

But it does not work and I am not sure why. When I use the command in talkactions WITHOUT actually passing any variables i.e. without casting the spell (the one that should actually pass the target variable), it will print some table even though I have not passed anything :confused: -> it will print that in the console window.
 
Solution
why dont you use a global table like
Lua:
targets = {}
then
Lua:
target[Player(cid):getId()] = Player(cid):getTarget()
that way you can access with
Lua:
print(("Player: %s - Target of Player: %s"):format(Player(cid):getId(), target[Player(cid):getId()]))
that's what I said while testing :eek:

May I ask why do you need it?
I need to check if the target is still selected by the player after casting a spell, when using talkaction command

the target has to be the same that's why I need this function. I can't simply specify new target because it might be different to the one that was selected while casting the spell

by the way, this function is not only useful for checking the old target, it can be expanded and used for different variety of things as long as it doesn't break server and won't cause bugs in the long term :eek:
 
that's what I said while testing :eek:

I need to check if the target is still selected by the player after casting a spell, when using talkaction command

the target has to be the same that's why I need this function. I can't simply specify new target because it might be different to the one that was selected while casting the spell

by the way, this function is not only useful for checking the old target, it can be expanded and used for different variety of things as long as it doesn't break server and won't cause bugs in the long term :eek:

This will be terribly slow, reading and writing to file everytime someone uses this command. How would you handle xx people spamming this command at once?
 
Condition object
I mean, for example 100 people will use this command. Every time the file will be opened and the data read, it will take time and a lot of resources.

If you want to do it good. You can make it via c++ in server code to save last target.
 
I mean, for example 100 people will use this command. Every time the file will be opened and the data read, it will take time and a lot of resources.

If you want to do it good. You can make it via c++ in server code to save last target.

You mean like a function in C++

sadly, C# and Web php are my strong points haha so writing a function in C++. I will just break something
 
You mean like a function in C++

sadly, C# and Web php are my strong points haha so writing a function in C++. I will just break something
Make a method in c++ and add a new function to lua which uses that method. If you are strong in any other programming language you should be able to do it without problems if you look the c++ syntax.
 
why dont you use a global table like
Lua:
targets = {}
then
Lua:
target[Player(cid):getId()] = Player(cid):getTarget()
that way you can access with
Lua:
print(("Player: %s - Target of Player: %s"):format(Player(cid):getId(), target[Player(cid):getId()]))
 
Solution
why dont you use a global table like
Lua:
targets = {}
then
Lua:
target[Player(cid):getId()] = Player(cid):getTarget()
that way you can access with
Lua:
print(("Player: %s - Target of Player: %s"):format(Player(cid):getId(), target[Player(cid):getId()]))

awesome haha I didn't know it was possible

got to the stage with c++ where you could read storedTargets but couldn't figure out how to save them as actual creatures

but nonetheless thank you streamside :p
 
Never store userdata in Lua as it's only pointer to data that never is the same.
Simple example:
data/lib/core/game.lua:
Code:
if(not globalSavedTargetsTable) then
    globalSavedTargetsTable = {}
end

function Game.playerStoreTarget(player, target)
    globalSavedTargetsTable[player:getId()] = target:getId()
end

function Game.eraseStoredTarget(player)
    globalSavedTargetsTable[player:getId()] = nil
end

function Game.compareStoredTarget(player, target)
    return globalSavedTargetsTable[player:getId()] == target:getId()
end
logout.lua:
Code:
function onLogout(cid)
    Game.eraseStoredTarget(Player(cid)) --you will get memory leak if you letf the variable in global table
    return true
end
talkaction_test.lua:
Code:
function onSay(cid, words, param)
    local player = Player(cid)
    local target = player:getTarget()
    print((target and Game.compareStoredTarget(player, target)) and "Target match" or "Target don't match")
end
spell_test.lua:
Code:
function onCastSpell(cid, var)
    local player = Player(cid)
    Game.playerStoreTarget(player, player:getTarget())
    return combat:execute(cid, var)
end
 
Never store userdata in Lua as it's only pointer to data that never is the same.
Simple example:
data/lib/core/game.lua:
Code:
if(not globalSavedTargetsTable) then
    globalSavedTargetsTable = {}
end

function Game.playerStoreTarget(player, target)
    globalSavedTargetsTable[player:getId()] = target:getId()
end

function Game.eraseStoredTarget(player)
    globalSavedTargetsTable[player:getId()] = nil
end

function Game.compareStoredTarget(player, target)
    return globalSavedTargetsTable[player:getId()] == target:getId()
end
logout.lua:
Code:
function onLogout(cid)
    Game.eraseStoredTarget(Player(cid)) --you will get memory leak if you letf the variable in global table
    return true
end
talkaction_test.lua:
Code:
function onSay(cid, words, param)
    local player = Player(cid)
    local target = player:getTarget()
    print((target and Game.compareStoredTarget(player, target)) and "Target match" or "Target don't match")
end
spell_test.lua:
Code:
function onCastSpell(cid, var)
    local player = Player(cid)
    Game.playerStoreTarget(player, player:getTarget())
    return combat:execute(cid, var)
end

omg thank you haha that was cool. I've noticed how the table was expanding. Who knows how big it could grow :eek:
 
Back
Top