buggy table handling

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
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

buggy table handling

Post by Pospos »

Hello.
I have an issue with a project of mine(i know, i make a lot of projects, it's because i want to improve my abilties the quickest possible) it's a platformer, and i have a problem very strange. I have a lua table, and i try to add a value to this table by a function but it doesn't work, each time i tell to the computer to print the length of the table, it always return zero. Why.
there's the buggy code:

collision.lua

Code: Select all

function newCollisionBox(x, y, id, what_x, what_y, skin) -- suuposed to create a collision object.

blocs = {} -- this is the table i want to add a value in
blocs[id] = {}  -- this is the value i add
blocs[id].x = x
blocs[id].y = y
blocs[id].sprite = love.graphics.newImage('assets/tiles/'..skin..'.png') 

end
main.lua

Code: Select all

function love.draw()

    love.graphics.print(loop, 0, 0)-- values
    love.graphics.print(up .. down .. left .. right, 0, 10)
    love.graphics.print(#blocs, 0, 20) -- here i want to verify my code, but it always return nil

    playerdraw()
end
Attachments
projet.love
(76.64 KiB) Downloaded 79 times
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: buggy table handling

Post by Azhukar »

Code: Select all

blocs = {}
This creates a new table each time it is called. You're calling the function twice, meaning you're creating 2 different tables, then only indexing the last created one. Declare the blocs table outside of newCollisionBox function.
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Re: buggy table handling

Post by Pospos »

thanks for responding so quickly.
But the value blocs[1] is still equal to 0 even if it was supposed to be two because i declared it with
blocs[id].x, blocs[id].y, and blocs[id].sprite.
User avatar
Azhukar
Party member
Posts: 478
Joined: Fri Oct 26, 2012 11:54 am

Re: buggy table handling

Post by Azhukar »

Pospos wrote: Wed Oct 25, 2017 1:31 pm thanks for responding so quickly.
But the value blocs[1] is still equal to 0 even if it was supposed to be two because i declared it with
blocs[id].x, blocs[id].y, and blocs[id].sprite.
It is not equal to 0, it is equal to nil.

This is what you're doing:

Code: Select all

blocs = {}
blocs[1] = 123
print(blocs[1]) --123
blocs = {}
blocs[2] = 456
print(blocs[1]) --nil
See http://lua-users.org/wiki/TablesTutorial
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Re: buggy table handling

Post by Pospos »

ok. Thanks for your help. that's quite nice.
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: buggy table handling

Post by Beelz »

EDIT: Got ninja'd on the reply, but here's a minimal solution anyways:
-----------------------------------------------------------------------------------------

What Azhukar meant is that you are overwriting the table every time you call the newCollisionBox function...

Code: Select all

local _id = 0
local function nextID()
	_id = _id + 1
	return _id
end

blocs = {} -- move this outside of the function so it's not overwritten every time

function newCollisionBox(x, y, w, h, skin) -- removed "id" as param (keep logic seperate(see local function above))
	local bloc = { -- make a local "bloc" that gets inserted into "blocs" table
		id = nextID(),
		x = x,
		y = y,
		w = w, -- not sure what "what_x" and "what_y" were for...
		h = h, -- so changed to witdh and height for now
		--sprite = love.graphics.newImage('assets/tiles/'..skin..'.png')  
	}

	--insert using "id" as index
	blocs[bloc.id] = bloc
end

function love.load()
	newCollisionBox(10, 10, 100, 100)
	newCollisionBox(200, 200, 200, 200)
end

function love.draw()
  --love.graphics.print(loop, 0, 0)-- values
  --love.graphics.print(up .. down .. left .. right, 0, 10)


  for i, v in pairs(blocs) do -- using pairs because of named indexes(although in this case the names are just numbers, so you could probaby get away with ipairs, at least until you remove an index)
  	love.graphics.rectangle("fill", v.x, v.y, v.w, v.h)
  end

  love.graphics.print(#blocs, 0, 20) -- here i want to verify my code, but it always return nil

  --playerdraw()
end

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
Pospos
Citizen
Posts: 73
Joined: Sun Jun 18, 2017 11:10 am
Location: Agadir

Re: buggy table handling

Post by Pospos »

actually, this part did work but it made another issue there, that the character is not colliding with objects anymore
check this
Attachments
game.love
(77.38 KiB) Downloaded 69 times
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 81 guests