• 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 Local variable to config.lua

RatoKing

New Member
Joined
Mar 17, 2021
Messages
11
Solutions
1
Reaction score
2
GitHub
RatoKing
Hello everyone,

I'm trying to set a variable in config.lua and read this variable in other .lua script, I mean, is it possible?

Thanks ;-)
 
Solution
E
yes it is possible, just add a new entry in configmanager.cpp/h and luascript.cpp and recompile your server and you will be able to use configManager.getNumber(configKeys.WHATEVER_YOU_WANT)

Gonna need a bit more info @RatoKing

Server Version, Script that you currently have and/or the specific variable or situation you're trying to create.
 
Oh sorry.

Lemme explain then ;-)


How it works at the moment:

data\scripts\creaturescripts\others\bestiary_kill.lua
Lua:
local bestiaryOnKill = CreatureEvent("BestiaryOnKill")
function bestiaryOnKill.onKill(player, creature, lastHit)
    if not player:isPlayer() or not creature:isMonster() or creature:hasBeenSummoned() or creature:isPlayer() then
        return true
    end

    for cid, damage in pairs(creature:getDamageMap()) do
        local participant = Player(cid)
        if participant and participant:isPlayer() then
            local bestAmount = 5 --how much will increase after kill a monter in bestiary
            for i = bestAmount, 1, -1 do
            participant:addBestiaryKill(creature:getName())
            end
        end
      end

    return true
end
bestiaryOnKill:register()

So, I'd like to change the
Code:
local bestAmount variable
to a variable where I would edit on config.lua.

Server Version, Script that you currently have and/or the specific variable or situation you're trying to create.
Server version: 12.64 (OTSERVBR)
Script: Above
Tfs: 1.3
 
yes it is possible, just add a new entry in configmanager.cpp/h and luascript.cpp and recompile your server and you will be able to use configManager.getNumber(configKeys.WHATEVER_YOU_WANT)

 
Solution
Oh sorry.

Lemme explain then ;-)


How it works at the moment:


Lua:
local bestiaryOnKill = CreatureEvent("BestiaryOnKill")
function bestiaryOnKill.onKill(player, creature, lastHit)
    if not player:isPlayer() or not creature:isMonster() or creature:hasBeenSummoned() or creature:isPlayer() then
        return true
    end

    for cid, damage in pairs(creature:getDamageMap()) do
        local participant = Player(cid)
        if participant and participant:isPlayer() then
            local bestAmount = 5 --how much will increase after kill a monter in bestiary
            for i = bestAmount, 1, -1 do
            participant:addBestiaryKill(creature:getName())
            end
        end
      end

    return true
end
bestiaryOnKill:register()

So, I'd like to change the
Code:
local bestAmount variable
to a variable where I would edit on config.lua.
I see.

config.lua is a poor choice for this in my opinion, though.

I'd suggest creating a new file in
data/lib/core/

Name it whatever you want, an example might be customLibs.lua

Then register that file in core.lua (same directory)

Inside of that file customLibs.lua put your variable without the local.
So in your case, put it like
Lua:
bestAmount = 5

Then you can call it from any script using the variable name.
 
yes it is possible, just add a new entry in configmanager.cpp/h and luascript.cpp and recompile your server and you will be able to use configManager.getNumber(configKeys.WHATEVER_YOU_WANT)


Lua:
local bestiaryOnKill = CreatureEvent("BestiaryOnKill")
function bestiaryOnKill.onKill(player, creature, lastHit)
    if not player:isPlayer() or not creature:isMonster() or creature:hasBeenSummoned() or creature:isPlayer() then
        return true
    end

    for cid, damage in pairs(creature:getDamageMap()) do
        local participant = Player(cid)
        if participant and participant:isPlayer() then
            local bAmount = configManager.getNumber(configKeys.WHATEVER_YOU_WANT) --how much will increase after kill a monter in bestiary
            for i = bAmount, 1, -1 do
            participant:addBestiaryKill(creature:getName())
            end
        end
      end

    return true
end
bestiaryOnKill:register()

The .lua file should be like this?
 
Last edited:
Lua:
local bestiaryOnKill = CreatureEvent("BestiaryOnKill")
function bestiaryOnKill.onKill(player, creature, lastHit)
    if not player:isPlayer() or not creature:isMonster() or creature:hasBeenSummoned() or creature:isPlayer() then
        return true
    end

    for cid, damage in pairs(creature:getDamageMap()) do
        local participant = Player(cid)
        if participant and participant:isPlayer() then
            local bAmount = configManager.getNumber(configKeys.WHATEVER_YOU_WANT) --how much will increase after kill a monter in bestiary
            for i = bAmount, 1, -1 do
            participant:addBestiaryKill(creature:getName())
            end
        end
      end

    return true
end
bestiaryOnKill:register()

The .lua file should be like this?
Looks right.
 
I see.

config.lua is a poor choice for this in my opinion, though.

I'd suggest creating a new file in
data/lib/core/

Name it whatever you want, an example might be customLibs.lua

Then register that file in core.lua (same directory)

Inside of that file customLibs.lua put your variable without the local.
So in your case, put it like
Lua:
bestAmount = 5

Then you can call it from any script using the variable name.

Why do you think it's a bad idea? I mean, could you explain? Thanks.
 
Why do you think it's a bad idea? I mean, could you explain? Thanks.
Needing to modify the source and recompile every time you want to add a new variable is a waste of time, when a much easier alternative is available.

Just my opinion, though.
 
Back
Top