Error calling get_window_width()

I’m getting:

ERROR:SCRIPT: backgrounds/battlefield/segment_factory.script:17: bad argument #2 to 'get_window_width' (RenderScriptInstance expected, got userdata)
stack traceback:
	[C]: in function 'get_window_width'
	backgrounds/battlefield/segment_factory.script:17: in function <backgrounds/battlefield/segment_factory.script:16>

In:

function update(self, dt)
	local window_width = render.get_window_width()
end

On:
OS X El Capitan 10.11.2

Running Defold:
Version 1.2.72
(181f1cd0d18716abc26524eb83444ec3b1c3455e)
Java 1.8.0_05

EDIT: Changed from Bug to Question to minimize confusion.

You can only use the render object from within a render script. If you need access to the render.get_window_width/height() you can do this in at least two ways:

1: Post to the render script and have it post back it’s dimensions:

segment_factory.script:

msg.post("@render:", "get_dimensions")

your render script:

function on_message(self, message_id, message, sender)
  if message_id == hash("get_dimensions") then
     msg.post(sender, "window_dimensions", { width = render.get_window_width(), height = render.get_window_height() })
  end
end

2: Let your render script put get_window_width/height in a shared Lua module that can be accessed from segment_factory.script

3 Likes

@britzl Thank you - that worked like a charm!

Note, the receiver url should read “@render:” instead of “@render”.

True, I’ve edited my answer.

Sorry for the nitpick but the msg.post specifies “get_dimes t ions” rather than “get_dimensions”.

Thanks! I guess it’s shame on me for not running the code before posting it here :slight_smile: