In the Defold v1.12 editor, is there a way to get built in lua types? For example, so that code completion works for things like:
local n = gui.get_node("xyz")
n._ -- want (correct) code completion here
If I look at the docs for gui.get_node() it says it returns an “instance node” - so I understand it returns an instance of a “node” but the editor does not seem to know that is what it is. This isn’t a node-specific question, but any “types” that are built in (publicly provided by the Defold engine).
Basically I want to be able to code in lua like I would in a compiled language like C or Dart or Java, etc. - so I can specify the explicit return type (rather than just userdata I want to know if it is a node or vmath.vector3 etc.). Another example:
local n = vmath.vector3()
n. -- here the code completion should be ".x", ".y", ".z", etc., but it's not
If you code in an editor like VSCode or other that have lua lsp-server and use lua annotations, you can have code autocompletion.
I use vscode extension defold-kit, search in the forum there is a thread about it.
It works like a charm.
I think the defold editor has a language server builtin, maybe annotations has to be configured somewhere…??
Yes there is. You can use the same kind of annotation file as in VSCode. Example of a partial annotation file of vmath:
vmath_api.lua:
---@meta
---@class vector3
---@field x number
---@field y number
---@field z number
vector3 = {}
---Functions and constants for working with vector math.
---@class defold_api.vmath
vmath = {}
---Create a vector3
---@return vector3 v3 The created vector3
function vmath.vector3() end
If I put this file in my project and then create a vector3 and try to access a member I get this:
Where would be the best place to even start working on something like this? What parts of the projects would need to be changed? I don’t think I can do much because of my RSI, but if it could help with third party editor support then it could be worth the pain.
@totebo lol, thanks for the reminder - I knew that about 3 wks ago when I read the docs, but forgot now as I came back to continue learning and actually work with it some more. Is it just me, or should the docs be considered incorrect and rather be something like `—@return integer The ID for the underlying (inaccessible) instance.`?
@Mathias Thanks! I did try the Zed editor with some extensions, and for that editor the vmath.vector and such docs worked find, however if I recall correctly the Defold provided lua code completions did not work, even after trying for several hours (various extensions etc. then working with it). The Zed editor was sub-par overall for Defold v1.12 work vs just the Defold v1.12 Editor.
I’ve tried VS Code repeatedly in the past and have never been satisfied…so I’ve not given it a shot yet for Defold projects. If it is the only working option, I will… but I’m not quite there yet, I think/hope
I’ve tried various old posts on getting better Lua code completion within the editor - but they didn’t seem to improve anything vs the default editor setup. My own Lua code docs work perfectly for code completion within the editor, automatically. So, I’m guessing if I simply added lua files with all the doc definitions I am interested in, that would probably work - does anyone have such a doc for Defold, or know offhand where to get one? (I’m taking a quick look right now… so far I’ve found gui_script.cpp so as a source to work from.)
At least for my projects, I am interested in addressing this issue - probably initially as a .lua file to add to a project, as I’m not ready for tackling understanding Defold extensions etc. yet, never mind c++… I have not worked in c++ yet, C yes, Object-C too, Java, etc. but c++ always seems to me like it really depends on who coded it and how - so one persons/projects c++ can be almost completely foreign to another person/project based on language features, libraries, coding conventions, etc. … so little hesitant to dig into that at present.
@britzl This may seem a little tangental, but it’s related to trying to get correct code completion in the editor; are these docs incorrect:
/*# gets the node with the specified id
*
* Retrieves the node with the specified id.
*
* @name gui.get_node
* @param id [type:string|hash] id of the node to retrieve
* @return instance [type:node] a new node instance
* @examples
*
* Gets a node by id and change its color:
*
* ```lua
* local node = gui.get_node("my_node")
* local red = vmath.vector4(1.0, 0.0, 0.0, 1.0)
* gui.set_color(node, red)
* ```
*/
static int LuaGetNode(lua_State* L)
{
//...shortened...
return 1
}
The return type is clearly int , not some instance of a node (nor an instance of any object-like thing - just a primitive).
For myself, coming from mostly doing static languages type programming, (for gaming related programming), rather than scripting, when I see a return type of instance I would automatically assume this is some form of object or perhaps struct (or in case of Lua, table) – certainly not an int (or in Lua an integer)… would that not be the most common understanding of returning an instance? So should this just say that it returns an int in c++ and that it returns an integer in Lua?
Thanks. Clearly there is stuff I just don’t understand about that .cpp file or how it is being used.
So, in Defold lua .gui_script file, the value returned here: local node = gui.get_node(hash(“some_name”)) – this node value is not actually an integer?
hehe, not trying to be rude - but it does return an int after all. It is an int that represents a “reference”… but it is an int. I guess I have to unlearn-old/learn-new… a “reference” in defold-land is not an “object” or a “table” or anything that can be treated as such. Seems unusual to me - is this normal terminology in Lua, or is it terminology and semantics specific to the “Defold domain”? Thanks again for your good answer - I read it but it’s kinda over my head atm
It’s not a “Defold” thing. A reference is just that, a reference, a number. If you conme from C++, it’s still just a number, an address.
And Lua objects are references, and the actual data type we use in C is an integer. Still, it’s still a reference to that object.