Got a funny one for you that i’m rather embarassed to admit i’m struggling with here, because it absolutely is something simple i’m missing. Perhaps just a fresh pair of eyes will help…
Essentially, when attempting to append a value to a table with a string key, the table simply doesn’t seem to accept it and returns as empty later in the code.
---@ Global Variables
VehicleConfigs = {} -- Contains the config data for all vehicles
---@ Find all vehicle files and initialise vehicles
AddEventHandler('onResourceStart', function(name)
for _, v in ipairs(FLS_VehConfigs) do
local loadFile= LoadResourceFile(GetCurrentResourceName(), "./Vehicles/".. v ..".json")
local extract = json.decode(loadFile)
local vehicleName = tostring(v) -- 'v' is currently equal to 'police', 'police2' e.t.c.
VehicleConfigs[vehicleName] = extract -- Add data to array with key = vehicle name
print(#VehicleConfigs) -- Returns 0 without fail.
end
end)
If i attempt to add a value to the array with the following function…
table.insert(VehicleConfigs, extract)
It works perfectly fine! However, it doesn’t seem to be playing ball with the good ol’ square brackets. Or the period for that matter, I’ve given that a go too.
As I said, it’s going to turn out to be something super simple that I missed however I’ve been racking my brains for hours and decided a second pair of eyes may be of benefit.
G’day! When table keys are strings and not numbers, #tableName for the length of the table won’t work. You must iterate through the table with a counter to get it’s length.
As an example, take the following;
local exampleOne = {
"Value One", -- Also written as [1] = "Value One"
"Value Two", -- Also written as [2] = "Value Two"
}
local exampleTwo = {
["keyOne"] = "Value One",
["keyTwo"] = "Value Two",
}
print(#exampleOne) -- Returns 2
print(#exampleTwo) -- Returns 0
local count = 0
for k, v in pairs(exampleTwo) do
count = count + 1
end
print(count) -- Returns 2
So when you have string keys for a table, iterate and increment a counter to get the length.
That’s why this is happening;
print(#VehicleConfigs) -- Returns 0 without fail.
As for your code, I don’t know why this wouldn’t work for you;
VehicleConfigs = {}
AddEventHandler('onResourceStart', function(name)
for _, vehicleName in ipairs(FLS_VehConfigs) do
local loadFile = LoadResourceFile(GetCurrentResourceName(), "./Vehicles/"..vehicleName..".json")
VehicleConfigs[tostring(vehicleName)] = json.decode(loadFile)
end
end)
Were you maybe thinking that it was empty because you were using the wrong techinque for getting the table length? Or are you actually trying to access it and it’s empty/nil?
If i’m to be honest, both responses combined resolved this. I was relying on ‘#’ to count the items in the array for debug, and therefore didn’t look any further into the client side of the resource where it loops through the array it’s passed. And thanks to @Loaf-Scripts , I looked at the loops I was using and realised I was utilising ‘ipairs’ which, of course, won’t work with keys that are not numbered, something I knew but didn’t know i’d added.