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

Display an array element

Blade33711

Member
Joined
Aug 6, 2012
Messages
133
Solutions
1
Reaction score
5
Location
Poland
How to do to display an array element?
Lua:
function onUse(cid, item, frompos, item2, topos)
local items = {["reward"] = {item = 1111, pos = {x=1000, y=1000, z=7}}
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, "".. items[1] ..".")
end
 
Solution
Ty, but it didn't work for:
Lua:
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, items.reward.pos ..".")

Error:
attempt to concatenate field 'pos' (a table value)
because you cant concatenate table values, you have to use a string or number
Lua:
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, ("X: %d, Y: %d, Z: %d"):format(items.reward.pos.x, items.reward.pos.y, items.reward.pos.z))
this gets the x y z values and places them in each %d in order
you can access table elements by using the . operator (only works for string keys) or [] (works for integers & strings)
you're trying to access items[1] when index 1 doesn't exist, i assuming you're trying to access "reward"
stuff like ["reward"] = and reward = is interpreted as a string key, so you can access it with items.reward since they're strings
after which you can get the item id by doing this:
items.reward.item -- would be 1111
you can learn more about this here: Lua Tables
you also dont need to concatenate (see Programming in Lua : 3.4) empty strings to your value, you can simply do: items.reward.item .. "."
the end product would be this:
Lua:
-- define items once every time the script is loaded, instead of having it re-defined every time onUse runs
local items = {
    reward = {item = 1111, pos = {x=1000, y=1000, z=7}
}

function onUse(cid, item, frompos, item2, topos)
    doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, items.reward.item ..".")
    return true
end
 
Ty, but it didn't work for:
Lua:
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, items.reward.pos ..".")

Error:
attempt to concatenate field 'pos' (a table value)
 
Ty, but it didn't work for:
Lua:
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, items.reward.pos ..".")

Error:
attempt to concatenate field 'pos' (a table value)
because you cant concatenate table values, you have to use a string or number
Lua:
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, ("X: %d, Y: %d, Z: %d"):format(items.reward.pos.x, items.reward.pos.y, items.reward.pos.z))
this gets the x y z values and places them in each %d in order
 
Solution
because you cant concatenate table values, you have to use a string or number
Lua:
doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, ("X: %d, Y: %d, Z: %d"):format(items.reward.pos.x, items.reward.pos.y, items.reward.pos.z))
this gets the x y z values and places them in each %d in order
I cri everytime I have to delve into character classes. ;c
 
Ty! One more question. How to display this array?
Lua:
local cfg = {items = {2342, 2354, 2564}}
if you want to just print out the array you can do this
Lua:
for k, v in pairs(cfg) do
    print(k, v)
end
but to print it to the player you'd have to do the same thing but instead of print you'd do doPlayerSendTextMessage, and you'd have to convert tables and shit to strings using tostring
 
Back
Top