How to register a method with multiple input parameters in Lua.RegisterFunction?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Elledan3101
Posts: 6
Joined: Mon Oct 18, 2021 3:53 am

How to register a method with multiple input parameters in Lua.RegisterFunction?

Post by Elledan3101 »

Hello! My question is in the title: How to register a method with multiple input parameters in Lua.RegisterFunction?
Here my listing:

Code: Select all

	public class StorageHolderLuaWrapper : MonoBehaviour
	{
		[Header("General settings")]
		[SerializeField]
		private ItemsDataBase dataBase;
		[SerializeField]
		private StorageHolder storageHolder;

		private void OnEnable()
		{
			Lua.RegisterFunction($"{nameof(HelloWorld)}", this, SymbolExtensions.GetMethodInfo<string>((message) => HelloWorld(message)));

			Lua.RegisterFunction($"{nameof(Contains)}", this, SymbolExtensions.GetMethodInfo<string, bool>((itemID) => Contains(itemID)));

			Lua.RegisterFunction($"{nameof(AddItem)}", this, SymbolExtensions.GetMethodInfo<string>((itemID) => AddItem(itemID)));
			//Lua.RegisterFunction($"{nameof(AddItemAmount)}", this, SymbolExtensions.GetMethodInfo<string>((message) => AddItemAmount(message)));
			Lua.RegisterFunction($"{nameof(AddItemAmount)}", this, SymbolExtensions.GetMethodInfo<string, double>((itemID, amount) => AddItemAmount(itemID, amount)));

			Lua.RegisterFunction($"{nameof(RemoveItem)}", this, SymbolExtensions.GetMethodInfo<string>((itemID) => RemoveItem(itemID)));
			Lua.RegisterFunction($"{nameof(RemoveItemAmount)}", this, SymbolExtensions.GetMethodInfo<string>((message) => RemoveItemAmount(message)));

		}

		private void OnDisable()
		{
			Lua.UnregisterFunction($"{nameof(HelloWorld)}");

			Lua.UnregisterFunction($"{nameof(Contains)}");

			Lua.UnregisterFunction($"{nameof(AddItem)}");
			Lua.UnregisterFunction($"{nameof(AddItemAmount)}");

			Lua.UnregisterFunction($"{nameof(RemoveItem)}");
			Lua.UnregisterFunction($"{nameof(RemoveItemAmount)}");
		}

		private void AddItem(string itemID)
		{
			var itemObject = dataBase[itemID];
			storageHolder.Storage.TryToAdd(itemObject, 1, out _);

			Debug.Log($"[{nameof(StorageHolderLuaWrapper)}] {name} says: {nameof(AddItem)}: {itemID}");
		}

		private void AddItemAmount(string message)
		{
			string[] array = message.Split(';');
			if (array.Length == 2)
			{
				string itemID = array[0];
				int.TryParse(array[1], out var amount);

				var itemObject = dataBase[itemID];
				storageHolder.Storage.TryToAdd(itemObject, amount, out _);
			}
			else
			{
				Debug.LogError($"[{nameof(StorageHolderLuaWrapper)}] can't parse message: {message}");
			}

			Debug.Log($"[{nameof(StorageHolderLuaWrapper)}] {name} says: {nameof(AddItemAmount)}: {message}");
		}

		private void AddItemAmount(string itemID, double amount)
		{
			int amountInt = (int)amount;
			var itemObject = dataBase[itemID];
			storageHolder.Storage.TryToAdd(itemObject, amountInt, out _);

			Debug.Log($"[{nameof(StorageHolderLuaWrapper)}] {name} says: {nameof(AddItemAmount)}: {itemID} х {amountInt}");
		}


		private void RemoveItem(string itemID)
		{
			var itemObject = dataBase[itemID];
			storageHolder.Storage.TryToRemove(itemObject, 1);

			Debug.Log($"[{nameof(StorageHolderLuaWrapper)}] {name} says: {nameof(RemoveItem)}: {itemID}");

		}

		private void RemoveItemAmount(string message)
		{
			string[] array = message.Split(';');
			if (array.Length == 2)
			{
				string itemID = array[0];
				int.TryParse(array[1], out var amount);

				var itemObject = dataBase[itemID];
				storageHolder.Storage.TryToRemove(itemObject, amount);
			}
			else
			{
				Debug.LogError($"[{nameof(StorageHolderLuaWrapper)}] can't parse message: {message}");
			}

			Debug.Log($"[{nameof(StorageHolderLuaWrapper)}] {name} says: {nameof(RemoveItemAmount)}: {message}");
		}

		private bool Contains(string itemID)
		{
			Debug.Log($"[{nameof(StorageHolderLuaWrapper)}] {name} says: {nameof(Contains)}: {itemID}");

			var itemObject = dataBase[itemID];

			return storageHolder.Storage.Contains(itemObject);
		}

		private void HelloWorld(string message)
		{
			Debug.Log($"[{nameof(StorageHolderLuaWrapper)}] {name} says: {message}");
		}

The console throws me the same error every time:
Assets\StorageHolderLuaWrapper.cs(35,106): error CS1593: Delegate 'Func<string, double>' does not take 2 arguments


I see your Lua Wizzard allows me to create methods that have multiple parameters.
wizzard.PNG
wizzard.PNG (39.13 KiB) Viewed 266 times
I've double-checked your docs, scene example, script templates, and even this forum, but didn't find any examples for my case. Can you help me? :roll:
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to register a method with multiple input parameters in Lua.RegisterFunction?

Post by Tony Li »

Hi,

Try this form:

Code: Select all

// Register AddItemAmount(string itemID, double amount):
Lua.RegisterFunction(nameof(AddItemAmount), this, SymbolExtensions.GetMethodInfo(() => AddItemAmount(string.Empty, (double)0)));
Elledan3101
Posts: 6
Joined: Mon Oct 18, 2021 3:53 am

Re: How to register a method with multiple input parameters in Lua.RegisterFunction?

Post by Elledan3101 »

Tony Li wrote: Mon Oct 18, 2021 9:27 am Hi,

Try this form:

Code: Select all

// Register AddItemAmount(string itemID, double amount):
Lua.RegisterFunction(nameof(AddItemAmount), this, SymbolExtensions.GetMethodInfo(() => AddItemAmount(string.Empty, (double)0)));
...and... success. thanks! :D

your quest is done, Hero. now go your own way. other citizens are waiting for your help! :lol:
User avatar
Tony Li
Posts: 21981
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to register a method with multiple input parameters in Lua.RegisterFunction?

Post by Tony Li »

lol Glad to help! :-)
Post Reply