Having trouble grasping returning data from a table with Lua - I originally got it all working by just having
local props = {"model/path1", "model/path2"}
for i=1, table.count(list) do
spawnicon:SetModel(list[i])
end
Now I want to store more information regarding these props so I set the list up like so
local props = { [Metal Fence] = { model="model/path", price = 100} }
I've been doing trial and error for the past hour or so and keep just getting errors..
Your indexes must be a string in this case
local props = { ["Metal Fence"] = { model="model/path", price = 100} }
I was not on my usually PC with my code so I re-wrote it and missed the quotes BUT... I needed to be more specific with my issue..
local Barricades = {
["Metal Fence"] = {model = "models/props_c17/fence01a.mdl", price = 100}
}
This is my code above - what I'm trying to achieve is a simplified spawn menu as 99% of the stuff in the normal menu will not be used. I already am using DGrid and SpawnIcon to lay the blocks out but I can not get it to pull the model from my list no matter what I've tried.. So I just get error blocks. I just do not know how to properly pull data from tables in Lua.
Barricades[i] (where i is a number) will return the i item from the table. You would then proceed as such:
Barricades[i].model
Barricades[i].price
this returns the model and price.
However I would suggest using pairs instead of numerical for loop as its easier to work with unless you realy need numerical loop:
for name, data in pairs(Barricades) do -- name will be for example "Metal Fence" and data will be a table with the keys model and price, access them with data.model and data.price
local spawnicon = vgui.Create("SpawnIcon")
spawnicon:SetSize(spawnIconSize, spawnIconSize)
spawnicon:InvalidateLayout( true )
spawnicon:SetModel(data.model) -- this will give you the model, you can do the same with price later
grid:AddItem(spawnicon)
function spawnicon:DoClick()
end
end
Sorry for not using the code tags for longer code, FP still has not fixed them
Sorry, you need to Log In to post a reply to this thread.