generateQuads Limited to two instances?

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
andrewdoeslearn
Prole
Posts: 1
Joined: Sat Jun 06, 2020 12:59 am

generateQuads Limited to two instances?

Post by andrewdoeslearn »

I have the wierdest glitch.

Now, I am generating quads perfectly fine from ss3.png and tree.png.
for some reason, even though ss.png is the same file as ss3.png with a different name, it will not generate quads, and I am literally going nuts trying to figure out what I have missed.

I have changed so many parts of my code just to figure out why it keep throwing the error:
"Quad expected, nil found"

Usually this means my table is empty. But what am I missing?

if I change this snippet:

Code: Select all

  local x = 2
    while x < self.mapWidth do
    self:setstumptile(x , 6, TRUNK)
        x = x + 3
    end
to

Code: Select all

    local x = 2
    while x < self.mapWidth do
    self:setTile(x , 6, TRUNK)
        x = x + 3
    end

and remove the stump for loop in the render section, the above code works fine.

What am I missing????
Desperate here.
Thanks.

Andrew

Here is the code.

Code: Select all

Map = Class{}
require 'bin/globals/Tiles'
require 'Util'
require 'Util2'
require 'Player'
require 'bin/utils/Diagnostics'

MWIDTH = 50
-- Y Must always be one pixel higher than x
MHEIGHT = 51
CAMHEIGHT = 243
CAMWIDTH = 432
WBOUND = MWIDTH * 16 - CAMWIDTH
HBOUND = MHEIGHT * 16 - CAMHEIGHT


-- a speed to multiply delta time to scroll map; smooth value
local SCROLL_SPEED = 62

-- constructor for our map object
function Map:init()
    self.mapWidth = MWIDTH
    self.mapHeight = MHEIGHT
    
    self.spritesheet = love.graphics.newImage('graphics/ss3.png')
    self.sprites = generateQuads(self.spritesheet, 16, 16)
    self.tileWidth = 16
    self.tileHeight = 16
    self.tiles = {}

    self.stumpspritesheet = love.graphics.newImage('graphics/ss.png')
    self.stumpsprites = generateQuads2(self.stumpspritesheet, 16, 16)
    self.stumptilewidth = 16
    self.stumptileheight = 16
    self.stumptiles = {}

    self.treeSpritesheet = love.graphics.newImage('graphics/tree.png')
    self.treeSprites = generateQuads(self.treeSpritesheet, 48, 96)
    self.treeTileWidth = 48
    self.treeTileHeight = 96
    self.treeTiles = {}


    -- associate player with map
    self.player = Player(self)

    -- camera offsets
    self.camX = 0
    self.camY = 0
    camPosX = math.floor(self.camX)
    camPosY = math.floor(self.camY)
    camDX = camPosX - CAMWIDTH
    camDY = camPosY - CAMHEIGHT

----------------------------------------------------------------------TILE FILL-------------------------------------------------
    -- first, fill map with Tiles
    for y = 1, self.mapHeight do
        for x = 1, self.mapWidth do
            if x % 2 == 0 then
                self:setTile(x, y, FILL_TILE)
            elseif x % 1 == 0 then 
                self:setTile(x, y, FILL_TILE)    
            end
        end
    end
-- Tree Tiles -------------------------
    for y = 1, 101 do
        for x = 1, self.mapWidth do
                self:setTreeTile(x, y, 1)
        end
    end

    local x = 2
    while x < self.mapWidth do
    self:setstumptile(x , 6, TRUNK)
        x = x + 3
    end


end


-- function to update camera offset with delta time
function Map:update(dt)
    self.player:update(dt)
    ------------------------------------------------------------- -----CAMERA EDGE MOVEMENT --------------------------------------------------
    -- Translation locks
    -- Left Bounds
    if self.camX < 1 then
        LcamLock = 1
    else
        LcamLock = 0
    end
    -- Right Bounds
    if self.camX > WBOUND then
        RcamLock = 1
    else
        RcamLock = 0
    end
    -- Top Bounds
    if self.camY < 1  then
        TcamLock = 1
    else
        TcamLock = 0
    end
    -- Bottom Bounds
    if self.camY > HBOUND then
        BcamLock = 1
    else 
        BcamLock = 0
    end

    -- Translate Right
    if math.floor(self.player.x) > math.floor(self.camX) + CAMWIDTH - 10 and
        self.player.dx > 0 and
        RcamLock == 0 or
        love.keyboard.isDown('right') and
        RcamLock == 0 then
        self.camX = self.camX + WALKING_SPEED * dt
    end
    -- Translate Left
    if math.floor(self.player.x) < math.floor(self.camX) and
        self.player.dx < 0 and
        LcamLock == 0 or
        love.keyboard.isDown('left') and
        LcamLock == 0 then
        self.camX = self.camX - WALKING_SPEED * dt
    end
        -- Translate Down
    if math.floor(self.player.y) > math.floor(self.camY) + CAMHEIGHT - 10 and
        self.player.dy > 0 and
        BcamLock == 0 or
        love.keyboard.isDown('down') and
        BcamLock == 0 then
        self.camY = self.camY + WALKING_SPEED * dt
    end

    -- Translate up
    if math.floor(self.player.y) < math.floor(self.camY)  and
        self.player.dy < 0 and
        TcamLock == 0 or
        love.keyboard.isDown('up') and
        TcamLock == 0 then
        self.camY = self.camY - WALKING_SPEED * dt
    end

    px = self.player.x
    py = self.player.y

    -- Tree / player interaction
    --Left Side of tile
    local blockOffset = 12
    if self:tileAt(px + self.player.width + 2, py + self.player.height - 12 + blockOffset ).id == 4 or self:tileAt(px + self.player.width - 12 + 2, py + blockOffset).id == 4 then
        R_COLLISSION_LOCK = true
    else
        R_COLLISSION_LOCK = false
    end
    --Right Side of Tile
    if self:tileAt(px - 2, py + 0 + blockOffset).id == 4 or self:tileAt(px - 2, py + self.player.height - 12 + blockOffset).id == 4  then
        L_COLLISSION_LOCK = true
    else 
        L_COLLISSION_LOCK = false
    end

    -- Bottom of tile
    if self:tileAt(px + self.player.width, py - 2 + blockOffset).id == 4 or self:tileAt(px, py - 2  + blockOffset).id == 4  then
        U_COLLISSION_LOCK = true
    else
        U_COLLISSION_LOCK = false
    end
    -- Top of tile
    if self:tileAt(px, py + self.player.height -12 + 2 + blockOffset).id == 4 or self:tileAt(px + 16, py + self.player.height - 12 + 2 + blockOffset).id == 4 then
        D_COLLISSION_LOCK = true
    else 
        D_COLLISSION_LOCK = false
    end
end

-- gets the tile type at a given pixel coordinate
function Map:tileAt(x, y)
    return {
        x = math.floor(x / self.tileWidth) + 1,
        y = math.floor(y / self.tileHeight) + 1,
        id = self:getTile(math.floor(x / self.tileWidth) +1, math.floor(y / self.tileHeight) + 1)
        
    }
end

function Map:getTile(x, y)
    return self.tiles[(y - 1) * self.mapWidth + x]
end
function Map:setTile(x, y, id)
    self.tiles[(y - 1) * self.mapWidth + x] = id
end

-- STUMP TILE MANAGEMENT
function Map:getstumptile(x, y) 
    return self.stumptiles[(y - 1) * self.mapWidth + x]
end
function Map:setstumptile(x, y, tid)
    self.stumptiles[(y - 1) * self.mapWidth + x] = tid
end

-- TREE TILE MANAGEMENT
function Map:getTreeTile(x, y)
    return self.treeTiles[(y - 1) * self.mapWidth + x]
end
function Map:setTreeTile(x, y, sid)
    self.treeTiles[(y - 1) * self.mapWidth + x] = sid
end

-- renders our map to the screen, to be called by main's render
function Map:render()

   for y = 1, self.mapHeight do
        for x = 1, self.mapWidth do
            local tile = self:getTile(x, y)
            if tile ~= TILE_EMPTY then
                love.graphics.draw(self.spritesheet, self.sprites[tile],
                    (x - 1) * self.tileWidth, (y - 1) * self.tileHeight)
            end
        end
    end

   -- Draw Player Layer
    self.player:render()

    -- Draw Tree Map - MIDGROUND LAYER
    for y = 1, self.mapHeight do
        for x = 1, self.mapWidth do
            local tile = self:getTreeTile(x, y)
                love.graphics.draw(self.treeSpritesheet, self.treeSprites[tile],
                    (x - 1) * self.treeTileWidth, (y - 1) * self.treeTileHeight)
        end
    end 

        -- Draw stump - FORGROUND LAYER
    for y = 1, self.mapHeight do
        for x = 1, self.mapWidth do
            local tilex = self:getstumptile(x, y)
                love.graphics.draw(self.stumpspritesheet, self.stumpsprites[tilex],
                    (x - 1) * self.stumptilewidth, (y - 1) * self.stumptileheight)
        end
    end


    
end
hoistbypetard
Prole
Posts: 26
Joined: Fri May 22, 2020 7:00 pm

Re: generateQuads Limited to two instances?

Post by hoistbypetard »

It looks like you're calling generateQuads2 for ss.png and generateQuads for ss3.png. How do those two functions differ?

I'd suggest a little debugging by print statements here.

right after

Code: Select all

    self.stumpsprites = generateQuads2(self.stumpspritesheet, 16, 16)
add (caution: i wrote this in a forum text box, not in an interpreter...)

Code: Select all

for i,q in ipairs(self.stumpsprites) do
    print('stumpsprites['..tostring(i)..']', q:getViewport())
end
then right before

Code: Select all

                love.graphics.draw(self.stumpspritesheet, self.stumpsprites[tilex],
                    (x - 1) * self.stumptilewidth, (y - 1) * self.stumptileheight)
add

Code: Select all

print('# of stump sprites', #self.stumpsprites)
print('drawing stump sprite at', tilex)
and see if that helps you understand what's going on. You'll need to run from the terminal if you want to see the print statements, and if you're on windows, you'll need to run using lovec.exe instead of love.exe.

If you can't figure it out that way, you might want to try reproducing the problem in a little script that we can run, then zip up the script and all the files and post them as an attachment. (Or do that with your whole project if it's small enough.)
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 3 guests