Button/Lever operated platforms?

Hi! Im sorry to bother but I’m new to using defold, and I’m making a platformer to start off with. It’s similar to fireboy and watergirl, where the player characters need to step on buttons or push levers to raise or lower platforms. I was wondering how you would implement that in-game? As I’m not sure how to program the actions of one game object (the player character) colliding with another (the button/lever) would influence another (the platform). Thank you in advance for any help!! ToT

So best thing to do with things like this is just try many different approaches and see what does and doesnt work. Its also good to do some searching and even the lua/defold chatgpt that someone made is pretty decent most of the time at answering questions.

One way you can handle this is have the button to have a collision object and when the player comes in contact with it you can send out a message to the platform to do a function. You can also check for a key input at collision time if you want to trigger when using some type of input. I’m sure there are more ways and “better” ways but this is a way that first pops into my mind and what I would attempt.

I would do like this:

  • Create a button.go with a trigger collision object and sprite to represent the button to trigger the platform
  • Create a button.script and attach it to the button.go
  • Add a script property with the id of the thing it is going to control
  • The button.script should check if it collides with the player (if the player is standing on it)
  • When it detects the collision it will send a message to the object it is activating

Something like this:

-- button.script
go.property("thing_to_activate", hash(""))

function on_message(self, message_id, message, sender)
	if message_id == hash("trigger_response") then
		if message.enter then
			msg.post(self.thing_to_activate, "activate")
		else
			msg.post(self.thing_to_activate, "deactivate")
		end
	end
end

And then obviously a script on your platform to react to the “activate” message sent from button.script