关于Lua中的out和ref

本文介绍了一个C#与Lua脚本交互的例子,展示了如何在C#中定义带有out和ref参数的方法,并从Lua脚本中调用这些方法。

原贴:http://blog.youkuaiyun.com/sinat_20559947/article/details/48065719

luaframework中只有out的实例:TestOut.unity

 直接上例子:

C#代码:

using System;
using LuaInterface;
namespace myLua
{
    class MainClass
    {
        public string name = "Ocean";
        public void CSharpMethod(string name ,out int count)
        {
            Console.WriteLine ("这是C#里的一个方法");
            count = name.Length;
        }
        public void TestRef(string name,ref int count)
        {
            Console.WriteLine (name);
            Console.WriteLine (count);
            count = name.Length;
        }

        public static void Main (string[] args)
        {
    
            // 创建一个Lua解释器                        
            Lua lua = new Lua();

                        // 在C#中使用lua的语法调用lua脚本                        
            lua.DoFile("luaScript.lua");               
               }
      }
}

Lua代码:

myClass = MainClass()  
print(myClass.name)  
returnValue,strLength
= myClass:CSharpMethod("Ocean")
--
myClass:CSharpMethod("Ocean",nil) --一般情况下最好是这么写,不要省略那个参数
--这个地方一般是要加上第二个参数,传递nil,因为在通过射线取hit的方法中,获取的out的值并不是在参数的末尾,如果不传一个nil,好像是会报错的


print(returnValue, strLength) --returnValue是方法的返回值,如果没有返回值就返回nil

returnValue,count = myClass:TestRef("Ocean",5) --这里第二个参数是ref的取值,必需要传递一个参数

 

转载于:https://www.cnblogs.com/vsirWaiter/p/8176076.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值