Page 1 of 1

My table.sort doesn't work

Posted: Thu Dec 26, 2024 1:06 am
by GoldenTCat
I'm developing a game and the character is divided into parts and I'm trying to make a code that makes the sprites not messed up when the character rotates using table.sort, the problem is that with or without this function it remains messy :shock:

part of the code that SHOULD fix the order of the sprites

Code: Select all

	local sort = function(a, b) return a.z < b.z end
	table.sort(p.body, sort)
	
	for i, b in pairs(p.body)do	b.grid = newGrid(0, p.dir, 65, 65) end	--this selects each sprite for each body part

I've been researching how to fix it for a while and now all I can do is ask on the love 2d website

Re: My table.sort doesn't work

Posted: Thu Dec 26, 2024 2:14 am
by pgimeno
Hello, welcome to the forums!

table.sort is designed to work on sequences (tables with numeric indices), and you're using pairs() to iterate it, which suggests it's either not a sequence, or you should be using ipairs() instead.

Re: My table.sort doesn't work

Posted: Thu Dec 26, 2024 2:38 am
by GoldenTCat
pgimeno wrote: Thu Dec 26, 2024 2:14 am Hello, welcome to the forums!

table.sort is designed to work on sequences (tables with numeric indices), and you're using pairs() to iterate it, which suggests it's either not a sequence, or you should be using ipairs() instead.
oh, I understand now, I didn't know that :o