Gamemode Scripting - Getting data from a list/table
7 replies, posted
Hello Devs,
I apologize for the wall of text in advance, but I want to be as thorough as possible. I've been a longtime lurker on Facepunch since roughly 2009. For all things Garry's Mod, I feel this is one of the best places to go. Because of this, the other day I finally set out to register for myself so I could ask a question that I haven't quite found the solution to as of yet.
Currently, I'm writing a buy menu for a gamemode that I've been working on and modifying. Using Derma UI features, I've had no trouble creating a menu that functions relatively well, thanks to the Garry's Mod wiki, forum posts from this very site, and a handful of tutorials. The majority of my programming experience resides in C and C++, as well as Java, although C++ is definitely my strong suit. As such, I am not entirely familiar with all Lua conventions. Although I've been dabbling in it for well over a year or so, this is the first time where I am attempting to gather a key from a list (or at least I am fairly certain it is a key,) allow me to provide some context.
This here is the code executed when the vehicles category is clicked in the buy menu:
vehiclesButton.DoClick = function(vehiclesButton)
LocalPlayer():EmitSound(ButtonNoise)
local vehiclesPanel = Menu:Add("VehiclesPanel")
local iconList = vgui.Create("DIconLayout", vehiclesPanel)
iconList:SetPos(10,5)
iconList:SetSize(vehiclesPanel:GetWide(), vehiclesPanel:GetTall())
iconList:SetSpaceY(10)
iconList:SetSpaceX(10)
local entityArray = {}
entityArray[1] = scripted_ents.Get("lunasflightschool_rebelheli")
entityArray[2] = scripted_ents.Get("lunasflightschool_combineheli")
entityArray[3] = scripted_ents.Get("lfs_crysis_vtol")
for k, v in pairs(entityArray) do
local icon = vgui.Create("SpawnIcon", iconList)
icon:SetModel(v["Model"])
icon:SetToolTip(v["PrintName"])
iconList:Add(icon)
icon.DoClick = function(icon)
LocalPlayer():EmitSound(BuyNoise)
LocalPlayer():ConCommand("ctf_buyentity "..v["ClassName"])
end
end
One the user accesses the vehicles tab, a local array "entityArray" is populated with entities I wish to add to the menu using scripted_ents.Get() that are supported and installed by the server via addons. The local DIConLayout, "iconList" then grabs some basic information about them to make very general spawn icons inside the menu. Right now, the icon:SetModel(v["Model"]) function for the iconList does not render the icon with the entity's model correctly for these vehicles, but I suspect that is due to the way the shared.lua file is defined for them, rather than a flaw in my code, as it works for other entities with models that are set in the conventional way.
All good so far. The "ctf_buyentity" console command is then called when the player clicks on one of the icons, which is planned to subtract from the player's balance, as a buy menu should. The "Classname" used in the ConCommand argument concatenation is assigned to the ent:GetClass() call in the buyEntity command. While this implementation works well for standard entities, I have not been able to get it to work for two specific addons:
Simfphys Base & Simfphys Armed Vehicles (Public Github links)
From my understanding, using the scripted_ents.Get() call does not work for these vehicles because each one uses the same entity, "gmod_sent_vehicle_fphysics_base" because each vehicle's attributes are applied using data in a table/list. Doing some digging in the Simfphys Base, I found the SpawnSimfphysVehicle() console command that I can call for each icon, depending on the specific vehicle, as seen above. My only drawback is how to populate my menu with the actual vehicle entity data. Is there something simple I am missing?
I would greatly appreciate any insights or ideas that anyone may have for my issue. Thanks.
I don’t really know what specifically is inside this data folder (mainly how it’s formatted), but I found this after searching doing a search with the keyword “file”.
simfphys_base/simfphysduplicator.lua at a6e41950947f167cb1cd7e08..
Use this to grab all the data files then use
simfphys_base/simfphysduplicator.lua at a6e41950947f167cb1cd7e08..
to actually read/parse the data.
Hello ButterKing5000,
I'll see about working with some of the information you've provided here when I get home this afternoon. I suppose I should've provided the link to my gamemode's own GitHub, which I will do now.
CaptureTheFlag-Redux by TricycleRaptor (GitHub)
Thanks for the reply!
After a followup, I got the menu to populate the list of vehicles. However, I have not been able to determine how to get the specific data, even still.
I have successfully populated the menu, and am now attempting to use the ctf_simphys_buyvehicle command when a vehicle is clicked from the menu, which is a near identical copy of SpawnSimfphysVehicle(). Unfortunately, once the user has clicked on the vehicle of their choice from the menu, the console says that a nil value was passed for concatenation. Here is the code:
local vehicleList = list.Get( "simfphys_vehicles" )
local vehicle = vehicleList[vname]
for k, v in pairs(vehicleList) do
local printname = v
local icon = vgui.Create("SpawnIcon", iconList)
icon:SetModel(v["Model"])
icon:SetToolTip(v["Name"])
iconList:Add(icon)
icon.DoClick = function(icon)
LocalPlayer():EmitSound(BuyNoise)
LocalPlayer():ConCommand("ctf_simfphys_buyvehicle "..v[vehicle])
end end
The console command specifically takes the second value here where "sim_fphys_combineapc_armed" is the information I need to get from the vehicleList variable.
Any thoughts? Thanks.
Can you confirm that the vehicleList in your code is actually the correct info and not an empty table for example? You may also want to ensure vname contains the correct data if you haven’t already.
Also, what issues did you have with the previous method I linked?
Aye, the vehicleList data is the correct information, or at least I am 90% sure that is the case. I do not fully understand the Simfphys implementation yet, as using it to populate my menu did get some of their information like their model and names. See here:
https://files.facepunch.com/forum/upload/456852/fe31a851-d72c-4cc4-b3f6-25d981df1acf/20190410231834_1.jpg
I grayed out the first three icons because they are entities from other addons and work as normal. The rest of the vehicles are from the "simfphys_vehicles" list I am looking to get the additional information from. I am not sure if vname is the correct data, but looking here tells me that might be the case. Using the SpawnSimfphysVehicle() command in console with the name, such as "sim_fphys_combineapc_armed" spawns the Combine APC as it should.
With your original suggestion, I found that the tool information wasn't turning up much help for my issue. If there is something I missed, however, please let me know. It's driving me crazy how close this is to being complete, as it is the last major feature of the gamemode.
Is the concommand part of your code setting off the concatenation error? I believe you probably are trying to get the name incorrectly. Assuming the data is organized by a string key, you’ll likely be using the variable k to retrieve the “vname” (really it’s ID). (Probably unnecessary here, but v.Name is likely its pretty name if you need that as well).
LocalPlayer():ConCommand("ctf_simfphys_buyvehicle "..k)
Oh my god,
I can't believe I hadn't thought of that, using the k in place of the v. The menu now works as intended. Thanks so much for the input, it was just the nudge I needed.
Sorry, you need to Log In to post a reply to this thread.