I have three types of crates in my levels that I spawn on load.
local push_crates = { search_and_spawn( "crates", level_data.PUSH_CRATE, "/level_spawner#factory_crate_push", false ) }
local slide_crates = { search_and_spawn( "crates", level_data.SLIDE_CRATE, "/level_spawner#factory_crate_slide", false ) }
local pull_crates = { search_and_spawn( "crates", level_data.PULL_CRATE, "/level_spawner#factory_crate_pull", false ) }
I then wanted to put them linearly into another table:
level_data.crates = {
unpack(push_crates),
unpack(slide_crates),
unpack(pull_crates)
}
However, the rest of the code breaks if one or more of the types are missing form the level. It didn’t start working until I did this whole mess:
if #push_crates > 0 and #slide_crates > 0 and #pull_crates > 0 then
level_data.crates = {
unpack(push_crates),
unpack(slide_crates),
unpack(pull_crates)
}
elseif #push_crates > 0 and #slide_crates > 0 then
level_data.crates = {
unpack(push_crates),
unpack(slide_crates)
}
elseif #slide_crates > 0 and #pull_crates > 0 then
level_data.crates = {
unpack(slide_crates),
unpack(pull_crates)
}
elseif #push_crates > 0 and #pull_crates > 0 then
level_data.crates = {
unpack(push_crates),
unpack(pull_crates)
}
elseif #push_crates > 0 then
level_data.crates = {
unpack(push_crates)
}
elseif #slide_crates > 0 then
level_data.crates = {
unpack(slide_crates)
}
elseif #pull_crates > 0 then
level_data.crates = {
unpack(pull_crates)
}
end
I’m guessing it happens when any of the tables are empty, it still inserts, something, nil or an empty table.
Any idea how to shorten that code?