Pass screensize to controller

Hello,
i’m new to defold (and lua) and i am creating a mobile app.
I need the screen size in my main controller. Therefore i set up a custom render script and inserted

msg.post("/main#controller", "screen", {height = render.get_height(), width = render.get_width()})

at the end of the init function.
However the on_message function of my main.script is never called.
my main.collection contains a game objection with the id “controller” which contains the main.script.

What am i doing wrong?

Hi,

You need to access the script and not the gameobject “controller”.

can you tell me how to do that?

So you have attached your script component to your gameobject then when click on your script, in the properties section you will see a URL path. Yours should be “/controller#main”

So in your render script msg.post() it will be:

msg.post("/controller#main", "screen", {height = render.get_height(), width = render.get_width()})

Where controller is the id of the gameobject and main the id of the script.

You can read up on addressing here

ok, i copied the url “/controller#main” from the collection(yours contained a typo), but the on_message still does not get called. i have a print right at the start of it and it doesn’t appear in the console

The good tip is that you can always check empirically the current script url by simply printing msg.url() in init() - this will print out the current script’s url at the beginning :wink:

3 Likes

What does the URL say when you do a print for the url on the script’s init function?

print(msg.url())
1 Like