Reputation: 675
I have a question regarding LUA.
I have been working with LUA for some time now while modding an old video game, and I recently ran into an issue that I have not been able to solve after a few days of trying.
In simplest terms, all I'm trying to do is insert a data table inside of another table, however the table I'm trying to insert into happens to be located inside of another table, which in itself is an entry in yet another table.
I know it must sound confusing, hence I made a simplified diagram of the situation.
In the diagram underneath, I want to insert "[1]" into "["Platforms"]".
VehicleClass = {}
VehicleClass[1] =
{
["Platforms"] = {},
[1] = {*data*},
}
In other words, and my main question rephrased: How would I insert "[1]" into "[Platforms]" WITHOUT doing the following:
VehicleClass = {}
VehicleClass[1] =
{
["Platforms"] = {
[1] = {*data*},
},
}
One notable thing I'm trying to achieve here is to insert "[1]" into "["Platforms"]" WITHIN "VehicleClass[1]", however I already sort of suspect that would be impossible.
I'm still somewhat a begginer with LUA and therefore I spent some time trying to resolve the issue myself. Beforehand I tried doing things like:
VehicleClass = {}
VehicleClass[1] =
{
["Platforms"] = {},
[1] = {*data*},
table.insert(VehicleClass[1]["Platforms"], VehicleClass[1][1]),
}
or
VehicleClass = {}
VehicleClass[1] =
{
["Platforms"] = {},
["Platforms"][1] = {*data*},
}
however all my previous attempts failed.
I may have missed some special LUA formatting that would be required here, as I said I'm still a begginner with LUA.
Thanks in advance and I hope I can finally be able to get some expert advice on this.
Upvotes: 1
Views: 103
Reputation: 2531
If you try just insert data try:
table.insert(VehicleClass[1]["Platforms"], 1, {*data*})
If you try insert if not exists, but update if exists, then try:
VehicleClass[1]["Platforms"][1] = {*data*}
If you try just create this object:
VehicleClass =
{
{
["Platforms"] = {
{*data*}
}
}
}
If you are confused using this, then I propose do it in stages:
local myPlatformData = {*data*}
local myPlatformsData = { myPlatformData }
local myVechicle = { ["Platforms"] = myPlatformsData }
VehicleClass = { myVechicle }
Or more simple:
local myPlatformData = {*data*}
local myPlatformsData = {}
myPlatformsData[1] = { myPlatformData }
local myVechicle = { ["Platforms"] = myPlatformsData }
VehicleClass = {}
VechicleClass[1] = myVechicle
Upvotes: 1