gpt4 book ai didi

Lua Gideros : Call function with multiple parameters

转载 作者:行者123 更新时间:2023-12-05 05:26:24 24 4
gpt4 key购买 nike

在我使用 Gideros Studio 的游戏中,我有一个具有多个参数的函数。我想在一个参数上调用我的函数,然后在另一个参数上调用。这可能吗?

这是我的功能:

local function wiggleroom(a,b,c)
for i = 1,50 do
if a > b then
a = a - 1
elseif a < b then
a = a + 1
elseif a == b then
c = "correct"
end
return c
end
end

我想将ab进行比较,但稍后调用bc上的函数.例如:

variable = (wiggleroom(variable, b, c) --if variable was defined earlier
variable2 = (wiggleroom(a, variable2, c)
variable3 = (wiggleroom(a, b, variable3)

我还希望能够将此函数用于多个对象(对每个参数调用两次)。

最佳答案

如果我的理解正确,您可以考虑使用类的 lua 版本。如果你不认识他们,你可能想看看 this .

例子:

tab = {}

function tab:func(a, b, c) -- c doesn't get used?
if a then self.a = a end
if a then self.b = b end
if a then self.c = c end

for i = 1,50 do
if self.a > self.b then
self.a = self.a - 1
elseif self.a < self.b then
self.a = self.a + 1
elseif self.a == self.b then
self.c = "correct"
end
end
return c -- not really necessary anymore but i leave it in
end

function tab:new (a,b,c) --returns a table
o = {}
o.a = a
o.b = b
o.c = c
setmetatable(o, self)
self.__index = self
return o
end

--how to use:
whatever1 = tab:new(1, 60) --set a and b
whatever2 = tab:new() --you also can set c here if needed later in the function

whatever1:func() --calling your function
whatever2:func(0,64)

print(whatever1.a) -->51
print(whatever2.a) -->50
print(whatever1.c) -->nil
whatever1:func() --calling your function again
whatever2:func()
print(whatever1.a) -->60
print(whatever2.a) -->64
print(whatever1.c) -->correct
print(whatever2.c) -->correct

关于Lua Gideros : Call function with multiple parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26445261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com