gilzoide / godot-lua-pluginscript

Godot PluginScript for the Lua language, currently based on LuaJIT's FFI
https://gilzoide.github.io/godot-lua-pluginscript/topics/README.md.html
MIT License
308 stars 21 forks source link

'self' argument not passed to GDScript automatically #12

Closed Alikae closed 2 years ago

Alikae commented 2 years ago

Hi @gilzoide.

Firstly, I would like to thank you for your work.

While trying to call a GDScript method from a Lua script, the owner of the method is not passed as first argument and need to be given manually.

Sample Code:

Test.gd:

extends Node

func _ready(): var lua_script = load("res://LuaTest.lua") lua_script.ltest(self)

func test(): print('It worked !')`

LuaTest.lua:

`local LuaTest = { extends = "Node", }

function LuaTest:ltest(parent) parent.test() -- The code work if we pass parent as argument here end

return LuaTest`

Corresponding error: E 0:00:00.490 test: attempt to index local 'o' (a nil value)

Is there something I missed on how it's supposed to work ? Thanks by advance,

bojjenclon commented 2 years ago

Hi, it looks like you may be calling the method incorrectly. I see you're using dot notation - e.g. parent.test() in your Lua file. For Lua "objects" you should actually use a colon, like so parent:test(). This will automatically pass in self.

In essence, parent.test would look for a class wide method whereas parent:test would look for an instance method

Alikae commented 2 years ago

Thanks. Look like I need to dive a little more in Lua. :pray:

gilzoide commented 2 years ago

Yeah, Lua is a little bit different than most programming languages. The next thing I want/need to do is a document like "From GDScript to Lua" showing these differences. Sorry that it isn't in place yet.


Not related, but if want, you can make highlighted code blocks with triple backticks and the language name.

For example, writing:

```gdscript
extends Node

func _ready():
    var lua_script = load("res://LuaTest.lua")
    lua_script.ltest(self)

func test():
    print('It worked !')`
shows like
```gdscript
extends Node

func _ready():
    var lua_script = load("res://LuaTest.lua")
    lua_script.ltest(self)

func test():
    print('It worked !')`