Lua MiddleClass. How to pass "self" from another file - lua

Say, If i have two or more files using the middleclass extension more or less like this. I omitted some of the obvious middleclass implementation code.
File A:
function Battlefield:initialize()
self.varA
self.varB
end
function Battlefield:attack()
--I want to use self.varA here
end
File B
BattlefieldInstance = Battlefield:new()
function doStuff()
BattlefieldInstance:attack()
end
I know this structure more or less works because i already use it plenty on my project, but my problem is that i want to use these self variables. Normally a self instance is passed between functions inside the same file to do this, but when i do it from another file i obviously can't pass self, because it would be another self, and i need the self from the file where the function is located. Sorry if my question is a bit confusing. I'll try and clarify any questions there are.

I have no idea what middleclass is, but I think you're confusing yourself. The way self works in Lua is a function that looks like function Battlefield:attack() is absolutely the same thing as function Battlefield.attack(self). In other words, self is just an implicit first parameter to the function. And a method call instance:attack() is exactly equivalent to instance.attack(instance) (though it won't evaluate instance twice if you use an expression there).
In other words, BattlefieldInstance:attack() should do exactly what you want.

'self' is a keyword that means 'the current object'. So in the case of Battlefield functions, 'self.varA' inside the function is the same variable as 'Battlefield.varA' outside the function.

Middle Class was a lib that I first saw developed for Love2D; I assuming its the same one that corona is using? (I've used Corona a fair bit... but not Middle Class's OOP system)
either way you can also try using meta tables directly, as so:
---FILE A---
Battlefield= {}
Battlefield.__index = Battlefield
function Battlefield:new()
return setmetatable({var1 = 'somedata', var2 = 'somemodata', var3 = 'lotsodata'}, Battlefield)
end
function Battlefield:attack()
print(self.var1)
end
---FILE B---
BattlefieldInstance = Battlefield:new( )
function doStuff()
BattlefieldInstance:attack()
end
and that'll print out self.var1 (somedata).
Hope this helps!

Related

What is self and what does it do in lua?

I'm a new programmer in lua, and there are lots of things I still probably don't know about. I googled what self is in lua, but I still don't understand it. If anyone could give me the easiest explanation for what "self" does in lua, it would be really helpful.
self is just a variable name. It is usually automatically defined by Lua if you use a special syntax.
function tbl:func(a) end
is syntactic sugar for
function tbl.func(self, a) end
That means Lua will automatically create a first parameter named self.
This is used together with a special function call:
tbl:func(a)
which is syntactic sugar for
tbl.func(tbl, a)
That way self usually refers to the table. This is helpful when you do OOP in Lua and need to refer to the object from inside your method.
Similar to this in C++.

Lua functions: how to use tables as function arguments and self-document the code

When I have functions which take many arguments it's sometimes useful to pass a single table as an argument instead of many local variables.
function example_1(arg_one, arg_two, arg_three)
end
becomes
function example_2(arg_table)
arg_table.arg_one, arg_table.arg_two, arg_table.arg_three
end
The problem is, when calling the function somewhere else in code, it's hard to remember what arg_table needs to consist of. There are plenty of code-completion plugins for many editors which will help you with remembering the arguments for the example_1 function, but not for example_2.
Is there any way to write the example_2 function with a table parameter in a way that is still a table, but also shows the necessary parameters for the function inside the ()?
something like this (which does not work):
function example_2(arg_table = {arg_one, arg_two, arg_three})
end
Write your formal parameter list and documentation with separate parameters, as usual. Then document that if the first (and only) actual argument is a table, the effective arguments will be taken from the table using the formal parameter names as string keys.
You can keep the function parameters as they are and pass a table for convenience.
For example:
function example(arg_one, arg_two, arg_three)
-- do stuff
end
local tbl = {"value for arg_one", "value for arg_two", "value for arg_three"}
example(table.unpack(tbl))
Note that this doesnt work for tables with named keys.

How to pass variable by reference in Corona

I'm using Corona SDK.
I want to write a function which receives component as parameter and removes it like that:
function removeComponent(component)
if component then component:removeSelf() end
component = nil
end
Well, it works but my parameter does not get nil after using this function. Probably I have to pass it by reference, but I'm not sure it is possible with Corona.
This does not really make sense as presented in your example.
What exactly are you trying to accomplish? Is component a global ? Or a key in a table?
In your example, component is the name of a local variable in your function. Your component = nil only removes the value from the local variable, and thus will be lost.
If you want to have global effects, you'd need to pass the name of the variable you'd want to eliminate as string:
function removeComponent(component)
if _G[component] then -- exists globally?
_G[component]:removeSelf()
end
_G[component] = nil
end
Note that this style of programming (using the global table for this kind of things) is generally not a good idea. In the best case it can surprise you, in the worst case you end up zapping things like standard functions as print.
Therefore I'd recommend puttign things in their own table, and passing it on to the function.
Lua doesn't support passing by reference, but since it does support return values you can always achieve what you want using this idiomatic approach:
function removeComponent(component)
if component then component:removeSelf() end
return nil
end
And then call it like this:
a = removeComponent(a)
Edit: It's worth pointing out too that since Lua supports multiple return values and multiple assignments, you never actually need pass-by-reference. If you need several items updated, pass them in and return them and then do the call as
a,b = myFunction(a,b)
Its not different really than any other language. Passing by a value by reference (in C++ for example) would not stop any program from holding another copy of this same value elsewhere.
I know nothing of Corona, but this isn't really a Corona question so much as a Lua style question. However, if I had wrote it, I would ensure that the 'component' userdata or underlying value would clear itself up. If the userdata was accessed again, it should throw an error complaining about re-using a dead userdata.
I have written this code:
local component = display.newCircle(100, 100, 100);
local function removeComponent(c)
if component then component:removeSelf() end
component = nil
end
removeComponent(component)
if component == nil then print("Component is nil") else print("Component is not nil") end
And it prints "Component is nil". Perhaps you have a copy of your component somewere else or may be you forget to call function removeComponent or something else. Need to see more of your code

Getting multiple references to the same method = multiple objects?

I'm new to Dart, so maybe I'm missing something here:
This works:
In my main(), I have this:
var a = _someFunction;
var b = _someFunction;
print("${a == b}"); // true. correct!
Where _someFunction is another top-level function.
This does NOT work: (at least not how I'm expecting it to)
Given this class...
class Dummy {
void start() {
var a = _onEvent;
var b = _onEvent;
print(a == b); // false. ???????
}
void _onEvent() {
}
}
Instantiating it from main() and calling its start() method results in false. Apparently a new instance of some function or closure object is created and returned whenever my code obtains a reference to _onEvent.
Is this intentional behaviour?
I would expect that obtaining multiple references to the same method of the same instance returns the same object each time. Perhaps this is intended for some reason. If so; what reason? Or is this a bug/oversight/limitation of VM perhaps?
Thanks for any insights!
Currently, the behaviour seems to be intentional, but the following defect is open since May 2012: https://code.google.com/p/dart/issues/detail?id=144
If I were to guess, I'd say that setting "var a = _onEvent;" creates a bound method, which is some sort of object that contains both the function as well as this. You are asking for bound methods to be canonicalized. However, that would require the team to create a map of them, which could lead to worries about memory leaks.
I think they made "var a = _someFunction;" work early on because they needed static functions to be constants so that they could be assigned to consts. This was so that they could write things like:
const logger = someStaticLoggingFunction;
This was in the days before statics were lazily evaluated.
In any case, I would say that comparing closures for equality is a edge case for most languages. Take all of the above with a grain of salt. It's just my best guess based on my knowledge of the system. As far as I can tell, the language spec doesn't say anything about this.
Actually, now that I've read (https://code.google.com/p/dart/issues/detail?id=144), the discussion is actually pretty good. What I wrote above roughly matches it.

Use of metatables in Lua

I'm trying to implement a object notation in Lua scripts.
Here is what I succeeded to do with the C API:
I created a new global table "Test" as a class and added a field "new" in it, pointing to a function written in C
i.e I can do this in lua code: "local obj = Test.new()" (it calls the function "new")
The "new" C function creates and returns a new table, and registers functions in it as fields (e.g "add", "count"...)
i.e I can do this: "obj:add("mike")" and "obj:count()" (obj is passed as first arguments with the ":" notation)
2 questions:
1) Everything works as expected, but the thing I'm wondering is: What is the advantage of using metatables in my case?
I see everywhere that metatables can help me to achieve what I tried to do, but I don't understand where they would be useful?
Adding fields to tables as methods isn't correct?
How could metatables help me (If added as my tables metatables)?
2) In fact I'm trying to reproduce the behaviour of C++ in Lua here.
For example, when I write this in C++: "Test *obj = new Test();"
I expect C++ and the constructor of Test to return me a pointer of an instance of Test.
This is exactly what I'm trying Lua to do for me.
The thing is that I use a table in this case, as the return of "new", but not a pointer so I can call methods on it later with Lua (using its fields), like a standard C++ object (with the operator ->).
To be able to retreive the actual pointer of my class in the C fonctions, I added a field "ptr" (light uservalue) to the table returned by "new". Without it, I would have been able to manipulate only the Lua table in my C function, nothing more (so no more method calls on the real pointer).
My second question is, Is it the right way to do it?
Do you have better idea on how to be able to manipulate my pointer everywhere without this "ptr" field?
Thank you,
Nicolas.
The main reason is that you get the __index metamethod.
Without it, every instance of a object has to have all the functions associated with it: which can make tables why large; and use a lot of memory.
local methods = {
foo = function() return "foo" end ;
bar = function() return "bar" end ;
bob = function() return "bob" end ;
hen = function() return "hen" end ;
}
So you have either
function new_no_mt ( )
local t = {}
for k , v in pairs ( methods ) do t [ k ] = v end
return t
end
vs
local mt = { __index = methods ; }
function new_mt ( )
return setmetatable ( { } , mt )
end
There are other benefits too;
defining operators;
easy type comparison via comparing metatables.
Changing method in metatable changes it for all objects, but changing it on one object only changes it for that object.
Otherwise, what you are trying to do sounds like the perfect situation for userdata.
userdata can only be indexed when you have an __index metatmethod (theres no table to put methods in)
What is the advantage of using metatables in my case?
Here's one.
Test.new = function() end
I just destroyed your ability to create new Test objects globally. If you had protected Test with a metatable, I would have had to really work to be able to do that.
Here's another:
local myTest = Test()
You can't do that without a metatable (overload the __call metamethod). And it looks much more natural for Lua syntax than calling Test.new.
And another:
local testAdd = Test() + Test()
Operator overloading. You can't do that without metatables.
Also:
This is exactly what I'm trying Lua to do for me.
Here's a tip: don't.
Different languages are different for a reason. You should do things in Lua the Lua way, not the C++ way. I can understand wanting to provide certain C++ mechanisms (like overloading and so forth), but you shouldn't use new just because that's what C++ uses.
Also, there are plenty of libraries that do this work for you. SWIG and Luabind are two big ones.

Resources