Message Post (SOLVED)

I have the following module called functions and a controller script:

Functions (Module):

local M = {}

M.message_post = function(receiver, id)
  msg.post(receiver, id)
end

return M

Controller (Script):

local functions = require("lib.functions")

function init(self)
  functions.message_post("receiver here", "id here")
end

Is it good practice just to reuse a function like message_post instead of using msg.post() the whole time in my game.

Because your wrapper function does not do any extra work, I would suggest just using msg.post().
In my game, if I need to do a bunch of calculations or do stuff before I post a message, I throw a lot of that into a function, but still call the raw msg.post(). It really just depends on what your use case is.

1 Like

If you are using msg.post() is it better to use prehashed string from a module or just a string?

I don’t know if there is a performance benefit, but I personally am going to be moving all of my message strings/hashes to a Lua module to use as a constants file. When I store them, I am going to be using pre-hashed strings because everytime you interact with the Defold engine, it gives you back the hashed versions, so I might as well both send and receive hashed strings to keep consistency.

3 Likes

Yes, it’s better to pre-hash your strings instead of hashing them every frame. You can define them at the top of your file like

local action_touch = hash("touch")

Or do like @gamzwithjamz is doing and put them into a module.

You can use something like this to make get/set automatic. It calculates the hash the first time you request it then reuses that hash every time you call it after.

2 Likes

What about properties such as scale, position of go.animate. Should you also hash it?

It would make the most sense for values that are used often, perhaps every frame, such as action ids for input or collision messages. Values used less frequently wouldn’t be as important to hash.

3 Likes