Skip to content

Commit

Permalink
WIki Documentation update (Sync with API Repo) (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michieal authored Oct 6, 2023
1 parent 1995bbc commit 2a46266
Show file tree
Hide file tree
Showing 7 changed files with 280 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
site/
*.sh
*.ps1
*.ps1
/.idea/
86 changes: 86 additions & 0 deletions docs/examples/dotnet/example1.md
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);
}
}
```
4 changes: 2 additions & 2 deletions docs/examples/objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Normally, when using `push_variant()` or the return value of a GDScript function
!!! info "In the background..."
In order to call functions and get values from the object we assign the mt_Object metatable to it. This is a custom metatable we defined for Objects. This allows for methods to be called on the object, values to be get/set on the Object and much more.
### With the default object metatable
By default every field and method is available to Lua. You can limit what Lua has access to by defining either the `lua_fields()` method, which must return an Array of the names of fields/methods. By default this array is treated as a blacklist of methos. If you set [permissive](../classes/lua_default_object_metatable.md#permissive-bool) to `false`, it will be treated as a whitelist instead. The other way to limit access is by defining the `__index()` method, which takes reference to the [LuaAPI](../classes/lua_api.md) object and a string of the field being requested by Lua. You can then return any value you please to lua.
By default every field and method is available to Lua. You can limit what Lua has access to by defining either the `lua_fields()` method, which must return an Array of the names of fields/methods. By default this array is treated as a blacklist of methods. If you set [permissive](../classes/lua_default_object_metatable.md#permissive-bool) to `false`, it will be treated as a whitelist instead. The other way to limit access is by defining the `__index()` method, which takes reference to the [LuaAPI](../classes/lua_api.md) object and a string of the field being requested by Lua. You can then return any value you please to lua.

This is all made possible by the [LuaDefaultObjectMetatable](../classes/lua_default_object_metatable.md) assigned to the `object_metatable` field on the [LuaAPI](../classes/lua_api.md) object by default. For even more control, you can define your own object metatable. See the [LuaObjectMetatable](../classes/lua_object_metatable.md) class.

Expand Down Expand Up @@ -147,4 +147,4 @@ func _ready():
print(player.pos)
```

---
---
92 changes: 92 additions & 0 deletions docs/getting_started/dotnet.md
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).
47 changes: 47 additions & 0 deletions docs/getting_started/export.md
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.)
48 changes: 48 additions & 0 deletions docs/getting_started/getting_started.md
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)
4 changes: 3 additions & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ nav:
- Getting Started:
- getting_started/before_you_start.md
- getting_started/installation.md
- getting_started/getting_started.md
- Classes:
- classes/lua_api.md
- classes/lua_coroutine.md
Expand All @@ -35,6 +36,7 @@ nav:
- classes/lua_default_object_metatable.md
- Examples:
- examples/objects.md
- examples/dotnet/example1.md
- Contributing:
- contributing/ways_to_contribute.md
- contributing/development_environment.md
Expand Down Expand Up @@ -67,4 +69,4 @@ theme:
scheme: slate
toggle:
icon: material/brightness-4
name: Switch to light mode
name: Switch to light mode

0 comments on commit 2a46266

Please sign in to comment.