lua学习笔记(二)

上一个学习笔记都是记录了一些基本案例,这个学习笔记要记录一些自己遇到的问题

  • lua不支持函数重载!
  • wtf
  • 引用关系,这里的test方法,不需要return a
    function test(f)
    	f.b=3
    	f.c=4
    end
    
    a={}
    a.b=1
    a.c=2
    
    print(a.b,a.c)
    
    test(a)
    
    print(a.b,a.c)
    

    ——– Output ——

    1 2

    3 4

  • lua的操作符:

http://lua-users.org/wiki/ExpressionsTutorial

lua里面,false和nil为false,其他则为true

  • lua的assert:

a = false–nil

assert(a, “a is nil or false”)

print(“Hello World”)

——– Output ——

这里没有打印出Hello World,assert会打断后续的程序执行

  • lua的table类型的copy (http://lua-users.org/wiki/CopyTable)[http://lua-users.org/wiki/CopyTable]

  • lua的return: 这样是正确的:

    function fuck()

    i=0
    
    if i==0 then
    
    	return i
    
    else
    
    	return i+1
    
    end
    

    end

    print(fuck())

    而这样的连写return会引起编译错误(c#会有warning但并无error) function fuck()

    return i
    
    return i+1
    

    end

    print(fuck())

  • lua的table.remove和#长度:

    t={1,2,3,4}

    print(“length=”..#t)

    for i,v in pairs(t) do

    print(i,v)
    

    end

    table.remove(t,2)

    print(“length=”..#t)

    for i,v in pairs(t) do

    print(i,v)
    

    end

    ——– Output ——

    length=4

    1 1

    2 2

    3 3

    4 4

    length=3

    1 1

    2 3

    3 4

这里的lua table会在remove后自动调整长度

a={}

a[“k1”]=1

a[“k2”]=2

print(“length=”..#a)

for i,v in pairs(a) do

print(i,v)

end ——– Output ——

length=0

k1 1

k2 2

用非数字作为table的key,则不会影响其长度。

最近的文章

UnityEditor扩展:自定义Hierarchy图标

能上代码就不BB了。参考自:http://answers.unity3d.com/questions/431952/how-to-show-an-icon-in-hierarchy-view.html{% highlight c %}using UnityEditor;using UnityEngine;using System.Collections.Generic;[InitializeOnLoad]class HierarchyIconDisplay{ static Textu...…

继续阅读
更早的文章

lua学习笔记

安装luaforwindows https://github.com/rjpcomputing/luaforwindows/releases,然后将lua添加进环境变量,并且使用SciTE就可以编辑并运行lua代码了。Lua在线教程 http://lua-users.org/wiki/TutorialDirectory其他的诸如手册和教程,也可以在开始菜单的Lua下面找到 Hello World print("Hello World") 分号可有可无 注释 --[[多行注释]...…

继续阅读