Basic Defold concept

Hi
I just got Defold downloaded a couple of days ago and wanted to see how it works. I have watched some youtube videos and read etc. but it hasn’t clicked yet…

Do anyone have a tutorial/video or something just explaining a simple thing like a button, when clicked/touched it does something to another object like changing text or move it x +5?
My challenge lies in understanding how to refer to other objects.

*About me: Made 4 small apps and 1 game in Game maker, and one small app in Corona (Lua) and playing with python for webscraping and calculating stuff.

1 Like




I hope what I’m responding with is useful to you

1 Like

Thanks for the reply.
The second I have read but did not explain the concept I am trying to get well enough.
The first I haven’t seen. I look into that later :slight_smile: But I just think it is changing the text of the same object (the button) not a different object which I am trying to understand how to do.

check the third link

This is typically done via message passing, at least if it’s from a gui component like a button to a gameobject. Imagine this setup with one gui containing a button and a game object for the player:

The gui looks like this:

The gui has the following script:

function init(self)
	msg.post(".", "acquire_input_focus")
end

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.released then
		if gui.pick_node(gui.get_node("jump_button"), action.x, action.y) then
			msg.post("player", "jump")
		end
	end
end
  1. The script acquires input focus (ie will get input events from the engine).
  2. The script checks for a touch action (mapped in input bindings). In my case for left mouse button (which will auto bind for touch events on mobile as well)
  3. If a touch action is detected and the touch/click was released (ie finger leaves screen or mouse button) then we proceed to check if the finger/mouse was within the bounds of the button box node named “jump_button”
  4. If the touch/click was within the bounds of the button we send a message to a game object with id “player”. The message we send has the id “jump”.

Message passing can be used to send a message to any part of your application from anywhere else. It’s very powerful and the typical use-case is as easy as above, but for sending messages to other systems you need to understand the addressing system used in Defold. You can read more about this in the Message Passing manual.

The receiver of the “jump” message is the player game object. The player has a script attached which will take care of the message:

function on_message(self, message_id, message, sender)
	if message_id == hash("jump") then
		local to = go.get_position().y + 20
		go.animate(".", "position.y", go.PLAYBACK_ONCE_PINGPONG, to, go.EASING_INOUTQUAD, 1)
	end
end

Here we detect the message (note messages are hashes for performance reasons) and we let the engine animate the game object using go.animate().

3 Likes

If you on the other hand want to manipulate one game object from another via a script component attached to the game object then you can do it directly, without using message passing.

If a script on a game object with id “foo” wants to get the position of game object with id “bar” then you do like this:

-- get position of the game object this script is attached to
local my_pos = go.get_position()

-- get the position of the game object with id "bar"
local bar_pos = go.get_position("bar")

The same goes for most of the functions in the go.* namespace. They can all take an optional other game object id. If the id is omitted you operate on the game object the script is attached to.

1 Like

if you’re a youtube person, these may be of use to you: