Accelerating objects using the "set_speed" message

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

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 :thinking:

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.

2 Likes

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

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

1 Like