# Accelerating objects using the "set\_speed" message

**URL:** <https://forum.defold.com/t/accelerating-objects-using-the-set-speed-message/68962>\
**Category:** Questions\
**Created:** [July 27, 2021, 8:29pm UTC](https://forum.defold.com/t/accelerating-objects-using-the-set-speed-message/68962 "2021-07-27T20:29:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![digigameworld73](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/digigameworld73/32/8921_2.png) [@digigameworld73](https://forum.defold.com/u/digigameworld73)\
**Post date:** [July 27, 2021, 8:29pm UTC](https://forum.defold.com/t/accelerating-objects-using-the-set-speed-message/68962/1 "2021-07-27T20:29:35Z")

</div>

good day Defolders, this forum recently helped me with gradually accelerating the ground objects in my game which I’m building from the endless runner tutorial from Defold but I have encountered another issue after programming the game to spawn different platforms randomly i remembered that the floor was constantly accelerating and despite the “set\_speed” message from my ground script to the platform controller script and it ends up looking really weird because the floor will end up moving faster than the platform and i have been trying to get them to accelerate at the same speed possibly using the “set\_speed” message i have tried everything but for my current understanding at least here is the ground script  
Ground script:

```
        -- ground.script
        -- require the game_state LUA module
        local game_state = require("main.game_state")
        --Declares a local variable for acceleration
        local acceleration = 0.005
        function on_message(self, message_id, message, sender)
        	if message_id == hash("set_speed") then -- <1>
        		self.speed = message.speed -- <2>
        	end
        end

    local pieces = { "floor0", "floor1", "floor2" }--<1>
    function init(self)
    	self.speed = 600--speed in pixels
    end
    function update(self, dt)
    	game_state.speed = game_state.speed * (1 + dt * acceleration)
    	for i, p in ipairs(pieces) do -- <4>
    		local pos = go.get_position(p)
    		if pos.x <= -960 then -- <5>
    			pos.x = 1918+ (pos.x + 960)
    		end
    		pos.x = pos.x - self.speed * dt * game_state.speed -- <7>
    	go.set_position(pos, p) -- <8>
    	end
    end'

```

Here is the platform controller script with what i tried doing:  
Platform script:

```
-- controller.script
go.property("speed", 500)
local game_state = require("main.game_state")
local acceleration = 0.005

local grid = 460
local platform_heights = { 200, 350, 400, 500, 510, 600} -- <1>
local platforms = { "#grass platform","#grassplatform2", "#rockplatform" , "#rockplatform2"} --"#rockplatform", "#rockplatform2", "#rockplatform3"
local long_platforms = { "#grassplatformlong", "#grassplatformlong2", "#rockplatformlong", "#rockplatfromlong2"}--"#rockplatformlong", "#rockplatfromlong2", "#rockplatfromlong3" 
function init(self)
	msg.post("ground/control#ground", "set_speed", { speed = self.speed })
	msg.post("Dave#Character", "set_speed", { speed = self.speed })
	self.gridw = 0
end
function update(self, dt) -- <2>
	self.gridw = self.gridw + self.speed * dt + acceleration 
	if self.gridw >= grid then
		self.gridw = 0

		-- Maybe spawn a platform at random height
		if math.random() > 0.2 then
			local h = platform_heights[math.random(#platform_heights)]
			local f = platforms
			if math.random() > 0.5 then
				f = long_platforms
			end
			local platform = f[math.random(1, #f)]
			local p = factory.create(platform, vmath.vector3(1600, h, 0), nil, {}, 0.6)
			msg.post(p, "set_speed", { speed = self.speed * dt * acceleration })
		end
	end
end

```

---

<div class="post-metadata">

**Author:** ![Mathias\_Westerdahl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/mathias_westerdahl/32/228_2.png) [@Mathias\_Westerdahl](https://forum.defold.com/u/Mathias_Westerdahl)\
**Post date:** [July 28, 2021, 8:09am UTC](https://forum.defold.com/t/accelerating-objects-using-the-set-speed-message/68962/2 "2021-07-28T08:09:25Z")

</div>

Are the “set\_speed” messages arriving in the on\_message? Have you verified?

I think one unfortunate thing is that there is a builtin “set\_speed” message (used for sounds). I would assume it would cause an error in the logs though 🤔

---

<div class="post-metadata">

**Author:** ![Alex\_8BitSkull](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/alex_8bitskull/32/5424_2.png) [@Alex\_8BitSkull](https://forum.defold.com/u/Alex_8BitSkull)\
**Post date:** [July 28, 2021, 8:29am UTC](https://forum.defold.com/t/accelerating-objects-using-the-set-speed-message/68962/3 "2021-07-28T08:29:33Z")

</div>

It doesn’t cause an error, but it does seem to clamp values 0-1.

Edit: Sorry, I remembered wrong. It doesn’t do that. It swallows other parameters specified in the table and gives speed = 1 if speed isn’t specified. It does however not give any errors or warnings.

> [@Monarch - Simple screen manager](https://forum.defold.com/t/monarch-simple-screen-manager/14138/42):
>
> I put this in a project that doesn’t have Monarch and got the same results as you: msg.post(".", "set\_speed", {kmh = 250}) DEBUG:SCRIPT: hash: [set\_speed] DEBUG:SCRIPT: { --[[0000015544431E90]] speed = 1 } Reserved messages are discussed in [this thread](https://forum.defold.com/t/reserved-words/53940), but “set\_speed” is not mentioned. Looks like the sound speed parameter was added in [1.2.162](https://forum.defold.com/t/defold-1-2-162-has-been-released/62959), which is after the thread on reserved words happened and might explain the absence from the list. If indeed my theory is correct.

---

<div class="post-metadata">

**Author:** ![digigameworld73](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/digigameworld73/32/8921_2.png) [@digigameworld73](https://forum.defold.com/u/digigameworld73)\
**Post date:** [July 28, 2021, 6:13pm UTC](https://forum.defold.com/t/accelerating-objects-using-the-set-speed-message/68962/4 "2021-07-28T18:13:59Z")

</div>

I’m trying to make the platforms and the ground objects to accelerate uniformly over time and I thought the "set\_speed message has something to do with it

---

<div class="post-metadata">

**Author:** ![digigameworld73](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/digigameworld73/32/8921_2.png) [@digigameworld73](https://forum.defold.com/u/digigameworld73)\
**Post date:** [July 28, 2021, 9:37pm UTC](https://forum.defold.com/t/accelerating-objects-using-the-set-speed-message/68962/5 "2021-07-28T21:37:39Z")

</div>

But thanks for your help guys i have solved the problem I was editing the wrong script I forgot I created a script to manage the speed initially
