Module with prehashed message ids
As many of you know, it is a good practice to pre hash message ids, so for a generic solution prepare a module (e.g. msgs.lua) that just returns one table with key indexed hashes of message ids, like this for example:
return {
-- SYSTEM MESSAGES:
CONTACT = hash("contact_point_response"), -- defold generated message, sent on one point collision of kinematic and dynamic objects
COLLISION = hash("collision_response"), -- defold generated message, sent on collision
TRIGGER = hash("trigger_response"), -- defold generated message, sent on trigger collision
ANIM_DONE = hash("animation_done"), -- defold generated message, sent when a sprite animation is done
SET_PARENT = hash("set_parent"), -- defold message to change parent for a game object
RAY_CAST = hash("ray_cast_response"), -- defold message sent as a response to a raycast
WIN_RESIZE = hash("window_resized"), -- defold message sent on window resize event
ENABLE = hash("enable"), -- enable game object or componenet
DISABLE = hash("disable"), -- disable game object or component
-- EXAMPLE MESSAGES:
TOUCH = hash("touch"), -- popular input for mobile control
ANIM = hash("animate"), -- play an animation (anim)
STOP_ANIM = hash("stop_animation"), -- stop any animation
FLIP = hash("flip"), -- flip a sprite
REGISTER = hash("register"), -- register as a subsriber to receive all messages from a publisher
UNREGISTER = hash("unregister") -- deregister form subscription list
}
and then use it after requiring globally once or locally per script, for example:
msgs = require "main.msgs"
like this:
if message_id == msgs.ANIM_DONE then ... end
Don’t name your module msg (it is used by Defold, e.g. msg.url()). Don’t send DISABLE message and then try to do something with a component/go - I spend whole day on finding bug, where I send DISABLE message because I tried to make something BEFORE deleting the object and was wondering why it is not perfomed and object disappers ANIM_DONE message is only sent after a animation that is not looped - so never wait for a looped animation to be done - use another variable to lock animation and unlock it if needed. WIN_RESIZE is useful when you want to manage different windowed appplication or mobile with horizontal and vertical GUI - you have to manage changes on your own and on this event it is possible. If you have any other useful messages or ideas to unify message system, share it