• 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 Possible to store functions in an array?

Ahilphino

Excellent OT User
Joined
Jun 5, 2013
Messages
1,667
Solutions
1
Reaction score
733
Code:
local reward = {
        [1] = cid:setMaxHealth(getCreatureMaxHealth(cid)+200),
        [2] = cid:setMaxMana(getCreatureMaxMana(cid)+200)
}

Obviously this isn't working, I was wondering is it possible? I've tried googling etc with no succesful answer :(
 
Code:
local reward = {
[1] = cid:setMaxHealth(getCreatureMaxHealth(cid)+200),
[2] = cid:setMaxMana(getCreatureMaxMana(cid)+200)
}

function onUse(cid, item, fromPosition, itemEX, toPosition)
            local plusone = getPlayerStorageValue(cid, 30011)
            setPlayerStorageValue(cid, 30011, plusone+1)             
   return true
end

thanks gajs :)
 
Last edited:
Code:
local reward = {
[1] = cid:setMaxHealth(getCreatureMaxHealth(cid)+200),
[2] = cid:setMaxMana(getCreatureMaxMana(cid)+200)
}

function onUse(cid, item, fromPosition, itemEX, toPosition)
            local plusone = getPlayerStorageValue(cid, 30011)
            setPlayerStorageValue(cid, 30011, plusone+1)             
   return true
end

Script is obviously not done yet.. but basically I want it to players to get a bonus depending on what their storagevalue at 30011 is. So if it's 1, they get +200 hp, if their value is 2, they get +200 mana and so on etc. There's going to be ALOT of these so I figured using an array would be best but I could also make a bunch of elseifs.

I never used LUA before but I know it's possible to store things like cid:setMaxHealth(getCreatureMaxHealth(cid)+200)
I just don't know how and how to use them later.

I'm new so sorry if I explained it bad. This the only thing I would like to get help with, the rest I can figure out myself :)

Uh.... I don't think you can do functions in an array like that. but you could make a function that adds all the stuff together, for example: (I don't use TFS 1.0, so go fuck yourself if you complain, this is an example and you can redo it for your server)

Code:
local reward = {
[1] = {health = 200}, 
[2] = {mana = 200},
[3] = {mana = 200, health = 0, speed = 20, sausages = 1, whatever = 10}, -- Add whatever you want here, it really doesn't matter, you could do anything like this table
}

--Then you make a function that adds up the above table values
function onUse(cid, item, fromPosition, itemEX, toPosition)
            local plusone = getPlayerStorageValue(cid, 30011)+1
            setPlayerStorageValue(cid, 30011, plusone)            
            if reward[plusone].mana then
                    setCreatureMaxMana(cid, reward[plusone].mana)
            end
            if reward[plusone].heatlh then
                    setCreatureMaxHealth(cid, reward[plusone].health)
            end
   return true
end

--There may be easier ways to do this, but I just woke up, and the above would work.
 
Code:
local reward = {
    [1] = {health = 200, mana = 200},
    [2] = {health = 500, mana = 500}
}

function onUse(cid, item, fromPosition, itemEX, toPosition)
    local player = Player(cid)

    local storage = player:getStorageValue(30011)
    local targetReward = reward[storage]
    if not targetReward then
        return true
    end

    player:setStorageValue(30011, math.max(1, storage) + 1)
    if targetReward.health then
        player:setMaxHealth(targetReward.health)
        player:addHealth(player:getMaxHealth())
    end

    if targetReward.mana then
        player:setMaxMana(targetReward.mana)
        player:addMana(player:getMaxMana())
    end

    return true
end
 
You can easily store functions in tables, but what you did was calling the function, so even if it had worked you would only store the return value of that call in the table.

Code:
local rewards = {
  [1] = function(player) player:setMaxHealth(player:getMaxHealth() + 100) end,
  [2] = function(player) player:setMaxMana(player:getMaxMana() + 100) end
}

function onUse(cid, item, fromPosition, itemEX, toPosition)
  local player = Player(cid)
  local reward = rewards[player:getStorageValue(30011)]
  if reward then
    reward(player)
  end

  return true
end
 
Code:
local rewards = {
[1] = {"doPlayerAddHealth", cid, 100}
}

pcall(loadstring(rewards[1][1] .. "(" .. rewards[1][2] .. ", " .. rewards[1][3] .. ")"))
 
You can easily store functions in tables, but what you did was calling the function, so even if it had worked you would only store the return value of that call in the table.

Code:
local rewards = {
  [1] = function(player) player:setMaxHealth(player:getMaxHealth() + 100) end,
  [2] = function(player) player:setMaxMana(player:getMaxMana() + 100) end
}

function onUse(cid, item, fromPosition, itemEX, toPosition)
  local player = Player(cid)
  local reward = rewards[player:getStorageValue(30011)]
  if reward then
    reward(player)
  end

  return true
end

thanks i love u this will make it 100x easier :D
 
(I don't use TFS 1.0, so go fuck yourself if you complain, this is an example and you can redo it for your server)

LMFAO!!!!!!!! That's great man, letting it be known before everyone jumps on your case! Lmao! That made my day... :D


You can easily store functions in tables, but what you did was calling the function, so even if it had worked you would only store the return value of that call in the table.

Code:
local rewards = {
  [1] = function(player) player:setMaxHealth(player:getMaxHealth() + 100) end,
  [2] = function(player) player:setMaxMana(player:getMaxMana() + 100) end
}

function onUse(cid, item, fromPosition, itemEX, toPosition)
  local player = Player(cid)
  local reward = rewards[player:getStorageValue(30011)]
  if reward then
    reward(player)
  end

  return true
end

This right here? Genius! This really made my day, I knew you could store functions in tables (Hello, anyone heard of metatables and metamethods?), however I haven't really done it yet other than one function being called from a table through another metatable, anyways. This is great piece of code to learn from.

@Topic Thanks to zbizu and summ for showing us ways of using functions for values in tables
 

Similar threads

Back
Top