Render Script Error? (SOLVED)

I keep getting this error:

ERROR:SCRIPT: /main/LWGrab.lua:3: bad argument #2 to ‘get_window_width’ (RenderScriptInstance expected, got userdata)

When I run this code in a module script:

screenWidth = render.get_window_width()
screenHeight = render.get_window_height()

1 Like

The render.* function can only be called from a .render_script file. If you want access to render.get_window_width() you need to store that value in a Lua module (or a global variable). The same rule applies to go.* (.script files) and gui.* (.gui_script files).

1 Like

Awesome.

1 Like

Maybe an old thread, but are there any good examples how to do this? I just want to get the width and height setting to place stuff on the screen. Can’t understand it’s this hard? (Don’t know how to include a render script, or work with Lua modules either, so this answer doen’t help me much :confused: )

“SOLVED” it by using global variables. But was way to complicated :confused: :

  1. Dublicating the default render script (builtins/render/default.render_script) and the default renderer (builtins/render/default.render)
  2. In the new .render file, chose your own render script
  3. In game.project, choose your new render under Bootstrap > Render
  4. Now you can add global variables in your render script like this:

G_WIDTH = 100;
G_HEIGHT = 100;

function init(self)
G_WIDTH = render.get_width();
G_HEIGHT = render.get_height();

1 Like

You can find the default.render_script file under builtins/render. To modify it, make a copy elsewhere and make changes to that.

You also need to make a copy of the default.render file you’ll find with it. Point the copy of the default.render file towards your new render_script file and then engine towards the render file in your game.project file (it’s under “Bootstrap”).

As for lua modules, read up on them here. It feels like they are an answer to half the problems posted here =D

Edit: Ah, you got it. Good job!

1 Like

Haha thanks anyway :smiley: But, yeah I guess I should read more about Lua modules. But don’t use Defold as my main engine, so just want to do quick stuff and itterate fast (read bad code, but that works, but does not scale) :man_shrugging:

Lua modules are a powerful tool, even though most of my use cases boil down to “I want to use a global variable but was told that I shouldn’t”.

BTW, you might need this too, to know if the window size changes.

1 Like

Haha true :smiley:

Ah, thank you!

I will try to add the missing functionality to the window.* namespace of functions as soon as I have some time.

1 Like