-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIki Documentation update (Sync with API Repo) (#13)
- Loading branch information
Michieal
authored
Oct 6, 2023
1 parent
1995bbc
commit 2a46266
Showing
7 changed files
with
280 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
site/ | ||
*.sh | ||
*.ps1 | ||
*.ps1 | ||
/.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
Godot Lua API C# Examples | ||
=============== | ||
|
||
All future examples will be in the [Wiki](https://luaapi.weaselgames.info/latest). | ||
|
||
Getting Started Example (In C#) | ||
------- | ||
|
||
In this example, we recreate the main GDScript example from the README.md file. I have fully commented this | ||
example, to explain what each part does. Specific differences and a special note on this example: In C# you | ||
cannot assign a Method to a `Variant` for use with `LuaAPI.PushVariant`. So, to get around that, and make the | ||
example work, we will first wrap our called function in a `Callable` variable (wrapper). Please note the line | ||
`Callable print = new Callable(this, MethodName.LuaPrint);` in the example below. | ||
|
||
```csharp | ||
using Godot; | ||
|
||
public partial class Node2D : Godot.Node2D { | ||
private LuaApi lua = new LuaApi(); | ||
|
||
public void LuaPrint(string message) { | ||
GD.Print(message); | ||
} | ||
|
||
public override void _Ready() { | ||
GD.Print("Starting Node2D.cs."); | ||
|
||
// All builtin libraries are available to bind with. Use OS and IO at your own risk. | ||
// BindLibraries requires a "Godot Array" so, let's build one. | ||
Godot.Collections.Array libraries = new Godot.Collections.Array(); | ||
libraries.Add("base"); // Base Lua commands | ||
libraries.Add("table"); // Table functionality. | ||
libraries.Add("string"); // String Specific functionality. | ||
lua.BindLibraries(libraries); // Assign the specified libraries to the LuaAPI object. | ||
// In C#, .PushVariant does not work with Methods, so we use Callable to wrap our function. | ||
Callable print = new Callable(this, MethodName.LuaPrint); | ||
// Assign the Callable, so that the API can call our function. | ||
// Note, the lua function "cs_print" is now callable within Lua script. | ||
lua.PushVariant("cs_print", print); | ||
// Assign a Lua Variable named "message" and give it a value. | ||
lua.PushVariant("message", "Hello lua!"); | ||
|
||
// Use .DoString() to execute our Lua code. | ||
LuaError error = lua.DoString("cs_print(message)"); | ||
// Check for errors, and if there are any, Print them to the Godot Console. | ||
if (error != null && error.Message != "") { | ||
GD.Print("An error occurred calling DoString."); | ||
GD.Print("ERROR %d: %s", error.Type, error.Message); | ||
} | ||
|
||
error = lua.DoString(@" | ||
for i=1,10,1 do | ||
cs_print(message) | ||
end | ||
function get_message() | ||
return ""This message was sent from 'get_message()'"" | ||
end | ||
"); | ||
|
||
// Check for errors, and if there are any, Print them to the Godot Console. | ||
if (error != null && error.Message != "") { | ||
GD.Print("An error occurred calling DoString."); | ||
GD.Print("ERROR %d: %s", error.Type, error.Message); | ||
} | ||
|
||
var val = lua.PullVariant("get_message"); | ||
|
||
// Check to see if it returned an error, or a value. | ||
if (val.GetType() == typeof(LuaError)) { | ||
GD.Print("ERROR %d: %s", error.Type, error.Message); | ||
return; | ||
} | ||
|
||
// LuaAPI.CallFunction requires a Godot.Collections.Array as the container | ||
// for the parameters passed in, for the lua function. | ||
Godot.Collections.Array Params = new Godot.Collections.Array(); | ||
|
||
// We use .CallFunction to actually call the lua function within the Lua State. | ||
var message = lua.CallFunction("get_message", Params); | ||
// And, finally, we log the output of the function to Godot Output Console. | ||
GD.Print(message); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
Godot Lua API DotNet Notes | ||
=============== | ||
|
||
<!-- TOC --> | ||
* [Godot Lua API DotNet Notes](#godot-lua-api-dotnet-notes) | ||
* [Introduction](#introduction) | ||
* [Commands](#commands) | ||
* [Example Notes](#example-notes) | ||
<!-- TOC --> | ||
|
||
Introduction | ||
------- | ||
|
||
When working with this version of the editor, or the extension addon, you will need to use specific versions of the | ||
nuget packages for your project. This has been tested with v2.1-beta6. | ||
|
||
For this document, we will use *nix style paths. If you are a Windows user, please convert lines like `/path/to/` to | ||
`C:\path\to\` so that it will work with your system. MacOS users can use the *nix paths. | ||
|
||
The editor builds have a file named `mono_instructions.txt` included in the zipped release. | ||
The contents of this file are as follows: | ||
|
||
``` | ||
If you have GodotSharp locally cached dotnet may use one of them instead. To clear local cache run the following command: | ||
dotnet nuget locals all --clear | ||
To add the local nuget source, please run the following command: | ||
dotnet nuget add source /path/to/nuget_packages/ --name LuaAPINugetSource | ||
``` | ||
|
||
What this is saying is that you will want to open up a command prompt and do the following steps to install the correct | ||
nuget packages into your project. If you have downloaded the editor zip file, it comes with a directory named `nuget_packages` | ||
This is the directory that you will want to include into the second step in the instructions file. So, if you have extracted | ||
the Editor to `C:\Users\username\Godot` the correct path to use would be `C:\Users\username\Godot\nuget_packages`. | ||
Likewise, if you extracted the editor to `/home/username/Godot/` you would want to use `/home/username/Godot/nuget_packages`. | ||
|
||
Make sure to enclose directories with spaces with either a `"` or `'` so that the path resolves correctly. | ||
|
||
Additionally, you will most likely want to execute a `dotnet restore` command to install the correct nuget packages. See | ||
Commands below for an example on how to do this. | ||
|
||
Commands | ||
------- | ||
|
||
In your command prompt, execute these commands in the following order. | ||
|
||
`dotnet nuget locals all --clear` | ||
|
||
`dotnet nuget add source /path/to/nuget_packages/ --name LuaAPINugetSource` | ||
|
||
`dotnet restore '/pathtoproject/example_project.csproj' -f -s LuaAPINugetSource` | ||
|
||
This will set up the proper packages to work with the Editor / Add-on. Note: you may have to select the correct nuget | ||
source within your IDE. If so, please use the `LuaAPINugetSource` option. Note that in the third command, we are using | ||
the specific location (`-s <source>`) and we are forcing (`-f`) the restore. This is done to specifically use the custom | ||
nuget packages. | ||
|
||
Once you have done this, you will need to rebuild your project. You can do so either through your IDE or inside of the | ||
Godot Editor. | ||
|
||
Example Notes | ||
------- | ||
|
||
As you can expect, the method names in the LuaAPI class are in Dotnet style. So, something like `LuaAPI.push_variant` | ||
would be `LuaAPI.PushVariant` | ||
|
||
Starter Example: | ||
```csharp | ||
using Godot; | ||
using System; | ||
|
||
public partial class Node2D : Godot.Node2D | ||
{ | ||
// Called when the node enters the scene tree for the first time. | ||
public override void _Ready() | ||
{ | ||
GD.Print("Hello from C#"); | ||
LuaApi lua = new LuaApi(); | ||
lua.DoString("print(\"Hello from Lua\")"); | ||
} | ||
} | ||
``` | ||
Note that `lua.DoString()` is the dotnet version of `lua.do_string()`. | ||
|
||
The above starter example will produce its output into the Godot Output window in the Godot Editor. Here is the exact | ||
output it produces. | ||
``` | ||
Hello from C# | ||
Hello from Lua | ||
``` | ||
|
||
More [examples](csexamples%2FEXAMPLE1.md) will be included very soon (see Wiki). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Godot Lua API Installing Export Templates | ||
=============== | ||
|
||
<!-- TOC --> | ||
* [Godot Lua API Installing Export Templates](#godot-lua-api-installing-export-templates) | ||
* [Notes](#notes) | ||
* [How To Use](#how-to-use) | ||
<!-- TOC --> | ||
|
||
Notes | ||
------- | ||
You will need to download and install the templates from the releases, every time you update to a new release. | ||
To do so, go to the [Releases](https://github.com/WeaselGames/godot_luaAPI/releases) section on the GitHub repository and | ||
download the appropriate Export Templates package for your build. For example, if you are using the `v2.0.2-stable` | ||
release (current as of the writing of this document), you will want to download `export-templates.tpz` from the `Assets` | ||
section. This file, and its extracted contents will be discussed in this document. | ||
If you are using the DotNet (C#) Mono build, then you will want to download the `export-templates-mono.zip`. Go ahead and | ||
download these now, if you have not done so already. | ||
|
||
How To Use | ||
------- | ||
|
||
Now on how to use them, under releases we have 2 export template options. Use `export-templates` for the normal engine | ||
(GDScript only) builds without mono. These support all platforms. Then `export-templates-mono` is of course for Dotnet / | ||
Mono. Since Godot C# has low platform support right now, these templates only support Linux, Windows and MacOS. | ||
|
||
Inside the zip files you will see executables for each platform and architecture that is supported. There will be 2 for | ||
each supported combination, Release and Debug. Debug templates are to allow you to debug the exported project using | ||
the Godot debugging tools. The Release version has that all of the debugging code stripped out to optimize the final | ||
Release build. | ||
|
||
Here is where to find the Project Export profiles: | ||
![projexpmenu.png](.github%2FDocumentationImages%2Fprojexpmenu.png) | ||
|
||
You will want to either update your existing Project Export profiles, or make new ones. When creating or updating the | ||
export profile in the editor, You will see where it has you select the platform and architecture. To make use of our | ||
templates you need to set the file path for both the debug and release templates in the fields provided. Those fields | ||
are labeled Debug and Release in the Custom Templates section. (See image.) | ||
|
||
![exportwin.png](.github%2FDocumentationImages%2Fexportwin.png) | ||
|
||
Use the folder icon next to the two fields to select the template that matches with the `Architecture` and the | ||
`Custom Template` selection. In the case of what is shown in the image, we are using `Linux` and `x86_64`. So, we will | ||
select the location and file, based on where you extracted the the templates zip file that you downloaded above. | ||
(See Notes.) In this case, we will be using `linux_debug.x86_64` and `linux_release.x86_64`. | ||
|
||
You can now export your project or close the window. (The Project Export settings are saved automatically.) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
Getting Started | ||
--------------- | ||
|
||
Pro Tip: Be sure to download the Export Templates! The Export Templates that come with Godot will not work with this | ||
Editor/Add-on. Please see [Installing Export Templates](export.MD) for more information. | ||
|
||
Dotnet users will also want to refer to [dotnet.md](dotnet.md) for additional information and steps that are needed to | ||
make the Editor/Add-on work. | ||
|
||
**Running Lua for you first time:** | ||
```gdscript | ||
extends Node | ||
var lua: LuaAPI = LuaAPI.new() | ||
func _lua_print(message: String) -> void: | ||
print(message) | ||
func _ready() -> void: | ||
lua.push_variant("print", _lua_print) | ||
lua.push_variant("message", "Hello lua!") | ||
# All builtin libraries are available to bind with. Use OS and IO at your own risk. | ||
lua.bind_libraries(["base", "table", "string"]) | ||
# Most methods return a LuaError in case of an error | ||
var err: LuaError = lua.do_string(""" | ||
for i=1,10,1 do | ||
print(message) | ||
end | ||
function get_message() | ||
return "Hello gdScript!" | ||
end | ||
""") | ||
if err is LuaError: | ||
print("ERROR %d: %s" % [err.type, err.message]) | ||
return | ||
var val = lua.pull_variant("get_message") | ||
if val is LuaError: | ||
print("ERROR %d: %s" % [val.type, val.message]) | ||
return | ||
var message = val.call([]) | ||
print(message) | ||
``` | ||
|
||
Dotnet (mono) users see this [example redone in C#](..%2Fexamples%2Fdotnet%2Fexample1.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters