Skip to main content

PuerTS 3.0 Multi-Language Comparison Cheat Sheet

This table summarizes the syntax differences between JavaScript, Lua, and Python when interacting with C# in PuerTS, for quick reference.

๐Ÿ“– Detailed tutorials for each language: JS | Lua | Python


1. Environment Creationโ€‹

LanguageC# Code
JavaScriptvar env = new ScriptEnv(new BackendV8());
Luavar env = new ScriptEnv(new BackendLua());
Pythonvar env = new ScriptEnv(new BackendPython());

All three languages share a unified ScriptEnv + Backend architecture, differing only in the Backend type.


2. C# Namespace / Type Accessโ€‹

LanguageSyntax
JavaScriptCS.UnityEngine.Vector3 โ€” access directly via the global CS object
Lualocal CS = require('csharp') then CS.UnityEngine.Vector3
Pythonimport UnityEngine.Vector3 as Vector3 or puerts.load_type('UnityEngine.Vector3')

3. Object Creationโ€‹

LanguageExample
JavaScriptlet v = new CS.UnityEngine.Vector3(1, 2, 3);
Lualocal v = CS.UnityEngine.Vector3(1, 2, 3) โ€” no new
Pythonv = Vector3(1, 2, 3) โ€” no new

4. Instance Method Callsโ€‹

LanguageExampleNotes
JavaScriptrect.Contains(point)Dot syntax
Luarect:Contains(point)Colon syntax (auto-passes self)
Pythonrect.Contains(point)Dot syntax

5. Static Method Callsโ€‹

LanguageExample
JavaScriptCS.UnityEngine.Debug.Log('msg')
LuaCS.UnityEngine.Debug.Log('msg') โ€” dot syntax
PythonDebug.Log('msg') โ€” requires import first

In Lua, static methods use dot ., instance methods use colon :; JS and Python uniformly use dot.


6. Property Read/Writeโ€‹

LanguageReadWrite
JavaScriptlet w = rect.widthrect.width = 0.1
Lualocal w = rect.widthrect.width = 0.1
Pythonw = rect.widthrect.width = 0.1

Property read/write syntax is identical across all three languages, all using dot notation.


7. ref/out Parametersโ€‹

LanguageCreate ContainerPass as ArgumentGet Result
JavaScriptlet p = puer.$ref(initialValue)Func(p)puer.$unref(p)
Lualocal p = {initialValue} or {}Func(p)p[1]
Pythonp = [initialValue] or [None]Func(p)p[0]

Comparison example (out int b, ref int c):

// JavaScript
let outB = puer.$ref();
let refC = puer.$ref(10);
Example.InOutArgFunc(100, outB, refC);
console.log(puer.$unref(outB), puer.$unref(refC)); // 100, 20
-- Lua
local outB = {}
local refC = {10}
CS.Example.InOutArgFunc(100, outB, refC)
print(outB[1], refC[1]) -- 100, 20
# Python
outB = [None]
refC = [10]
Example.InOutArgFunc(100, outB, refC)
print(outB[0], refC[0]) # 100, 20

8. Generic Type Creationโ€‹

LanguageGeneric Class NameCreating List<int>
JavaScriptList$1puer.$generic(CS.System.Collections.Generic.List$1, CS.System.Int32)
LuaList_1puerts.generic(CS.System.Collections.Generic.List_1, CS.System.Int32)
PythonList__T1 (import) or List`1 (load_type)puerts.generic(List, System.Int32)

The backtick ` substitute differs across languages: JS uses $, Lua uses _, Python import uses __T.


9. Generic Method Callsโ€‹

LanguageSyntax
JavaScriptCall directly, V8 auto-infers type parameters
Luapuerts.genericMethod(Type, 'MethodName', GenericArgs...)
Pythonpuerts.genericMethod(Type, 'MethodName', GenericArgs...)
-- Lua example
local func = puerts.genericMethod(CS.MyClass, 'MyMethod', CS.System.Int32)
func(obj)

10. typeofโ€‹

LanguageSyntax
JavaScriptpuer.$typeof(CS.UnityEngine.ParticleSystem)
Luarequire('puerts').typeof(CS.UnityEngine.ParticleSystem)
Pythonpuerts.typeof(ParticleSystem)

11. null Representationโ€‹

LanguageScript-side nullNotes
JavaScriptnull / undefinedStandard JS null values
LuanilStandard Lua null value
PythonNoneStandard Python null value

12. Callbacks / Lambdaโ€‹

LanguageExample
JavaScriptobj.Callback = (msg) => { console.log(msg); }
Luaobj.Callback = function(msg) print(msg) end
Pythonobj.Callback = lambda msg: print(msg)

Getting a script function as a delegate on the C# side:

// Same for all three languages
Action<string> fn = env.Eval<Action<string>>("script code");
fn("hello");

13. Throwing Exceptionsโ€‹

LanguageSyntax
JavaScriptthrow new Error('msg')
Luaerror('msg')
Pythonraise Exception('msg')

Exceptions thrown from the script side are wrapped as corresponding exception types on the C# side, catchable via try/catch.


Appendix: Key API Reference Tableโ€‹

FeatureJavaScript (puer.xxx)Lua (require('puerts').xxx)Python (puerts.xxx)
Generic typepuer.$generic()puerts.generic()puerts.generic()
Generic methodโ€” (auto-inferred)puerts.genericMethod()puerts.genericMethod()
typeofpuer.$typeof()puerts.typeof()puerts.typeof()
ref/out createpuer.$ref(){} (table)[] (list)
ref/out retrievepuer.$unref()[1][0]
async/awaitpuer.$promise(task)โ€”โ€”

๐Ÿ“– Detailed tutorials for each language: