功能中的Vars不会重置每次调用(LUA)

时间:2014-04-19 19:50:29

标签: function lua

我正在尝试设置一个函数来将min添加到当前时间对象并返回一个新的时间对象。我为此创建了一个函数但由于某种原因,每次调用函数时函数中的变量都没有被重置/本地。 每次调用函数都会使用函数内局部变量的过去值,为什么?

  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   

2 个答案:

答案 0 :(得分:1)

当您将sunriseHour传递给AddTime时,它会通过引用而不是值传递,这意味着对AddTime内的BaseTime所做的任何更改都会更改为{ {1}} - 两个变量(sunriseHoursunriseHour)都指向同一个对象。

因此,当您在AddTime中编写以下内容时:

BaseTime

您正在修改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中执行此操作,或者为您的时间对象创建某种复制构造函数。

答案 1 :(得分:-1)

谢谢泥, 我知道我需要复制我的时间对象,否则因为我使用它的引用,它将修改原始对象。 我找到了一个复制函数,并使用它将对象复制到函数

中的本地对象

谢谢

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));