Why does it not loop thru my table

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
NoreoAlles
Citizen
Posts: 85
Joined: Mon Jan 03, 2022 5:42 pm

Why does it not loop thru my table

Post by NoreoAlles »

I got a level map with a collider table:

Code: Select all

level_test.collision = {
   c = {x = 0, y = 3 , width = 10, height = 3}, c = {x = 0, y = 10, width = 10 , height = 2 }
}
My main.lua, in wich i feed this table and a few other thing into a level constructer:

Code: Select all

require "src/player"
require "src/gradient"
require "src/cloud"
require "src/menu"
require "src/level"
require "levelMaps/level_test"

function love.load()
    world = love.physics.newWorld(0, 0)
    world:setCallbacks(beginCon, endCon)

    virtual_width = 1280 / 4
    virtual_height = 780 / 4
    window_width = virtual_width *  2
    window_height = virtual_height * 2
    love.window.setMode(window_width, window_height)
    love.graphics.setDefaultFilter("nearest", "nearest")
    menu = menu.new({{x = virtual_width / 2 - 30 / 2, y = virtual_height / 3 - 15 /2, width = 30, height = 15, selected = false, screen = "gamePlay"}, {x = virtual_width / 2 - 30 / 2, y = virtual_height / 1.5 - 15/2, width = 30, height = 15, selected = false, screen = "options"} }, {0.8, 1, 0.3, 1})
    p = Player.new(0, 0)
    g = gradient.new()
    Clouds = {}
    img = love.graphics.newImage("art/spritesheet.png")
    cloudMaker()
    l = Level.new(0, 0, 27, 12, 16, 16, img, level_test.map, level_test.collision, entities) 
    state = "start"
end
and my level constructer:

Code: Select all

Level = {}
Level.__index = Level

function Level.new(x, y, map_width, map_height,tile_width , tile_height, image, tiles, collision, entities) 
    local instance = setmetatable({}, Level) 
    instance.x = x
    instance.y = y
    instance.map = {}
    instance.map.width = map_width
    instance.map.height = map_height
    instance.map.tile_width = tile_width 
    instance.map.tile_height = tile_height
    instance.map.tiles = tiles
    instance.map.spritebatch = love.graphics.newSpriteBatch(image)
    instance.map.image = image
    instance.map.collision = collision
    instance.map.quads = {}
    for y=1, instance.map.image:getHeight() / instance.map.tile_height do 
        for x=1, instance.map.image:getWidth() / instance.map.tile_width do 
            q = love.graphics.newQuad((x-1)*instance.map.tile_width, (y-1)*instance.map.tile_height, instance.map.tile_width, instance.map.tile_height, instance.map.image:getWidth(), instance.map.image:getHeight())
            table.insert(instance.map.quads, q)
        end
    end
    for y, xs in ipairs(instance.map.tiles) do 
        for x, value in ipairs(xs) do 
            if value > 0 then 
                print("Theres one of 4")
                instance.map.spritebatch:add(instance.map.quads[value], ( x-1) * instance.map.tile_width, (y-1) * instance.map.tile_height )
            end
        end
    end

    instance.coll = {}
    instance.coll.bodys = {}
    instance.coll.shapes = {}
    instance.coll.fixture = {}

    for i, c in ipairs (instance.map.collision) do 
        local b = love.physics.newBody(world, c.x + instance.x - c.width / 2,  c.y + instance.y - c.height / 2)
        local s = love.physics.newRectangleShape(c.width * instance.map.tile_width, c.height * instance.map.tile_height)
        local f = love.physics.newFixture(b, s)
        table.insert(instance.coll.bodys, b)
        table.insert(instance.coll.shapes, s)
        table.insert(instance.coll.fixture, f)
    end

    return instance
end 
in wich i dont even seem to loop thru the table.
Heres a .love
Attachments
game.love
(21.95 KiB) Downloaded 5 times
hippity hoppity, your code is now my property
(thanks btw)
User avatar
marclurr
Prole
Posts: 43
Joined: Fri Apr 22, 2022 9:25 am

Re: Why does it not loop thru my table

Post by marclurr »

The collision table has the key "c" defined twice, so there will only be one element in it (the last one I suspect). It looks like you want to use that table as an array, just remove the "c=" parts and your loop should work.
User avatar
NoreoAlles
Citizen
Posts: 85
Joined: Mon Jan 03, 2022 5:42 pm

Re: Why does it not loop thru my table

Post by NoreoAlles »

marclurr wrote: Tue Aug 16, 2022 9:21 am The collision table has the key "c" defined twice, so there will only be one element in it (the last one I suspect). It looks like you want to use that table as an array, just remove the "c=" parts and your loop should work.
Thanks, that did it.
hippity hoppity, your code is now my property
(thanks btw)
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests