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

RevScripts Is there any difference?

T

Tibia Demon

Guest
Is there any difference in performance if I have my script tabled like this
Lua:
local items = {
    [5903] = {text = "Summoned", effect = CONST_ME_HOLYAREA, effectTwo = CONST_ME_HITBYPOISON, textTwo = "Hell", monsterx = "orshabaal", money = 8000},
    [4851] = {text = "Summoned", effect = CONST_ME_HOLYAREA, effectTwo = CONST_ME_CAKE, textTwo = "fire", monsterx = "Demon", money = 5000},
    [2062] = {text = "Summoned", effect = CONST_ME_HOLYAREA, effectTwo = CONST_ME_SOUND_RED, textTwo = "dragon", monsterx = "dragon lord", money = 1500},
    [2073] = {text = "Summoned", effect = CONST_ME_HOLYAREA, effectTwo = CONST_ME_MORTAREA, textTwo = "boss", monsterx = "morgaroth", money = 3000},
    [7487] = {text = "Summoned", effect = CONST_ME_HOLYAREA, effectTwo = CONST_ME_HITBYFIRE, textTwo = "dragon", monsterx = "dragon", money = 2500},
    [5791] = {text = "Summoned", effect = CONST_ME_HOLYAREA, effectTwo = CONST_ME_EXPLOSIONHIT, textTwo = "cyclops!!", monsterx = "cyclops", money = 1000}
}

local summonItemx = Action()

function summonItemx.onUse(player, item, fromPosition, target, toPosition, isHotkey)
    local summonItemx = items[item.itemid]
    local SummonItem = items[item.itemid]
    if SummonItem and player:removeTotalMoney(SummonItem.money) then
        player:say(SummonItem.text, TALKTYPE_MONSTER_SAY)
        player:getPosition():sendMagicEffect(SummonItem.effect)
        Game.createMonster(SummonItem.monsterx, player:getPosition())
    else
        player:getPosition():sendMagicEffect(SummonItem.effectTwo)
        player:say(SummonItem.textTwo, TALKTYPE_MONSTER_SAY)
    end
    return true
end

for k, v in pairs(items) do
    summonItemx:id(k)
end
summonItemx:register()
or having it in separated Lua files like this
Lua:
local orshabaal = Action()
local money = 1000

function orshabaal.onUse(player, item, fromPos, target, toPos, isHotkey)
    if player:removeTotalMoney(money) then
    Game.createMonster("orshabaal", player:getPosition())
    player:getPosition():sendMagicEffect(CONST_ME_HOLYAREA)
    else
    player:say('hell', TALKTYPE_MONSTER_SAY)
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    end
    return true
end

orshabaal:id(6546)
orshabaal:register()
Lua:
local demon = Action()
local money = 100

function demon.onUse(player, item, fromPos, target, toPos, isHotkey)
    if player:removeTotalMoney(money) then
    Game.createMonster("demon", player:getPosition())
    player:getPosition():sendMagicEffect(CONST_ME_HOLYAREA)
    else
    player:say('demon summoned', TALKTYPE_MONSTER_SAY)
    player:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
    end
    return true
end

demon:id(6545)
demon:register()
Will there be any noticeable performance difference?
 
Solution
I would aim for clean code than trying to gain any to zero perfomance gain in your case. We are talking about nano seconds. Only try to optimize your code if it's really a bottleneck. I would stick with the table and that's my opinion.

Also in your case that loop is executed once at startup. Then when script is accessing the hash table values by key is mostly O(1) operation.

Maintenance is key, rather than have 100 separate files
I would aim for clean code than trying to gain any to zero perfomance gain in your case. We are talking about nano seconds. Only try to optimize your code if it's really a bottleneck. I would stick with the table and that's my opinion.

Also in your case that loop is executed once at startup. Then when script is accessing the hash table values by key is mostly O(1) operation.

Maintenance is key, rather than have 100 separate files
 
Last edited:
Solution
negligible difference in performance.

But let's say you had 100 summon-able monsters.. having them in 100 separate files is just extremely cumbersome, and would take more ram.

First script example is best use-case.
 
Always do in one script when you have a scenario you can standardize (like opening doors or eating food for example). It takes less hdd space, less memory (because you declare less functions) and is easier to edit in case you want to make a small change.

The difference in performance can be small, but if you load 500 poorly written scripts, it may become noticeable.

Imagine this: you have 100 scripts doing same thing and you found a small bug. Now you have to open 100 files and fix the code.

The alternative is: you have one script. You spent 5 minutes fixing your code.

Same thing applies to global variables tfs uses. You have 300 lua files that send a green message to player. You entered the message type as number (22) instead of a variable (MESSAGE_INFO_DESCR). You want to update your server. The new client has different ids for message types. Now all your scripts send wrong message type or worse - crash the client. Your options are now:

  • overwriting the function (pretty bad idea because it could mess your reload if done improperly)
  • source edits to redirect the message types
  • fixing every script manually (very likely what you'd do if you really want to save the datapack)
  • throwing away the datapack and starting over (very likely what most ot owners would do)
 
Thank you all.
I will try to merge the possible ones to tables instead of having many files.
I didn't want to do it because I was lazy but it will make things easier for me later.

Seems like it will not affect on performance side now because my server still only has like 58 custom scripts but I am still on the early stages so the scripts number will increase later and may have noticeable performance delays.
and again all replies are great and helpful so I am confused who to give solution to but I will do it to first reply so thread gets closed.
 
Thank you all.
I will try to merge the possible ones to tables instead of having many files.
I didn't want to do it because I was lazy but it will make things easier for me later.

Seems like it will not affect on performance side now because my server still only has like 58 custom scripts but I am still on the early stages so the scripts number will increase later and may have noticeable performance delays.
and again all replies are great and helpful so I am confused who to give solution to but I will do it to first reply so thread gets closed.
For future;

You can give the solution to whomever you want to, however there are some general 'loose guidelines' that are just best practice.

-> In 99% of cases you typically give the best answer to the first post of which answered the original question of your thread.
-> It's generally frowned upon to give yourself the best answer.
-> In some rarer cases, where someone has answered your immediate question, but someone else posts an alternative and better solution, which may or may not have answered the initial question, but rather gave you a better solution/answer to your problem, you'd want to give that person the solution instead.
 
Back
Top