Jump to content

Lua is heartburn


wildcherrii
 Share

Recommended Posts

I got a pivot in the world , I've attached  a script to it, emit.lua

 

function Script:Start()

    --small box
    self.box=Model:Box()

end


function Script:UpdateWorld()

    self.MonitorBox()

end

 

Function Script:MonitorBox()

   self.box:Turn(0,1,0)

end

-------------------------------------------------------------------------------------

 

Please for the love of God, explain to me why this returns a " attempt to index field "box" a nil value.

Is it perhaps because Im using a custom named function?  Am i not allowed to make a function ? hrmmmp

Lua is such a ball slap, its horrible.  Nothing ever happens twice the same way, constantly having to restructure functions or  globals to get them to pick up elsewhere, then hope you don't break the entire code by changing the order to much.. blah blah..   

 

 

 

Link to comment
Share on other sites

This code should be safer. You also had a capital F for your custom function but that could have been due to auto correct.

Script.box = nil

function Script:Start()

    --small box
    self.box=Model:Box()

end


function Script:UpdateWorld()

    self:MonitorBox()

end

 

function Script:MonitorBox()

   if (self.box ~= nil) then self.box:Turn(0,1,0) end

end

I also don't like Lua because everything can be running fine until a random function triggers and crashes the app. This is one of the reasons I use C++. 

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

If you call a function like this in Lua it passes one argument to the function:

o.function(1)

If you call a function with a colon instead (:) it passes the object itself as an argument called self:, plus the other argument for the number one

o:function(1)

It's like the difference in C++ between a class method and static function.

Here is your working code:

function Script:Start()
    self.box=Model:Box()
end

function Script:UpdateWorld()
    self:MonitorBox()
end

function Script:MonitorBox()
   self.box:Turn(0,1,0)
end

 

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...