• 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 A Table and a Function: My Spiral Down to Madness

Aelu

root@Aelu:~#
Joined
Sep 23, 2015
Messages
95
Solutions
1
Reaction score
31
Location
127.0.0.1
Hey OTLand, so I have this small script snippet here:
Lua:
    Mining =
{
--Foraged Minerals
    [1] = {Item = "Quartz", Price = 25},
    [2] = {Item = "Earth Crystal", Price = 50},
    [3] = {Item = "Frozen Tear", Price = 75},
    [4] = {Item = "Fire Quartz", Price = 100},
--Gems
    [5] = {Item = "Emerald", Price = 250},
    [6] = {Item = "Aquamarine", Price = 180},
    [7] = {Item = "Ruby", Price = 250},
    [8] = {Item = "Amethyst", Price = 100},
    [9] = {Item = "Topaz", Price = 80},
    [10] = {Item = "Jade", Price = 200},
    [11] = {Item = "Diamond", Price = 750},
    [12] = {Item = "Prismatic Shard", Price = 2000}
}

What I am attempting to do is something like this:
Lua:
function GetItemInfo(i, p)
  local i = Mining[x].Item
  local p = Mining[x].Price
  print(i, p)
end

But I would like to be able to recall that..
Code:
x = The index value of the given table above (Mining)
i = "Item" in the index "x"
p = "Price" in the index "x

So, in essence, I'm trying to make the function "GetItemInfo()" be able to call both the item name of a given value in the table, as well as the price, so that if GetItemInfo(x) where x=1 the output would be something like..
Code:
Quartz   15

All in all, I'm attempting to index particular values from this table, so I'm able to recall the item's name OR the item's price by telling GetItemInfo(x) to search for the values of "Item" and "Price" within the table "Mining" at value "x".
Lua:
function GetItemInfo(i, p)
  --Some code--
  print(i) --Prints "Quartz"
  or
  print(p) --Prints "15"

I haven't used Lua in a long ass time, and have been learning Pascal, so this is confusing me.

Thanks,
-Aelu
 
What are you gonna do exactly?
If you wanna get any single item from the array the table call: Mining[x].Item/Price is just good.

//
For the function you can just return an array i guess.

Lua:
function getItemData(x)
  return Mining[x]
end
 
Last edited:
if you trying to access the table you need to call for an index if you already know the value or maybe just print all values with some loop

Lua:
function GetItemInfo(index)
    local Mining =
    {
    --Foraged Minerals
        [1] = {Item = "Quartz", Price = 25},
        [2] = {Item = "Earth Crystal", Price = 50},
        [3] = {Item = "Frozen Tear", Price = 75},
        [4] = {Item = "Fire Quartz", Price = 100},
    --Gems
        [5] = {Item = "Emerald", Price = 250},
        [6] = {Item = "Aquamarine", Price = 180},
        [7] = {Item = "Ruby", Price = 250},
        [8] = {Item = "Amethyst", Price = 100},
        [9] = {Item = "Topaz", Price = 80},
        [10] = {Item = "Jade", Price = 200},
        [11] = {Item = "Diamond", Price = 750},
        [12] = {Item = "Prismatic Shard", Price = 2000}
    }
  local i = Mining[index].Item
  local p = Mining[index].Price
  print(i, p)
end
print(GetItemInfo(1))
--Quartz 25
 
if you trying to access the table you need to call for an index if you already know the value or maybe just print all values with some loop

Lua:
function GetItemInfo(index)
    local Mining =
    {
    --Foraged Minerals
        [1] = {Item = "Quartz", Price = 25},
        [2] = {Item = "Earth Crystal", Price = 50},
        [3] = {Item = "Frozen Tear", Price = 75},
        [4] = {Item = "Fire Quartz", Price = 100},
    --Gems
        [5] = {Item = "Emerald", Price = 250},
        [6] = {Item = "Aquamarine", Price = 180},
        [7] = {Item = "Ruby", Price = 250},
        [8] = {Item = "Amethyst", Price = 100},
        [9] = {Item = "Topaz", Price = 80},
        [10] = {Item = "Jade", Price = 200},
        [11] = {Item = "Diamond", Price = 750},
        [12] = {Item = "Prismatic Shard", Price = 2000}
    }
  local i = Mining[index].Item
  local p = Mining[index].Price
  print(i, p)
end
print(GetItemInfo(1))
--Quartz 25

That's more like what I'm trying to do. However, I'm also wanting the ability to call only the name of the item or only the price of the item.
something like:
Lua:
print(GetItemInfo(1.Item))
but that causes the error:
Code:
input:23: ')' expected near 'Item'
Obviously I could rewrite that into another function altogether, but it'd just be much simpler to be able to call it within the main GetItemInfo(x).
 
That's more like what I'm trying to do. However, I'm also wanting the ability to call only the name of the item or only the price of the item.
something like:
Lua:
print(GetItemInfo(1.Item))
but that causes the error:
Code:
input:23: ')' expected near 'Item'
Obviously I could rewrite that into another function altogether, but it'd just be much simpler to be able to call it within the main GetItemInfo(x).
Lua:
    Mining =
{    --[itemid] = {Item = "item_name", Price = numeric_value}
--Foraged Minerals
    [1111] = {Item = "Quartz", Price = 25},
    [1112] = {Item = "Earth Crystal", Price = 50},
    [1113] = {Item = "Frozen Tear", Price = 75},
    [1114] = {Item = "Fire Quartz", Price = 100},
--Gems
    [1115] = {Item = "Emerald", Price = 250},
    [1116] = {Item = "Aquamarine", Price = 180},
    [1117] = {Item = "Ruby", Price = 250},
    [1118] = {Item = "Amethyst", Price = 100},
    [1119] = {Item = "Topaz", Price = 80},
    [1120] = {Item = "Jade", Price = 200},
    [1121] = {Item = "Diamond", Price = 750},
    [1122] = {Item = "Prismatic Shard", Price = 2000}
}

function getItemMiningInfo(itemid)
    if Mining[itemid] then
        return Mining[itemid].Item, Mining[itemid].Price
    end
    return false, false
end
Lua:
local i, p = getItemMiningInfo(1111)
print(i)
print(p)

i, p = getItemMiningInfo(1112)
print(i)
print(p)

i, p = getItemMiningInfo(111111111)
print(i)
print(p)
HKi0RHn.png

Lua:
function onUse(cid, item, fromPosition, itemEx, toPosition)
    local i, p = getItemMiningInfo(item.itemid)
    print(i)
    print(p)
    return true
end
 
Lua:
 Mining =
{    --[itemid] = {Item = "item_name", Price = numeric_value}
--Foraged Minerals
    [1] = {Item = "Quartz", Price = 25},
    [2] = {Item = "Earth Crystal", Price = 50},
    [3] = {Item = "Frozen Tear", Price = 75},
    [4] = {Item = "Fire Quartz", Price = 100},
--Gems
    [5] = {Item = "Emerald", Price = 250},
    [6] = {Item = "Aquamarine", Price = 180},
    [7] = {Item = "Ruby", Price = 250},
    [8] = {Item = "Amethyst", Price = 100},
    [9] = {Item = "Topaz", Price = 80},
    [10] = {Item = "Jade", Price = 200},
    [11] = {Item = "Diamond", Price = 750},
    [12] = {Item = "Prismatic Shard", Price = 2000}
}
function getItemMiningInfo(itemid)
    if Mining[itemid] then
        return Mining[itemid].Item, Mining[itemid].Price
    end
    return false, false
end
local i, p = getItemMiningInfo(1)
print("You see a", i, ".", "It is worth", p, "gold.")
You see a Quartz. It is worth 25 gold.

That worked:)
 
Lua:
 Mining =
{    --[itemid] = {Item = "item_name", Price = numeric_value}
--Foraged Minerals
    [1] = {Item = "Quartz", Price = 25},
    [2] = {Item = "Earth Crystal", Price = 50},
    [3] = {Item = "Frozen Tear", Price = 75},
    [4] = {Item = "Fire Quartz", Price = 100},
--Gems
    [5] = {Item = "Emerald", Price = 250},
    [6] = {Item = "Aquamarine", Price = 180},
    [7] = {Item = "Ruby", Price = 250},
    [8] = {Item = "Amethyst", Price = 100},
    [9] = {Item = "Topaz", Price = 80},
    [10] = {Item = "Jade", Price = 200},
    [11] = {Item = "Diamond", Price = 750},
    [12] = {Item = "Prismatic Shard", Price = 2000}
}
function getItemMiningInfo(itemid)
    if Mining[itemid] then
        return Mining[itemid].Item, Mining[itemid].Price
    end
    return false, false
end
local i, p = getItemMiningInfo(1)
print("You see a", i, ".", "It is worth", p, "gold.")


That worked:)
The main reason for itemid is so that you have a point of reference to index the table with.

If you aren't using an itemid in your script, you'd need to provide more information on what the script is trying/meant to do and maybe we could point you to an alternative option.
 
Back
Top