Data Realms Fan Forums
http://45.55.195.193/

Method(a nil value) Error
http://45.55.195.193/viewtopic.php?f=1&t=26027
Page 1 of 1

Author:  Zaneo [ Sat Oct 29, 2011 10:03 pm ]
Post subject:  Method(a nil value) Error

This has been driving me crazy as I can't find the error, I suspect it's something really silly, or something I don't understand about lua. I was wondering if somebody could go over it and see if anything jumps out. I can paste more if you think it would help.

FuturePos is a vector.
LzPos is a vector.
dist is a (float?)

Code:
function Update(self)
   
   ...
   
   if self.FuturePos ~= nil then
      ConsoleMan:PrintString("Future was not null")
   end
   if self.LZpos ~= nil then
      ConsoleMan:PrintString("lz was not  null")
   end
   if self ~= nil then
      ConsoleMan:PrintString("self was not null")
   end
   local dist = SceneMan:ShortestDistance(self.FuturePos, self.LZpos, false).X
   if dist ~= nil then
      ConsoleMan:PrintString("dist was not null")
   end
   self:MoveLeftRight(self, dist, false)
   
   ...
end


function MoveLeftRight(self, dist, emergency)   
   
   if emergency then
      dist = dist*2
   end

   local change = self.XposPID:Update(dist, 0)

   if change > 2 then
      self.Controller.AnalogMove = Vector(change/30, 0)
   elseif change < -2 then
      self.Controller.AnalogMove = Vector(change/30, 0)
   end
end



----------------------------------------------------------------
Console Output
----------------------------------------------------------------
"Future was not null"
"Lz was not null"
"self was not null"
"dist was not null"
Error: Attemted to call MoveLeftRight(a nil value)

Author:  Zaneo [ Sun Oct 30, 2011 12:01 am ]
Post subject:  Re: Method(a nil value) Error

Well I found the problem, having not worked with lua I didn't know how functions work. It turns out you need to update the meta-tables in order to create functions.

ie.

Code:
-- The controller class
   PIDController = {}
   PIDController.mt = {__index = PIDController}
   function PIDController:New(p_in, i_in, d_in, last_in, integral_in)
      return setmetatable(
         {
            p = p_in,
            i = i_in,
            d = d_in,
            last = last_in or 0,
            integral = integral_in or 0
         },
         PIDController.mt)
   end
   
   function PIDController:Update(input, target)
      local err = input - target
      local change = input - self.last
      self.last = input
      self.integral = self.integral + err
      return self.p*err + self.i*self.integral + self.d*change
   end

Author:  Abdul Alhazred [ Sun Oct 30, 2011 8:37 am ]
Post subject:  Re: Method(a nil value) Error

I'm not sure you need any help with this, but just in case... Any function declared like this:
Code:
function MoveLeftRight(self, dist, emergency)   
    ...
end

will be a global function (accessible to all scripts in the game) and will of probably overwrite any function with the same name declared somewhere else. That is why I prefer to put the declaration in the Create-function, like this:
Code:
function Create(self)
    function self:MoveLeftRight(dist, emergency)   
        ...
    end
end

Note the position of the 'self'.

You probably already know about the on-line version of the first edition of the book Programing in Lua and the Reference Manual, but I can also recommend buying a physical copy of the second edition of PIL. Because it is a really good read.

Author:  Zaneo [ Sun Oct 30, 2011 9:05 pm ]
Post subject:  Re: Method(a nil value) Error

Yeah I wasn't aware of that until I got some help from, #lua on freenode. I'm glad you posted this though as I still wasn't 100% sure how to do it properly.

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/