Varying results on Build/Rebuild + Launch (SOLVED)

Hi,

I’ve done a fairly simple example “game” in which I have a background image and a factory which attempts to spawn 12 objects.

The problem is that sometimes when I do a Build/Rebuild + Launch it doesn’t spawn the objects.

There’s no build errors.

go.property("width", 10)

go.property("row", 0)

go.property("scroll", 0)

function init(self)
	spawn_row(self)
end

function update(self, dt)

end

function get_tile_position(x, y)
	return vmath.vector3(24 + x * 48 + (y % 2) * 1, 0 - 24 - y * 24, 0)
end

function spawn_ball(self, x, y)
	local position = get_tile_position(x, y)

	local ball = factory.create("#ball_factory", position, nil, {})

	msg.post(ball, "set_parent", { parent_id = go.get_id(), keep_world_transform = 0 })
end

function spawn_row(self)
	if self.row % 2 == 0 then
		for i = 0, self.width - 1 do
			spawn_ball(self, i, 0)
		end
	else
		for i = 0, self.width - 2 do
			spawn_ball(self, i, 0)
		end
	end
end

spawn_ball() and spawn_row() are declared as global functions (and get_tile_position, but that’s not an issue).

This is not recommended since those two functions are globally available from any script, not just the script attached to the game object. If you have multiple instances of the same script they will overwrite each other and there’s no telling which instance is used. This will probably, I’m not 100% certain, also cause the relative url you have to the ball_factory to not always resolve to the right factory or a valid factory instance as at (you should see an error about this though).

You should unless you really know what you are doing prefix variable and function declarations with the local keyword. global declarations could be useful for utility functions perhaps, but then again I’d rather put those in a module instead.

Remember that if you do change spawn_row() and spawn_ball() to local then you need to make sure they are declared before the init() function (since it’s calling spawn_row)

Hi britzl,

Thank you for your feedback, I’ve changed to local functions (just didn’t know about that possibility as I’m new to this language and editor).

It did however not change the outcome, as I still have problems with the balls sometimes not spawning after launching.

The output I have is:

INFO:ENGINE: Defold Engine 1.2.73 (181bfb1)
INFO:ENGINE: Loading data from: build/default
INFO:ENGINE: Initialised sound device ‘default’

INFO:DLIB: SSDP: Started on address 10.1.25.119
WARNING:DLIB: Failed to send announce message (-22)
WARNING:DLIB: Failed to send announce message (-22)
WARNING:DLIB: Failed to send announce message (-22)
INFO:SOUND: Waiting for OpenAL device to complete
INFO:DLIB: SSDP: Done on address 10.1.25.119

Do you have only this single script or do you have more scripts in the project?

I only had that script (it’s modified now however, but still only 1 script).

I tried launching the game ~10 times in a row now and didn’t get the problem.

(But I did get it even when I used local functions)

Ok, so it’s working now?

Yes, it’s working now.

But since I don’t know the cause, it’s hard to know how to avoid it in the future. =)

Do you have a background image at the same Z position as the balls, or might the tiles cover them? Multiple objects at the same Z might be sorted different from time to time.

You’re right! That’s it. I recently went through the z-positions trying to figure out the boundaries (-1 to 1). I fixed that and after that I didn’t get the problem.

Maybe there should be a note about undefined behaviour with colliding z-positions? If it would never show, it would be easier for me to figure out that it was the problem.

Great! Smart thinking there @sicher!

2 Likes

Thank you both for your time. =)

1 Like

Just to help out future folks:

I was running into a bunch of

WARNING:DLIB: Failed to send announce message (-22)

every time I started up on Windows 10. In my case it was 100% related to having a bunch of network adapters in my Network settings that were in an in-between state of being useful. They weren’t active, but weren’t disabled either. I went into Networking / Change Adapter Options and ‘disabled’ all of the leftover adapters I had listed from various VPNs I use for work, etc and it cleared out all of those DLIB messages.

3 Likes