Problem with drawing moving sprites.

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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Problem with drawing moving sprites.

Post by bartbes »

Ipairs is unsafe with either adding or removing, but next (and thus pairs) is safe when removing, but not adding. The relevant line from the manual:
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.
Moonkis
Prole
Posts: 14
Joined: Thu Jan 09, 2014 4:37 pm

Re: Problem with drawing moving sprites.

Post by Moonkis »

bartbes wrote:Ipairs is unsafe with either adding or removing, but next (and thus pairs) is safe when removing, but not adding. The relevant line from the manual:
The behavior of next is undefined if, during the traversal, you assign any value to a non-existent field in the table. You may however modify existing fields. In particular, you may clear existing fields.
That is helpful!

Regarding updating objects the same frame they were added into the vector, how about this:

Code: Select all

	local max = #EntityManager.objects
	local index = 1
	while index <= max do
		local e = EntityManager.objects[index]
		e:update(dt)
		if not e:isAlive() then 
			table.remove(EntityManager.objects, index)
		else
			index = index + 1
		end
		max = #EntityManager.objects
	end
We can safely update (thus adding if an objects update calls the EntityManager.create method which adds the object into EntityManager.objects) and delete without skipping. Seeing as how it refreshes the max count of the table we iterate the added items as well.
Post Reply

Who is online

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