• 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 0.X how to deal with returns in LUA?

caquinha

Member
Joined
Aug 8, 2016
Messages
248
Solutions
1
Reaction score
24
how to deal with returns in LUA?

i tried to create some global functions like:

data/lib/usual.lua
Code:
function getBaseVocation(cid)
    local voc = getPlayerVocation(cid)
    print("1 data/lib/common.lua voc: " .. voc)
    if voc == 1 or voc == 4 or voc == 5 or voc == 6 or voc == 7 then
        return 1
    elseif voc == 2 or voc == 8 or voc == 9 or voc == 10 then
        return 2
    elseif voc == 3 or voc == 11 or voc == 12 or voc == 13 or voc == 14 or voc == 15 or voc == 16 or voc == 17 then
        return 3
    elseif voc == 0 then
        return 0
    end
end

so in a action/quest.lua
Code:
function onUse(cid, item, frompos, item2, topos)
            print("[action] voc -> " .. getPlayerVocation(cid))
            print("[action] baseVocation -> " .. getBaseVocation(cid))
end

The player vocation is 3, so it should return 3, but it is not returning 3, it is returning a strange number 268541257
(it is not priting print("1 data/lib/common.lua voc: " .. voc), is it right?)

On console it prints:
Code:
[action] voc -> 3
[action] baseVocation -> 268541257

Why?
 
Solution
i tried this:
Code:
local function ACTIONgetBaseVocation(cid)
    local voc = getPlayerVocation(cid)
    print("2 ACTIONgetBaseVocation: " .. voc)
    if isInArray({1, 4, 5, 6, 7}, voc) then
        return 1
    elseif isInArray({2, 8, 9, 10}, voc) then
        return 2
    elseif isInArray({3, 11, 12, 13, 14, 15, 16, 17}, voc) then
        return 3
    elseif voc == 0 then
        return 0
    end
end

function onUse(cid, item, frompos, item2, topos)
    local vocation = getPlayerVocation(cid)
    local base_vocation = ACTIONgetBaseVocation(cid)
    print("[action] voc -> " .. vocation)
    print("[action] baseVocation -> " .. base_vocation)
    local fromlib = getBaseVocation(cid)
    print("[fromlib] fromlib -> " .. fromlib)
    return true
end...
Your problem is weird, but Instead of your own script you can just use:
Lua:
local baseVocation = getVocationInfo(getPlayerVocation(cid)).fromVocation
It will return whatever you set fromvoc attribute in vocations.xml
 
Your problem is weird, but Instead of your own script you can just use:
Lua:
local baseVocation = getVocationInfo(getPlayerVocation(cid)).fromVocation
It will return whatever you set fromvoc attribute in vocations.xml

my from vocation is not like that
vocation 2 is fromvoc 2, 3=3

:(

any other idea?
 
my from vocation is not like that
vocation 2 is fromvoc 2, 3=3

:(

any other idea?
Have you tried just using the function as-is, all in 1 file?

Lua:
local function getBaseVocation(cid)
    local voc = getPlayerVocation(cid)
    print("1 data/lib/common.lua voc: " .. voc)
    if isInArray({1, 4, 5, 6, 7}, voc) then
        return 1
    elseif isInArray({2, 8, 9, 10}, voc) then
        return 2
    elseif isInArray({3, 11, 12, 13, 14, 15, 16, 17}, voc) then
        return 3
    elseif voc == 0 then
        return 0
    end
end

function onUse(cid, item, frompos, item2, topos)
    local vocation = getPlayerVocation(cid)
    local base_vocation = getBaseVocation(cid)
    print("[action] voc -> " .. vocation)
    print("[action] baseVocation -> " .. base_vocation)
    return true
end

Assuming it works correctly, you might have another function somewhere, that is identically named, and is over-riding your newer function.
 
Have you tried just using the function as-is, all in 1 file?

Lua:
local function getBaseVocation(cid)
    local voc = getPlayerVocation(cid)
    print("1 data/lib/common.lua voc: " .. voc)
    if isInArray({1, 4, 5, 6, 7}, voc) then
        return 1
    elseif isInArray({2, 8, 9, 10}, voc) then
        return 2
    elseif isInArray({3, 11, 12, 13, 14, 15, 16, 17}, voc) then
        return 3
    elseif voc == 0 then
        return 0
    end
end

function onUse(cid, item, frompos, item2, topos)
    local vocation = getPlayerVocation(cid)
    local base_vocation = getBaseVocation(cid)
    print("[action] voc -> " .. vocation)
    print("[action] baseVocation -> " .. base_vocation)
    return true
end

Assuming it works correctly, you might have another function somewhere, that is identically named, and is over-riding your newer function.

i tried this:
Code:
local function ACTIONgetBaseVocation(cid)
    local voc = getPlayerVocation(cid)
    print("2 ACTIONgetBaseVocation: " .. voc)
    if isInArray({1, 4, 5, 6, 7}, voc) then
        return 1
    elseif isInArray({2, 8, 9, 10}, voc) then
        return 2
    elseif isInArray({3, 11, 12, 13, 14, 15, 16, 17}, voc) then
        return 3
    elseif voc == 0 then
        return 0
    end
end

function onUse(cid, item, frompos, item2, topos)
    local vocation = getPlayerVocation(cid)
    local base_vocation = ACTIONgetBaseVocation(cid)
    print("[action] voc -> " .. vocation)
    print("[action] baseVocation -> " .. base_vocation)
    local fromlib = getBaseVocation(cid)
    print("[fromlib] fromlib -> " .. fromlib)
    return true
end

it prints:
Code:
2 ACTIONgetBaseVocation: 3
[action] voc -> 3
[action] baseVocation -> 3
[fromlib] fromlib -> 268541252

i also used the same function on data/lib/common.lua
Code:
local function getBaseVocation(cid)
    local voc = getPlayerVocation(cid)
    print("[data/lib/common.lua] voc: " .. voc)
    if isInArray({1, 4, 5, 6, 7}, voc) then
        return 1
    elseif isInArray({2, 8, 9, 10}, voc) then
        return 2
    elseif isInArray({3, 11, 12, 13, 14, 15, 16, 17}, voc) then
        return 3
    elseif voc == 0 then
        return 0
    end
end
 
i tried this:
Code:
local function ACTIONgetBaseVocation(cid)
    local voc = getPlayerVocation(cid)
    print("2 ACTIONgetBaseVocation: " .. voc)
    if isInArray({1, 4, 5, 6, 7}, voc) then
        return 1
    elseif isInArray({2, 8, 9, 10}, voc) then
        return 2
    elseif isInArray({3, 11, 12, 13, 14, 15, 16, 17}, voc) then
        return 3
    elseif voc == 0 then
        return 0
    end
end

function onUse(cid, item, frompos, item2, topos)
    local vocation = getPlayerVocation(cid)
    local base_vocation = ACTIONgetBaseVocation(cid)
    print("[action] voc -> " .. vocation)
    print("[action] baseVocation -> " .. base_vocation)
    local fromlib = getBaseVocation(cid)
    print("[fromlib] fromlib -> " .. fromlib)
    return true
end

it prints:
Code:
2 ACTIONgetBaseVocation: 3
[action] voc -> 3
[action] baseVocation -> 3
[fromlib] fromlib -> 268541252

i also used the same function on data/lib/common.lua
Code:
local function getBaseVocation(cid)
    local voc = getPlayerVocation(cid)
    print("[data/lib/common.lua] voc: " .. voc)
    if isInArray({1, 4, 5, 6, 7}, voc) then
        return 1
    elseif isInArray({2, 8, 9, 10}, voc) then
        return 2
    elseif isInArray({3, 11, 12, 13, 14, 15, 16, 17}, voc) then
        return 3
    elseif voc == 0 then
        return 0
    end
end
Sent you a pm with my discord info.

I feel like this won't be resolved easily, unless I can help you directly over a voicecall.

--
But still, this is my best guess
Assuming it works correctly, you might have another function somewhere, that is identically named, and is over-riding your newer function.
 
Last edited:
Solution
Back
Top