View unanswered posts | View active topics It is currently Sat Dec 28, 2024 4:43 pm



Reply to topic  [ 4 posts ] 
 Method(a nil value) Error 
Author Message

Joined: Sun Oct 23, 2011 7:57 am
Posts: 23
Reply with quote
Post 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)


Sat Oct 29, 2011 10:03 pm
Profile

Joined: Sun Oct 23, 2011 7:57 am
Posts: 23
Reply with quote
Post 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


Sun Oct 30, 2011 12:01 am
Profile
DRL Developer
DRL Developer

Joined: Tue Aug 11, 2009 5:09 am
Posts: 395
Reply with quote
Post 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.


Sun Oct 30, 2011 8:37 am
Profile

Joined: Sun Oct 23, 2011 7:57 am
Posts: 23
Reply with quote
Post 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.


Sun Oct 30, 2011 9:05 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 4 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.035s | 13 Queries | GZIP : Off ]