Question: STI and Windfield: Building Polygon Objects from Tables
Posted: Tue Mar 16, 2021 2:59 am
Hello,
For a few hours now, I have been attempting to build static collider boxes in Windfield using object layers created in Tiled (so I'm using STI).
I can make rectangles easily enough, but polygons are giving me trouble because they take vertices (x1, y1, x2, y2, etc), but the lua file with those vertices has those values nested inside of other tables:
etc, etc
I'm after the polygon table data so I can build shapes using Windfield's world:newPolygonCollider function.
I have tried various methods for grabbing nested tables, but haven't managed to get the x and y values I need. I'll just post my original code:
This code runs for each object in the object table above, then passes the x and y coords along with the polygon table to a function:
This function attempts to create a polygon (note, I have not factored in the x and y coords just yet, but will need to add those to each set of verticies):
I've messed around with unpacking the polygon table myself (two for loops to iterate through each table and their nested tables) and passing them to a new table that can be read directly by love.graphics.newPolygonShape in Windfield, but I only get as far as passing 2 vertices and getting an error.
tldr; I need to take the values from tables within tables:
and turn it into values I can pass like this:
I think I've been on the right track (above code not withstanding), but I need someone to set me straight on tables within tables and how to pull their values out.
For a few hours now, I have been attempting to build static collider boxes in Windfield using object layers created in Tiled (so I'm using STI).
I can make rectangles easily enough, but polygons are giving me trouble because they take vertices (x1, y1, x2, y2, etc), but the lua file with those vertices has those values nested inside of other tables:
Code: Select all
objects = {
id = 42,
name = "",
type = "",
shape = "polygon",
x = 269,
y = 640,
width = 0,
height = 0,
rotation = 0,
visible = true,
polygon = {
{ x = 0, y = 0 },
{ x = -12, y = 0 },
{ x = 51, y = -63 },
{ x = 51, y = -52 }
},
properties = {}
},
I'm after the polygon table data so I can build shapes using Windfield's world:newPolygonCollider function.
I have tried various methods for grabbing nested tables, but haven't managed to get the x and y values I need. I'll just post my original code:
This code runs for each object in the object table above, then passes the x and y coords along with the polygon table to a function:
Code: Select all
for i, par in pairs(levelMap.layers["semiSolidSlope"].objects) do
spawnSemiSolidSlope(par.x, par.y, par.polygon)
end
Code: Select all
function spawnSemiSolidSlope(x, y, polyTable)
local semiPlatform = world:newPolygonCollider(x, y, polyTable, {collision_class = "semiSolid"})
semiSlopePlatform:setType('static')
table.insert(semiSlopePlatforms, semiSlopePlatform)]]
end
tldr; I need to take the values from tables within tables:
Code: Select all
objects = {
obj = {
{x=1,y-3},
{x=-2,y=4},
{x=6,y=8},
{x=8,y=8}
},{
etc
}
}
Code: Select all
world:newPolygonCollider(x1, y1, x2, y2, x3, y3, etc)