LoTROInterface

LoTROInterface (https://www.lotrointerface.com/forums/index.php)
-   Lua Programming Help (L) (https://www.lotrointerface.com/forums/forumdisplay.php?f=50)
-   -   issue with functions inside of a class (https://www.lotrointerface.com/forums/showthread.php?t=1126)

SanDBoX 10-04-2010 10:16 PM

issue with functions inside of a class
 
I recently ran across an issue where when I am defining functions inside of a class I can no longer use the "self" identifier such as:
Code:

10  MyWindow = class( Turbine.UI.Window )
11
12  function MyWindow:Constructor()
13    Turbine.UI.Lotro.Window.Constructor( self ) --this part works fine
14    self.int = 5
15    ...
16  end
17
18  function MyWindow:BronkenFunction()
19    self.int = self.int + 1
20  end

Oversimplified but should give the right idea, in this kind of an instance I end up getting an "Attempt to index local self (a 'nil' value)" error, and the thing that bugs me the most is I have functions in other files that work just fine and they are written the same way (outside the constructor)

Any idea what it is that I am missing? This really has me lost tonight...

moebius92 10-04-2010 11:59 PM

When you call the BronkenFunction(), are you using . or :? I.e.,

myWindow = MyWindow();
myWindow.BronkenFunction();

vs.

myWindow = MyWindow();
myWindow:BronkenFunction();

Because you should be doing the second.

SanDBoX 10-05-2010 07:32 AM

Okay, that worked, and explains why it was so flacky and only did it when I clicked one button (all the other ways it was called worked fine). Guess I was more tired then I thought last night.

Thank you.

Digital_Utopia 10-05-2010 09:42 AM

Quote:

Originally Posted by SanDBoX (Post 5099)
Okay, that worked, and explains why it was so flacky and only did it when I clicked one button (all the other ways it was called worked fine). Guess I was more tired then I thought last night.

Thank you.

If you want to call a function with the common dot operator, create the function like so:

Code:

MyWindow.BronkenFunction = function()
    self.int = self.int + 1
end

Then you could call it with "MyWindow.BronkenFunction()"

Kryso 10-07-2010 02:25 PM

And this example would fail again, because there would be no "self" (or at least no valid self).

Code:

function Table:Foo( arg )
    self.bar= self.bar+ arg;
end

is just syntactic sugar for

Code:

Table.Foo = function( self, arg )
    self.bar = self.bar + arg;
end

and

Code:

Table:Foo( 1 );
is the same as

Code:

Table.Foo( Table, 1 );

Bonechip 10-07-2010 03:29 PM

What I read....
 
If the inverting-core acceptor deflects the complex chronotron-feedback analysis, try to provoke a coil-composition reflex and several quantum biosphere resonances, this will create a restricted isovolumic cochrane graviton-prediction, which ought to in fact dampen the polarizing maintenance-filament formulas. Then attempt a minimum abstract component-delay correction phase to input a reversible lucifugal primary ionization perimeter operation to cancel the celestial info-sphere greenhouse effect level-limits. As you are doing this, set in motion six homeostasis global-attractors from the constant chemical cybernetic-induction elliptical-beam, this will dislodge a krypton placebo-molecule from the kinetic synthesis-accelerator.

You guys are talking greek. LOL

SanDBoX 10-07-2010 04:49 PM

LOL not that bad bonechip ;)

Still seams funny to me that LUA has such a flexable syntax, I am usto very defined lines instead of the the whole "you can do it this way, or if you want this way, or this way... " and the fact that it all still works unless you make a dumb mistake is just Icing on the cake.

Digital_Utopia 10-07-2010 09:14 PM

Quote:

Originally Posted by Kryso (Post 5124)
And this example would fail again, because there would be no "self" (or at least no valid self).

Well..part of that is a bit of a typo. It should be:

Code:

self.BronkenFunction = function()
    self.int = self.int+1
end

Instead of using MyWindow.

Kryso 10-08-2010 04:15 AM

Quote:

Originally Posted by Digital_Utopia (Post 5128)
Well..part of that is a bit of a typo. It should be:

Code:

self.BronkenFunction = function()
    self.int = self.int+1
end

Instead of using MyWindow.

Yea this would work if you created the function in another function, so it would take self from parent scope - which is in a lot of cases bad practice, because it would create new function on every call. I mean it is usefull sometimes, but it shouldn't be used so we can call functions that are supposed to have "self" reference with dot operator.

Digital_Utopia 10-08-2010 06:37 AM

Quote:

Originally Posted by Kryso (Post 5129)
Yea this would work if you created the function in another function, so it would take self from parent scope - which is in a lot of cases bad practice, because it would create new function on every call. I mean it is usefull sometimes, but it shouldn't be used so we can call functions that are supposed to have "self" reference with dot operator.

Except most "class" definitions are technically functions. eg:

Code:

MyWindow = class( Turbine.UI.Window )
function MyWindow:Constructor()
      Turbine.UI.Lotro.Window.Constructor( self )



end

And, given that it isn't a local function, (i.e. something that will never be called from outside the class), wouldn't it make sense to make a unique copy of that function for every instance of the class?


All times are GMT -5. The time now is 09:39 PM.

vBulletin® - Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
© MMOUI