My table.sort doesn't work

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
GoldenTCat
Prole
Posts: 2
Joined: Thu Dec 26, 2024 12:48 am

My table.sort doesn't work

Post 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
User avatar
pgimeno
Party member
Posts: 3674
Joined: Sun Oct 18, 2015 2:58 pm

Re: My table.sort doesn't work

Post 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.
GoldenTCat
Prole
Posts: 2
Joined: Thu Dec 26, 2024 12:48 am

Re: My table.sort doesn't work

Post 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
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 4 guests