• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

Solved Which solution is better?

whitevo

Feeling good, thats what I do.
Joined
Jan 2, 2015
Messages
3,454
Solutions
1
Reaction score
627
Location
Estonia
Code:
function onThink()
local bonus = player:getData()
player:addMana(bonus)
end

OR

Code:
function onThink()
local bonus = table[cid]
player:addMana(bonus)
end

Which is is better to use?
a table which takes a little memory. or every time take the information out of metatable?
What im asking is, for performance is its easier to get information from tables or trough functions?
These above codes are simplified versions.
What I'm thinking between is to either create a table with appropriate information and let the creaturescript use the table the execute effects.

or every time onthink interval executes it generates the correct data with trough different functions.
 
I don't know what you are trying to do, but the things you listed work in diffrent ways.
Also note that onThink does not have the Player userdata value, it has lastExecuted and one more, can't remember.

Player:getData() could be updated when you ex. gain health and or mana, but an array / table has to be changed before that, unless you store functions in the array.
It really depends on what you wanna do :p

Code:
function Player:getData()
     return self ~= nil and self:getMaxMana() or nil
end

print(player:getData())

local table = {}
table[player:getId()] = player:getMaxMana()

print(table[player:getId()])

Code:
local things = {
    [2160] = "You found a crystal coin.",
}

print(things[item:getId()])

So as you can see above an array is good if you wanna serach huge arrays, or execute ~almost the same code~ with alot of things, ex like the attr script.
Also remember the function isInArray() it's the function you can use to serach for things in an array like, isInArray({2160}, player:getPosition():getTile():getItems()), that will return a boolean value (true or false) if there is a crystal coin on the tile where the player is.

Hope that helped you abit, but keep trying :)
 
not rly, knew all that xD
What I want to know was which is better for performance. (less lag, less pressure on CPU) this stuff xD
changing tables if needed and using the table every second.
or executing series of functions to get the same result every second.
 
You shouldn't have to worry about performance while doing minimal tasks. One method might be slower than the other, but it's a matter of nanoseconds, so it shouldn't matter that much.

Also note that onThink does not have the Player userdata value, it has lastExecuted and one more, can't remember.
onThink is available for creatures as well, and Creature userdata object is passed as argument.
 
You shouldn't have to worry about performance while doing minimal tasks. One method might be slower than the other, but it's a matter of nanoseconds, so it shouldn't matter that much.
Ah ok ty.
Just though its gona issue because was gona make yet another onThink function which will execute around 10 functions every second and it happens on every player in game.
(however this time these values rarely change)
I know I should learn c++ instead and write the missing functions in TFS, but don't feel like spending week on that yet xD
 
You shouldn't have to worry about performance while doing minimal tasks. One method might be slower than the other, but it's a matter of nanoseconds, so it shouldn't matter that much.


onThink is available for creatures as well, and Creature userdata object is passed as argument.

Oh just now noticed that I tought of onInterval xD, I should have noticed that when I wrote lastExecution and I know remember the other parameter is interval?

Ah ok ty.
Just though its gona issue because was gona make yet another onThink function which will execute around 10 functions every second and it happens on every player in game.
(however this time these values rarely change)
I know I should learn c++ instead and write the missing functions in TFS, but don't feel like spending week on that yet xD

Id start with Lua, you will learn alot by it.
C++ is alot harder to start with, you could be "good" within a few days with Lua, C++ ... well there is alot more going on and you have to think a diffrent way when coding.
But as Ninja said, if you are doing smaller things it does not matter.
Hard to say without knowing what you wanna do, for me I do diffrent things depending on the lenght of the code im going to execute, if it's short I might not even use an array, if it's really long ill loop the array.
There is no "always do this", if you are looking for performance.
 
Code:
function onThink()
local bonus = player:getData()
player:addMana(bonus)
end

OR

Code:
function onThink()
local bonus = table[cid]
player:addMana(bonus)
end

Which is is better to use?
a table which takes a little memory. or every time take the information out of metatable?
What im asking is, for performance is its easier to get information from tables or trough functions?
These above codes are simplified versions.
What I'm thinking between is to either create a table with appropriate information and let the creaturescript use the table the execute effects.

or every time onthink interval executes it generates the correct data with trough different functions.

If you are really concered about the perfomance, you shouldn't use Lua at all to do that.

But its faster to get information from tables.
 
Seems like using table is better option. (besides the fact that doing complicated scripts should be done in source itself xD)
I though same that table may be better, however these functions seem to be kinda tricky things and very fast. So can't but my finger it on it which would have been better.
Give us a detailed note on what you try to accomplish, then we could maybe tell you a better way to achieve it.
I kinda did, but I can give more detailed example. (still a pseudo code)
Code1
Code:
function executeEvent()
    player:registerEvent("onthink script")
    addEvent(eventRemove, 60000, cid, "onthink script")
end

function onThink()
local variable1 = player:getData(1)
local variable2 = player:getData(2)
local variable3 = player:getData(3)
local variable4 = player:getData(4)
local variable5 = player:getData(5)
  
    doSomethingWithVariable1()
    doSomethingWithVariable2()
    doSomethingWithVariable3()
    doSomethingWithVariable4()
    doSomethingWithVariable5()
end

function Creature:getData(int)
    if int == 1 then
        something = function1()
        something = function2()
        something = function3()
        something = function4()
    elseif int == 2 then
        something = function2()
        something = function5()
        something = function7()
        something = function8()
    elseif int == 3 then
        something = function3()
        something = function5()
        something = function7()
        something = function1()
    elseif int == 4 then
        something = function4()
        something = function3()
        something = function2()
        something = function6()
    elseif int == 5 then
        something = function8()
        something = function2()
        something = function5()
        something = function1()
    end
return something
end
Code2
Code:
function executeEvent()
    player:registerEvent("onthink script")
    addEvent(eventRemove, 60000, cid, "onthink script")
    onThinkTable[cid] = {
        variable1 = player:getData(1)
        variable2 = player:getData(2)
        variable3 = player:getData(3)
        variable4 = player:getData(4)
        variable5 = player:getData(5)
    }
end

function onThink()
local variable1 = onThinkTable[cid].variable1
local variable2 = onThinkTable[cid].variable2
local variable3 = onThinkTable[cid].variable3
local variable4 = onThinkTable[cid].variable4
local variable5 = onThinkTable[cid].variable5
  
    doSomethingWithVariable1()
    doSomethingWithVariable2()
    doSomethingWithVariable3()
    doSomethingWithVariable4()
    doSomethingWithVariable5()
end

function Creature:getData(int)
    if int == 1 then
        something = function1()
        something = function2()
        something = function3()
        something = function4()
    elseif int == 2 then
        something = function2()
        something = function5()
        something = function7()
        something = function8()
    elseif int == 3 then
        something = function3()
        something = function5()
        something = function7()
        something = function1()
    elseif int == 4 then
        something = function4()
        something = function3()
        something = function2()
        something = function6()
    elseif int == 5 then
        something = function8()
        something = function2()
        something = function5()
        something = function1()
    end
return something
end
 
Back
Top