Calling Message API function from within receiver

Hey everyone,

I just started using Defold and Lua and browsing the documentation I haven’t been able to understand one thing about message passing: who are the reicever of the API default messages such as draw_line?

Suppose I am writing a custom renderer which is able to respond to draw_rect by drawing the four sides of a rectangle using four draw_line calls .
How would I do it?
Both

elseif message_id == hash("draw_rect") then self.draw_line(message.rect, {start_point = message.rect.bottomLeft, end_point = message.rect.bottomRight, color = message.color })

and

elseif message_id == hash("draw_rect") then draw_line(message.rect, {start_point = message.rect.bottomLeft, end_point = message.rect.bottomRight, color = message.color })draw_line

rise a nil exception on draw_line, so I assume that the function is neither global or indexed on the renderer metatable.

Would I have to post four draw_line messages from the renderer to the engine?

Thanks in advance for your answer!

I have no idea about the technical details, but as far as I know you don’t have access to those underlying functions. You will have to send four draw_line messages. You can use the "@render:" address to send messages to the render script from any other script, but the “draw_line” and “draw_text” messages seem to be exceptions—at least in my test, they never get to the render script.

2 Likes

You can build helper function to draw the rect so it’s more convenient. Doing one message per line is still very fast.

2 Likes

Thanks for your replies!

I actually managed to draw the rectangle by posting a custom draw_rect message to the render.script and then posting 4 draw_line from the render.script to “itself”.

Now that I think about it it makes sense though: draw calls such as draw_line must be called on the update lifecycle event and polling messages is a convenient way to synchronize and batch draw requests from the Lua code with the engine rendering pipeline.

2 Likes