Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 721
Expand file tree
/
Copy path06_NativeContainers.lua
More file actions
Latest commit
153 lines (120 loc) · 3.75 KB
/
Copy path06_NativeContainers.lua
File metadata and controls
153 lines (120 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
--[[
说明:创建原生容器时通常需要指定参数类型,来确定容器内存放的数据类型
例如:
local array = TArray({ElementType})
local set = TSet({ElementType})
local map = TMap({KeyType}, {ValueType})
参数类型 示例 实际类型
boolean true Boolean
number 0 Interger
string "" String
table FVector Vector
userdata FVector(1,1,1) Vector
创建完成后,和原来的原生容器类型使用方式是相同的,更多接口可以参考源码:
TArray Plugins\UnLua\Source\UnLua\Private\BaseLib\LuaLib_Array.cpp
TSet Plugins\UnLua\Source\UnLua\Private\BaseLib\LuaLib_Set.cpp
TMap Plugins\UnLua\Source\UnLua\Private\BaseLib\LuaLib_Map.cpp
]]--
localScreen=require"Tutorials.Screen"
localM=UnLua.Class()
localfunctionprint_intro()
localmsg=
[[
使用以下按键执行各个示例,并在控制台查看输出:
数字1:TArray
数字2:TSet
数字3:TMap
—— 本示例来自 "Content/Script/Tutorials.06_NativeContainers.lua"
]] ..
"\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"
Screen.Print(msg)
end
functionM:ReceiveBeginPlay()
print_intro()
end
localfunctiondump_array(array)
localret= {}
fori=1, array:Length() do
table.insert(ret, array:Get(i))
end
return"[" ..table.concat(ret, ",") .."]"
end
functionM:One_Pressed()
print_intro()
Screen.Print("TArray")
print("========== TArray ==========")
print("注:索引从1开始")
localarray=UE.TArray(0)
print("New: ", dump_array(array))
array:Add(1)
array:Add(2)
array:Add(3)
array:Add(3)
print("Add: ", dump_array(array))
localindex=array:AddUnique(1)
print("AddUnique(1): ", dump_array(array), "Returns:", index)
array:Remove(1)
print("Remove(1): ", dump_array(array))
array:RemoveItem(3)
print("RemoveItem(3):", dump_array(array))
array:Insert(4, 1)
print("Insert(1, 4): ", dump_array(array))
array:Shuffle()
print("Shuffle(): ", dump_array(array))
array:Clear()
print("Clear: ", dump_array(array))
end
localfunctiondump_set(set)
localarray=set:ToArray()
localret= {}
fori=1, array:Length() do
table.insert(ret, array:Get(i))
end
return"(" ..table.concat(ret, ",") ..")"
end
functionM:Two_Pressed()
print_intro()
Screen.Print("TSet")
print("========== TSet ==========")
localset=UE.TSet(0)
print("New: ", dump_set(set))
set:Add(1)
set:Add(1)
set:Add(2)
set:Add(2)
set:Add(3)
set:Add(3)
print("Add: ", dump_set(set))
print("Length: ", set:Length())
print("Contains(3): ", set:Contains(3))
set:Clear()
print("Clear: ", dump_set(set))
end
localfunctiondump_map(map)
localret= {}
localkeys=map:Keys()
fori=1, keys:Length() do
localkey=keys:Get(i)
localvalue=map:Find(key)
table.insert(ret, key..":" ..tostring(value))
end
return"{" ..table.concat(ret, ",") .."}"
end
functionM:Three_Pressed()
print_intro()
Screen.Print("TMap")
print("========== TMap ==========")
localmap=UE.TMap(0, true)
print("New: ", dump_map(map))
map:Add(1, true)
map:Add(2, false)
map:Add(3, true)
print("Add: ", dump_map(map))
map:Remove(2)
print("Remove(2): ", dump_map(map))
localvalue=map:Find(3)
print("Find(3): ", dump_map(map), "Returns:", value)
map:Clear()
print("Clear: ", dump_map(map))
end
returnM