我正在尝试设置一个函数来将min添加到当前的time对象中,并返回一个新的time对象。我为此创建了一个函数,但由于某种原因,每次调用函数时,函数中的var都不会被重新设置/本地。每次对函数的调用都将使用函数中本地vars的过去值,为什么?
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 发布于 2014-04-19 20:03:11
当您将sunriseHour传递到AddTime时,它是通过引用而不是通过值传递的,这意味着在AddTime中对BaseTime所做的任何更改都是对sunriseHour的更改--两个变量(sunriseHour和BaseTime)都指向同一个对象。
因此,当您用AddTime编写以下内容时:
BaseTime.hour = BaseTime.hour + math.floor((BaseTime.min + MinAfter)/60)
BaseTime.min = BaseTime.min + MinAfter - (60 * (math.floor((BaseTime.min + MinAfter)/60)))你在修改sunriseHour。
您似乎不太明白这一点,因为您还在AddTime中为AddTime分配了一个新的值,这意味着您认为您有一个新的对象。如果您想要创建修改过的sunriseHour副本,那么您需要在AddTime中这样做,或者为您的time对象创建某种类型的复制构造函数。
发布于 2014-04-19 20:35:51
谢谢Mud,我知道我需要复制我的time对象,因为我使用了它的引用,它将修改原始对象。我找到了一个复制函数,并使用它将对象复制到函数中的本地函数中。
谢谢
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));https://stackoverflow.com/questions/23174725
复制相似问题