NPC Vehicle Dealer spawn icons

Deleted

it might be worth saying how you want it setout because honestly theres so many ways to do this.

a simple way would be something like:
[lua]
local Vehicles {
{model, name, description, price [etc any info you want in here]},
// do this for every vehicle
};

// loop through vehicles and make the icons

for k,v in pairs(Vehicles) do
local icon = vgui.Create(“SpawnIcon”)
icon:SetToolTip(v.Name … "
" … v.Description);
icon:SetModel(v.Model);
VehicleList:AddItem(icon);
// VehicleList being you DPanelList of vehicles
end
[/lua]

sorry its not more specific but thats a basic outline of one of the ways this can be done :slight_smile:

Deleted

unlike the low-end programming languages such as java and C++ you do not need to declare the structure of your table/array before you can use it.

in lua you just make a table and fill it with data.

in retrospect yes you are doing it correctly except:
[lua]
{Model, Name, Price}
[/lua]

Model, Name and Price are treated as variables.

simply remove them so you will have:
[lua]
local Vehicles = {
{“models/props_junk/garbage_milkcarton002a.mdl”, “Aston Martin DBS”, “250000”},
{“models/props_junk/PopCan01a.mdl”, “Audi R8 V10”, “100000”}}
}
[/lua]

to keep your code neat and awesome (this is purely personal preference) but have a line before each line/record you manually enter :slight_smile:

Deleted

the look will only work if you have declared string indexes.

for it to work your table will have to look something like the following:

[lua]
local Vehicles = {
{Model = “models/props_junk/garbage_milkcarton002a.mdl”, Name = “Aston Martin DBS”, Price = “250000”},
{Model = “models/props_junk/PopCan01a.mdl”, Name = “Audi R8 V10”, Price = “100000”},
{Model = “models/props_junk/garbage_takeoutcarton001a.mdl”, Name = “Audi TT '07”, Price = “30000”}
}
[/lua]

luckily lua automatically indexes our tables with integers starting at 1 (as opposed to low end languages starting at 0)

so by default you table will be something like:
[lua]
local Vehicles = {
{1 = “models/props_junk/garbage_milkcarton002a.mdl”, 2 = “Aston Martin DBS”, 3 = “250000”},
{1 = “models/props_junk/PopCan01a.mdl”, 2 = “Audi R8 V10”, 3 = “100000”},
{1 = “models/props_junk/garbage_takeoutcarton001a.mdl”, 2 = “Audi TT '07”, 3 = “30000”}
}
[/lua]

so if you are too lazy to add in string indexes, thats fine :smiley: you can change your loop to be something like:

[lua]
for k,v in pairs(Vehicles) do
local icon = vgui.Create(“SpawnIcon”)
icon:SetToolTip(v[2] … "
" … “$” … v[3]); // 2 is name | 3 is price
icon:SetModel(v[1]); // 1 is model
VehicleList:AddItem(icon);
end
[/lua]

Deleted

:smiley: np if you need anything else let me know

Deleted

a better alternative is to use DModelPanel.

its all the same functions that you have there just change SpawnIcon to DModelPanel.

Please tab your code, otherwise you’ll regret it if you come back to modify it later. It’s also a very good habit to get in to

Deleted

Try changing


local icon = vgui.Create("SpawnIcon")

to


local icon = vgui.Create("SpawnIcon", VehicleList)

Deleted

Deleted