Lua and C# Comparison

ParaSpace uses Lua as the scripting language for game worlds and gameplay content creation. ParaSpace uses UnityEngine for development and building. Unity engine uses C# as the development language, and ParaSpace maps the APIs provided by UnityEngine to Lua. ParaSpace provides API documents using C# function signature methods, and the message notification function uses Lua function declaration methods with type annotations.

Static function declarations in C# and use in Lua

For example: For type UnityEngine.GameObject, there are 4 static methods in Unity's official documents, as shown below:

CreatePrimitiveCreates a game object with a primitive mesh renderer and appropriate collider.
FindFinds a GameObject by name and returns it.
FindGameObjectsWithTagReturns an array of active GameObjects tagged tag. Returns empty array if no GameObject was found.
FindWithTagReturns one active GameObject tagged tag. Returns null if no GameObject was found.

Their declaration forms are as follows:
public static GameObject CreatePrimitive(PrimitiveType type);
public static GameObject Find(string name);
public static GameObject[] FindGameObjectsWithTag(string tag);
public static GameObject FindWithTag(string tag);
For function declarations, functions that have static annotations are called in Lua with the type plus the "." sign.

function Start()
  	-- call class static function
	  local objFinded = UnityEngine.GameObject.Find("ObjectName");
end

C# member function calls in Lua

Let's take UnityEngine.GameObject as an example to call the following member functions:

GetInstanceIDGets the instance ID of the object.
GetComponentReturns the component of Type type if the game object has one attached, null if it doesn't.

The above long-term functions are an instantiated object of type GameObject. You must first get or create an object instance before you can call it. Note that member functions are called with the ":" sign.

function Start()
  -- call class member function 
  
  -- get a class object instance 
  local objFinded = UnityEngine.GameObject.Find("ObjectName");
  if objFinded then
  	-- if finded then print it's instance id。
    print(string.format("object instance id:%d",objFinded:GetInstanceID() ));
  end
  
  -- or create new instance 
  local newObj = UnityEngine.GameObject.CreatePrimitive(UnityEngine.PrimitiveType.Plane);
  print(string.format("newObj instance id:%d",newObj:GetInstanceID() ));
  
end

Locate C# types in Lua

C# language uses namespaces to avoid type name conflicts. Lua has no concept of namespaces. In ParaSpace, Lua simulates namespaces with tables, placing C# types in different namespaces into different tables to avoid naming conflicts. For example, GameObject is a UnityEngine namespace, and Button in NGUI is a UnityEngine.UI namespace.

function Start()
  -- create new GameObject, below is equal C#'s call. "var obj = new UnityEngine.GameObject("buttonObj");  "
  local obj = UnityEngine.GameObject("buttonObj");
  
  -- add a unity component to it 
  local buttonInst = obj:AddComponent(typeof(UnityEngine.UI.Button));
  buttonInst.onClick:AddListener(function()
      print("buttonInst clicked");
  end);
end

-- Or you can simplify the type lookup
local GameObject = UnityEngine.GameObject;
local Button = UnityEngine.UI.Button;
function Start()
   local obj = GameObject("buttonObj");
   local buttonInst = obj:AddComponent(typeof(Button));
   buttonInst.onClick:AddListener(function()
       print("buttonInst clicked");
   end);
end

Lua uses properties in C#

For access to properties in C#, static properties are types and are accessed with the "." sign, while non-static properties are accessed with the instance object plus the "." sign.

function Start()
  -- check network is ready, the 'isNetworkSettled' is static property
  if ParaNetService.isNetworkSettled then
    print("netowrk is settled");
  end
  
  -- access none static property
  local player = ParaPlayerService.GetPlayerById(1001);
  if player then
    print("player id property ",player.playerID);
  end
  
end

Comparison of C# and Lua arrays

Use of arrays in C# is shown below:

public void Awake()
{
 	GameObject[] objs = GameObject.FindGameObjectsWithTag("Actor");
  for(int i=0; i<objs.Length; ++i){
    Debug.Log($"obj id:{objs[i].GetInstanceID()}");
  }
}

Use of C# exported arrays in Lua. C# arrays start with index 0.

local GameObject = UnityEngine.GameObject;
function Awake()
  local objs = GameObject.FindGameObjectsWithTag("Actor");
  for i=0,objs.Length-1 do
    print("obj id:",objs[i]:GetInstanceID());
  end
end