LUA table questions

I opted for creating my own collision system for platforms. But I can’t find answers for LUA I need.

  1. How to create a list of instances in the Lua table, by inserting identifiers as new values?
  2. What’s the correct way to access those identifiers in for loop?
  3. Are Lua tables wiped/ reset when new stage is loaded/reset?
    Here’s my mock-up of what I want to build.
--Lua module
local MOV_PLAT = {}
function moving_platform_list(inst)                 --All platforms call this function with self as argument on_init
    --??? how to create unique entry?
    MOV_PLAT.? = inst
end

function moving_platform_collision(inst,x1,x2,y1,y2)--Players sides
    --??? Is this how I can cycle through platform instances?
    for key,value in pairs(MOV_PLAT) do             --MOV_PLAT table where platforms added self
        --Platform horizontal points
        local px1 = value.x1
        local px2 = value.x2
        if (px1 <= x1 and px2 >= x1) or (px1 <= x2 and px2 >= x2) then
            --Platform vertical points
            local py1 = value.y1
            local py2 = value.y2
            if (py1 <= y1 and py2 >= y1) or (py1 <= y2 and py2 >= y2) then
                return value                        --Return platform instance?????
            end
        end
    end
end

Can I do simply table.insert(MOV_PLAT, inst) ?

you can create a table :

table.insert(MOV_PLAT, inst)

and then

for key,value in ipairs(MOV_PLAT) do
value. do things…
end

key will be 1,2,3,4

if you do

MOV_PLAT.randomName = inst
MOV_PLAT.randomName2 = inst

then you need to do

for key,value in pairs(MOV_PLAT) do
value.do things…
end

now the keys of the table will be:
randomName
randomName2

1 Like

Thank you!
But by this:

Did you mean the same I did in for loop?

Yes

1 Like