Function 函数中的变量不会在每次调用时重置(LUA)
我正在尝试设置一个函数,将min添加到当前时间对象并返回一个新的时间对象。我为此创建了一个函数,但由于某些原因,每次调用该函数时,函数中的变量都没有被重新设置/本地。 对函数的每次调用都将使用函数中本地变量的过去值,为什么Function 函数中的变量不会在每次调用时重置(LUA),function,lua,Function,Lua,我正在尝试设置一个函数,将min添加到当前时间对象并返回一个新的时间对象。我为此创建了一个函数,但由于某些原因,每次调用该函数时,函数中的变量都没有被重新设置/本地。 对函数的每次调用都将使用函数中本地变量的过去值,为什么 local function AddTime (MinAfter, BaseTime) if (MinAfter == nil) then MinAfter = 0 end if (BaseTime == nil) or (Base
local function AddTime (MinAfter, BaseTime)
if (MinAfter == nil) then MinAfter = 0 end
if (BaseTime == nil) or (BaseTime.min == nil) or (BaseTime.hour == nil) then BaseTime = os.date("*t") end
BaseTime.hour = BaseTime.hour + math.floor((BaseTime.min + MinAfter)/60)
BaseTime.min = BaseTime.min + MinAfter - (60 * (math.floor((BaseTime.min + MinAfter)/60)))
if BaseTime.hour > 24 then BaseTime.hour = 24 end
return BaseTime
end
local sunriseHour = os.date("*t" ,os.time {year = 2014, month = 4, day = 19, yday = 259, wday = 4, hour = 6, min = 0, sec = 0, isdst = false});
-- this is the original time object in this case sunraiseHour
print ("sunriseHour time:" .. (string.format("%02d",sunriseHour.hour) .. ":" .. string.format("%02d", sunriseHour.min)));
-- first call
local newtime1= AddTime(10, sunriseHour);
print ("call 1 time:" .. string.format("%02d", newtime1.hour) .. ":" .. string.format("%02d", newtime1.min));
-- on the 1st call I get 07:10 which is right
-- 2nd call
local newtime2= AddTime(10, sunriseHour);
print ("call 1 time:" .. string.format("%02d", newtime2.hour) .. ":" .. string.format("%02d", newtime2.min));
-- on the 2nd call I get 07:20 and not 07:10 since this was the 2nd call to the function - the BaseTime var within the function was not local
当您将
sunrisehoor
传递到AddTime时,它是通过引用而不是通过值传递的,这意味着在AddTime内部对BaseTime
所做的任何更改都是对sunrisehoor
的更改——这两个变量(sunrisehoor
和BaseTime
)都指向同一个对象
因此,当您在AddTime中编写以下内容时:
BaseTime.hour = BaseTime.hour + math.floor((BaseTime.min + MinAfter)/60)
BaseTime.min = BaseTime.min + MinAfter - (60 * (math.floor((BaseTime.min + MinAfter)/60)))
您正在修改日出时间
您似乎不太理解这一点,因为您还在AddTime中为BaseTime指定了一个新值,这表明您认为您有一个新对象。如果要创建sunriseHour的修改副本,则需要在AddTime中执行此操作,或者为时间对象创建某种副本构造函数。,
我知道我需要复制我的时间对象,否则因为我使用了它的引用,它将修改原始对象。
我找到了一个复制函数,并使用它将对象复制到函数中的本地对象
谢谢
function table.copy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
function AddTime (MinAdd, TimeObj)
local BaseTime = {};
local MinAfter = 0;
if (TimeObj == nil) or (TimeObj.min == nil) or (TimeObj.hour == nil) then BaseTime = table.copy(os.date("*t")) else BaseTime = table.copy(TimeObj) end;
if (MinAdd == nil) then MinAfter = 0 else MinAfter = MinAdd end;
BaseTime.hour = BaseTime.hour + math.floor((BaseTime.min + MinAfter)/60)
BaseTime.min = BaseTime.min + MinAfter - (60 * (math.floor((BaseTime.min + MinAfter)/60)))
if BaseTime.hour > 24 then BaseTime.hour = 24 end
return BaseTime
end
-- this is the original time object in this case sunraiseHour
local sunriseHour = os.date("*t" ,os.time {year = 2014, month = 4, day = 19, yday = 259, wday = 4, hour = 6, min = 0, sec = 0, isdst = false});
print ("sunriseHour time:" .. (string.format("%02d",sunriseHour.hour) .. ":" .. string.format("%02d", sunriseHour.min)));
-- first call
local newtime1= AddTime(10,sunriseHour);
print ("call 1 time:" .. string.format("%02d", newtime1.hour) .. ":" .. string.format("%02d", newtime1.min));
-- on the 1st call I get 07:10 which is right
-- 2nd call
local newtime2= AddTime(10,sunriseHour);
print ("call 2 time:" .. string.format("%02d", newtime2.hour) .. ":" .. string.format("%02d", newtime2.min));
-- on the 2nd call I get 07:20 and not 07:10 since this was the 2nd call to the function - the BaseTime var within the function become global
print ("Added time:" .. string.format("%02d", AddTime(20, sunriseHour).hour) .. ":" .. string.format("%02d", AddTime(20, sunriseHour).min));
在同一个对象上调用AddTime两次,首先添加10,然后添加另一个10,结果是添加20。明白了,但我确实需要保持原始对象不变,因为我想调用AddTime函数,只需保留几个对象。如何创建复制构造函数或创建复制对象的函数。就这样。这和你想的一样简单。我将编辑我的文章以显示一个示例,但我不知道对象可以包含的所有字段。