Jump to content

Leadwerks 5 Scripting


Josh
 Share

Recommended Posts

I am considering an extendable Virtual Machine in Leadwerks 5 with official support for one language and the ability to add your own (Python, etc.)

The two contenders are Lua and Squirrel.

The features we need are the following:

  • Autocompletion with knowledge of Leadwerks API and Leadwerks C++ types.
  • Debugging.
  • Exposing C++ classes to script with support for smart pointers.

We may use an external IDE this time, or we may use our own built-in one. Visual Studio Code might be a good option, and it is available for all platforms.

We need to dig into the details of this stuff and see how we can accomplish what we want. Any input you have on this is appreciated.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I would think the majority of the time spent is coding gameplay with Lua vs shader coding. Wouldn't want to gimp the majority one for the minority one.

At first glance a virtual machine of your own starts going down rabbit holes that seems dangerous. On the other hand if you supported languages like Python, C#, etc I bet sales would increase. Coders are picky about their language and like what they like. If they have the option to use their language as officially supported vs community supported people would like that. I don't personally like Python (white space as meaning? f that) but it's the fastest growing language currently so a lot of people know it and like it.

Squirrel is interesting. I've never used it but most anything that has the class keyword I like and feel comfortable with.

Link to comment
Share on other sites

I will only support one script language. I don't mind leaving the door open for other languages, if I can do it without hurting the official supported one.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

You can add this into your Lua.json file and it sort of give you autocompletion:

{
	"Entity:SetPosition": {
		"prefix": "SetPosition",
		"body": ["SetPosition($x,$y,$z,$global)"],
		"description": "Entity\nSets an entity's position in 3D space"
	},
	"Entity:SetRotation": {
		"prefix": "SetRotation",
		"body": ["SetRotation($pitch,$yaw,$roll,$global)"],
		"description": "Entity\nSets an entity's rotation in 3D space"
	},
	"Window:Create": {
		"prefix": "Window:Create",
		"body": [ "Window:Create($title,$x,$y,$width,$height,$style)"],
		"description": "Window\nCreates a new Window object"
	},
	"Window.Titlebar": {
		"prefix": "Window.Titlebar",
		"body": ["Window.Titlebar"]
	}
}

Image1.jpg

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

At this time I am leaning towards this solution:

  • Lua
  • Custom binding code implemented for smart pointers.
  • No built-in IDE
  • VS Code as official script IDE.
  • A single Leadwerks tools plugin for VS Code that installs everything you need.
  • New debugger that runs in VS Code, based on our existing Lua debugging system.
  • Shader files separated into vert, frag, geom, etc. files.
  • New VS Code plugin that validates and links shaders for testing.
  • Autocomplete based on snippets, as shown above.
  • Like 2
  • Thanks 1
  • Upvote 2

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

2 hours ago, AggrorJorn said:

I will post my extension for vs code this evening. 

What does it do?

Here is some improved Lua autocomplete. It would be totally possible to auto-generate the entire API by pulling the data from the XML files the docs are stored in:

{
	"Window:Create": {
		"prefix": "Window:Create",
		"body": "Window:Create($title,$x,$y,$width,$height,$style)",
		"description": "Window Function\nCreates a new Window object"
	},
	"Window.Titlebar": {
		"prefix": "Window.Titlebar",
		"body": "Window.Titlebar",
		"description": "Constant"
	},
	"Entity:SetPosition": {
		"prefix": "SetPosition",
		"body": "SetPosition($x,$y,$z,$global)",
		"description": "Entity Method\nSets the position of an entity in 3-dimensional space, using local or global coordinates."
	},
	"Entity:SetPosition (2)": {
		"prefix": "SetPosition",
		"body": "SetPosition($position,$global)",
		"description": "Entity Method\nSets the position of an entity in 3-dimensional space, using local or global coordinates."
	},
	"Entity:SetRotation": {
		"prefix": "SetRotation",
		"body": "SetRotation($pitch,$yaw,$roll,$global)",
		"description": "Entity Method\nSets the rotation of an entity in 3-dimensional space, using local or global coordinates."
	},
	"Source:SetPosition": {
		"prefix": "SetPosition",
		"body": "SetPosition($position)",
		"description": "Source Method\nSets the position of a source in 3-dimensional space, using global coordinates."
	},
}

It's not perfect. There is still no knowledge of what class methods can be used on what variable, but I think it is within the realm of acceptable support:

Image1.thumb.jpg.f304d1b17feac42e0c514d1eca62a9d6.jpg

Image2.thumb.jpg.07bca548d0f6918989efa0b3a6a841ca.jpg

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I would also like to attach fields and functions directly to the C++ object instead of having self.entity and entity.script:

window = Window:Create()
context = Context:Create(window)
world = World:Create()

model = Model:Box()

child = Model:Cylinder()
child.health = 100
child:SetParent(model)

function child:TakeDamage(damage)
	self.health = self.health-damage
end

componenttable[child]=child
child = nil

collectgarbage(collect)

thing = model:GetChild(0)

thing:SetPosition(1,2,3)
thing:TakeDamage(10)
print(thing.health)

It will take some work to figure out the memory collection issues.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Ah cool, you know how to make an extension.

You can also do hints / default values like this (You might have to remove the quotation marks though:

Script.$varname = "$value" --string "$label"

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

2 hours ago, Josh said:

It would be totally possible to auto-generate the entire API by pulling the data from the XML files the docs are stored in:

You could generate it and store it inside the extension and additionally let the extension look for the online version every time you start VS code. That way you have an offline version of the API at your disposal and when you go online, you get the latest documentation.

Link to comment
Share on other sites

Okay, so you are in the Leadwerks 5 editor. You want to run the current map that is open in the editor, in debug mode. You press F5.

How does Leadwerks Editor tell VS Code to run the Lua debugger and launch the current map?

?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

I found you can add a space at the end of a snippet name to make it unique, and it will still look the same (for function overloads):

{
	"table.insert": {
		"prefix": "table.insert",
		"body": "table.insert($table,$value)",
		"description": "Inserts a value into a table at the end of the table."
	},
	"table.insert ": {
		"prefix": "table.insert",
		"body": "table.insert($table,$position,$value)",
		"description": "Inserts a value into a table at the given position."
	}
}

Image1.jpg.95c5541d8db911849fd99104569929ad.jpg

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

CLI Options:
https://code.visualstudio.com/docs/editor/command-line

This will open main.lua and add your project folders to the workspace:

"C:\Program Files\Microsoft VS Code\Code.exe" -r "%UserProfile%\Documents\Leadwerks\Projects\MyGame\Scripts\Main.lua" "%UserProfile%\Documents\Leadwerks\Projects\MyGame\Scripts" "%UserProfile%\Documents\Leadwerks\Projects\MyGame\Shaders" "%UserProfile%\Documents\Leadwerks\Projects\MyGame\Source"
pause

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

2 hours ago, Josh said:

Okay, so you are in the Leadwerks 5 editor. You want to run the current map that is open in the editor, in debug mode. You press F5.

How does Leadwerks Editor tell VS Code to run the Lua debugger and launch the current map?

?

If you make an LE extension maybe the extension will always create a local server from VS Code to do the communicating between?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...