Script execution order [Solved]

I’m coming to you because I’m new to Defold and there’s something I don’t really understand, I’m currently taking a video course on Defold and I see a difference between what is on my screen and what happens in the video while the code is almost identical.

It is a simple brick break, in the video (as well as in what I have coded) at the start of a game the X position of the ball is defined on the same X axis as the racket, on the video the ball is replaced in the center of the racket but with a time lag whereas in my case it is instantaneous.

I then wonder if in the video the racket script actually runs after the ball.

Example: Position of the racket at 100 (X) so we also place the ball at 100, then we update the racket to 110 and we display the rendering with the ball at 100 but the racket at 110.

So my question is, can we change the execution order of scripts and/or game objects?

No, not really. Script execution order is not guaranteed and you need to design your code around this fact.

I’m not sure I understand the problem you are facing based on the description though. Could you elaborate? Or share some code. Or a video of what’s going on.

Sounds to me like there’s a problem with your code! Share your code here

like this 

and we’ll have a look!

Here is a comparison video:

As for the code, here is the one from the course:

Paddle:

function init(self)
	msg.post(".","acquire_input_focus")
	self.initPositionY = go.get_position().y 
	self.position = go.get_position()
end

function update(self, dt)
	local sizeRaquette = go.get("#sprite", "size")
	if self.position.x < sizeRaquette.x/2 then self.position.x = sizeRaquette.x/2 end
	if self.position.x > 1280 - sizeRaquette.x/2 then self.position.x = 1280 -sizeRaquette.x/2 end
	go.set_position(self.position)
end



function on_input(self, action_id, action)
	self.position = vmath.vector3(action.x,self.initPositionY,0)
end

Ball:

function init(self)
	
	self.currentState = "init"
	self.velocity = vmath.vector3(1,1,0)
	self.speed = 5
	msg.post(".","acquire_input_focus")

end

function update(self, dt)
	
	if self.currentState == "init" then -- SEE HERE FOR THE PROBLEM
		
		local positionRaquette = go.get_position("raquette")
		local sizeRaquette = go.get("raquette#sprite", "size")
		local sizeBalle = go.get("balle#sprite", "size")

		local y = sizeRaquette.y / 2 + sizeBalle.y / 2
		local offset = vmath.vector3(0,y+1,0)
		self.position = positionRaquette+offset

	elseif self.currentState == "move" then
		self.position = self.position + self.velocity * self.speed
	end

	go.set_position(self.position) 

end

--[[ I did not put the rest because it concerns the collision of the walls and the start of the movement of the ball in space ]]

And what I have coded:

Paddle:

function init(self)

	msg.post(".", "acquire_input_focus")

	self.size = vmath.mul_per_elem(
		go.get("#sprite", "size"),
		go.get("#sprite", "scale")
	)

	self.ox = self.size.x/2
	self.oy = self.size.y/2

	self.pos = go.get_position()

end

function update(self, dt)

	-- Screen border collision --

	if self.pos.x > G_WIDTH-self.ox then
		self.pos.x = G_WIDTH-self.ox
	elseif self.pos.x < self.ox then
		self.pos.x = self.ox
	end

	-- Set object position --

	go.set_position(self.pos)

end

function on_input(self, action_id, action)
	self.pos.x = self.pos.x + action.dx
end

Ball:

function init(self)

	msg.post(".", "acquire_input_focus")

	self.size = vmath.mul_per_elem(
		go.get("#sprite", "size"),
		go.get("#sprite", "scale")
	)

	self.ox = self.size.x/2
	self.oy = self.size.y/2

	self.vel = vmath.vector3(0,1,0)
	self.speed = 200

	self.move = false
	self.query_move_timer = false

end

function update(self, dt)

	local pos;

	if self.move then

		--[[ I do not put this passage to shorten because it is about the movement in space ]]

	else 

		-- Timer count for moving request (quick tapping of the screen) --

		if self.query_move_timer then
			self.query_move_timer = self.query_move_timer + dt
		end

		-- Set position to player --

		local player_pos = go.get_position("player")

		local player_size = vmath.mul_per_elem(
			go.get("player#sprite", "size"),
			go.get("player#sprite", "scale")
		)

		pos = player_pos+vmath.vector3(
			0,(player_size.y+self.size.y)/2,0
		)

		-- Set direction before start --

		self.vel.x = (pos.x < G_H_WIDTH) and 1 or -1

	end

	go.set_position(pos)

end

Even if I wrote it very differently, at the level of the position of the ball I do not understand what makes this difference?

Well, I’ve got to say, it seems pretty well coded to me :woman_shrugging: sorry that I can’t help you out here… britzl may know more than me.

You can change ball’s parent ( go.set_parent() ) to paddle, so you don’t need to worry about moving it left-right.
Other approach is to update their position in the same update function(which is also more efficient)

Well, if you position the ball to the current position of the paddle and then move the paddle in the same frame you’ll get that kind of delay. If you on the other hand first move the paddle and then the ball it will work.

This is a quick and easy solution. Remember to remove parenting when the ball is launched from the paddle!

Yep, this also works!

And a third option is to not move the ball in update when the ball is attached to the paddle. Instead send a message from the paddle to the ball and update in on_message instead.

But I think I’d do what @selimanac suggests and move the ball in the paddle.script when the ball is attached to the paddle.

That’s exactly what I thought on the first point you say, but how is this order of updates between scripts defined by Defold, just out of curiosity?

Thank you very much if not for your attention and your tips, I will apply it right away ! ^^

I’m honestly not entirely sure. The update loop will iterate over all component types per collection, and it’s the order in which the components are added to this list which matters.

And the order most likely depends on the creation/init code of the collection where I believe the order in which the game objects are added to the .collection file is what matters.

It is very likely indeed, it seems to me precisely that I did not create the files in the same order as the course I followed, which is surely the cause of this difference in behavior, anyway it’s a very basic course that didn’t dwell on the details.

In any case, I like this engine a lot and thank you again for answering my questions! :smile:

3 Likes

Would you be able to tell what course you are taking - maybe it could be something for others too.

1 Like

I had not said it because it is a course in French, it came from this Youtube channel if it may interest French people: https://www.youtube.com/@duruticodeur

2 Likes

Merci! Tutorials could always be always helpful, this one for the French-speakers here. And I’ll take the opportunity to brush up my French. :smiley:

1 Like

I take this opportunity to point out for others that these videos are far from perfect but are very useful if you are new to Defold, unfortunately the Defold presentation videos are almost non-existent in French…

1 Like

I don’t think I’ve seen these videos before. Thank you for sharing! I’ll make sure that we link to them!

1 Like