• Finding Table Index
    10 replies, posted
I didn't really know any other way to word the title, but I am making a shop. The shop uses a table to store all of the items, here is an example: [lua] SHOP.Items = {} SHOP.Items["trail_test"] = { Name = "Test Trail", Desc = "Blah", Action = function(ply, item) util.SpriteTrail(ply, 0, Color(255,0,0), false, 15, 1, 4, 1/(15+1)*0.5, "trails/plasma.vmt") end, }[/lua] And I have a concommand to buy an item, which is here: [lua] function SHOP_BuyItem(ply, cmd, args) local cash = ply:GetNWInt("Cash") local Item = args[1] //local price = SHOP.Items[item].Price if (SHOP.Items[Item]) then //if cash >= price then if (SHOP.Items[Item].Action != "nil") then if (SHOP.Items[Item].Action(ply, item)) then SHOP.Items[Item].Action(ply, item) end end //end end end concommand.Add("dra_buy_item", SHOP_BuyItem)[/lua] And now, in the menu I have it add a simple button for each item in the table for testing. Here is the code for that: [lua] for k, item in pairs(SHOP.Items) do local ItemUse = vgui.Create("DButton", ItemPanel) ItemUse:SetSize(100, 25) ItemUse:SetPos(0, 30) ItemUse:SetText(tostring(item.Desc)) ItemUse.DoClick = function() RunConsoleCommand("dra_buy_item", ***) end end[/lua] Now, if you look back at the code I use for buying the item, args[1] is SHOP.Items[args[1]]. How can I find that, and put it for the first argument for when I run dra_buy_item? Sorry if it's a bit confusing, feel free to ask any question.
Give all products a unique number then have a clientside and serverside table, then to cut down on memory used you just have to run something like dra_buy_item 1 or 2 or 3 and so on. So add UID = 1 and so on to each item then just compare to a table on the serverside.
Could you elaborate some more for me, please? Sorry, I never really understood tables. I added a "ID = 1", to the "testing_trail" item. Then looped through the SHOP.Items table, and checked if an item's ID was equal to args[1]. Here: [lua]function SHOP_BuyItem(ply, cmd, args) local item = args[1] for k, v in pairs(SHOP.Items) do if (v.ID == item) then if (SHOP.Items[item]) then if (SHOP.Items[item].Action != "nil") then if (SHOP.Items[item].Action(ply, item)) then SHOP.Items[item].Action(ply, item) end end end end end end concommand.Add("dra_buy_item", SHOP_BuyItem)[/lua]
Yea, that is exactly what you need to do. Tell me if you get any problems.
[QUOTE=Science;29761830]cut down on memory used[/QUOTE] :whoptc:
[QUOTE=Science;29762143]Yea, that is exactly what you need to do. Tell me if you get any problems.[/QUOTE] Well, I get one problem: It doesn't work. I change the RunConsoleCommand's first argument to: [lua]RunConsoleCommand("dra_buy_item", item.ID)[/lua] Is that incorrect?
Why have you used ID you are indexing the table with the item name so just send that through the console command and just do SHOP.Items[arg[1]] to find the table and use likes 90% less loops
[QUOTE=King Flawless;29762246]Why have you used ID you are indexing the table with the item name so just send that through the console command and just do SHOP.Items[arg[1]] to find the table and use likes 90% less loops[/QUOTE] that was my first question, must've been the way I worded it. In the RunConsoleCommand part, how can I get the item's name for the second argument?
[QUOTE=Science;29761830]cut down on memory used[/QUOTE] Actually it uses more as you would have the clean name [b]and[/b] the ID instead of just the clean name. RunConsoleCommand("dra_buy_item", k)
[QUOTE=FlapadarV2;29762328]Actually it uses more as you would have the clean name [b]and[/b] the ID instead of just the clean name. RunConsoleCommand("dra_buy_item", k)[/QUOTE] Wow, can't believe I never tried that. Thanks, FlapadarV2. Also, thanks Science for the attempted help.
[QUOTE=wakeboarderCWB;29762417]Also, thanks Science for the attempted help.[/QUOTE] His attempt to help just confused you it wasn't helpful he shouldn't be posted in this section until he can make stuff him self without the request section
Sorry, you need to Log In to post a reply to this thread.