Page 1 of 1

buggy table handling

Posted: Wed Oct 25, 2017 12:51 pm
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

Re: buggy table handling

Posted: Wed Oct 25, 2017 1:02 pm
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.

Re: buggy table handling

Posted: Wed Oct 25, 2017 1:31 pm
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.

Re: buggy table handling

Posted: Wed Oct 25, 2017 1:46 pm
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

Re: buggy table handling

Posted: Wed Oct 25, 2017 1:56 pm
by Pospos
ok. Thanks for your help. that's quite nice.

Re: buggy table handling

Posted: Wed Oct 25, 2017 2:00 pm
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

Re: buggy table handling

Posted: Wed Oct 25, 2017 2:55 pm
by Pospos
actually, this part did work but it made another issue there, that the character is not colliding with objects anymore
check this