How many sprites can you have on screen before slowdown?

for i=1,#self.zombies do
		local id = self.zombies[i]
		local zp = go.get_position(id)

Calling go.get_position() in a loop is not great because it creates multiple short-lived vector objects, right? Is there any alternative to this currently?

1 Like

You’re correct. One thing you could do is to create one v3 per zombie when the zombies are spawned and reuse the same v3 every frame. Something like this:

function init(self)
	math.randomseed(os.time())
	self.zombies = {}
	for i=1,200 do
		local x = math.random(1, 800)
		local y = math.random(1, 480)
		local p = vmath.vector3(x, y, 0)
		local id = factory.create("#factory", p)
		self.zombies[#self.zombies + 1] = {
			id = id,
			pos = p
		}
	end
end

function update(self, dt)
	local pp = go.get_position("/player/player")
	for i=1,#self.zombies do
		local zombie = self.zombies[i]
		local id = zombie.id
		local zp = zombie.pos
		local dir = vmath.normalize(pp - zp)
		zp = zp + dir * 10 * dt
		go.set_position(zp, id)
	end
end

I also recall that someone on Discord shared an extension that optimized for this scenario by working on the v3 components directly.

1 Like

And what about the case where the position is changed extraneously to the object? For example through physics.

I will also try to find the extension thanks!

Maybe go.get_position() could have an overload that accepts a vector reference as an argument to store the result.

This is correct. I made a similar optimisation to my original code too.

I was also thinking if it might make sense to have a global variable for the player position - if it was to be monitored by other routines? In this case it’s a single zombie factory. what would happen if I had other factories? They too could share the information? Although the best option would be to use a single update function for all factories.