2 posts
Posted 04 February 2013 - 05:17 PM
Title: does CC Lua support "self:" references
Just curious: is there an API for what, if any, of the computer/turtle structure is exposed via self: references? Apart from reading the source, of course. 8-} This started as a search for a method/hook/reference to poll so that various turtle types could discover their own type more gracefully than, say, testing whether specific API calls were defined.
If a specific goal is required I suppose it could be "lua code to put in a startup run by a turtle of arbitrary subtype to discover that subtype (ie., 'mining turtle' vs 'crafty turtle' vs 'turtle')". In reality I don't have a specific thing I'm trying to do with this, I'm just curious if it's supported.
3790 posts
Location
Lincoln, Nebraska
Posted 04 February 2013 - 05:29 PM
I do know that there are certain functions that return true on certain computers, such as term.isColor() returns true if the computer is advanced. You can also call if turtle then, and it will return true only if code is being run on a turtle.
404 posts
Location
St. Petersburg
Posted 04 February 2013 - 07:00 PM
here's how to use self:
local obj = {}
setmetatable(obj,{
__index = function(self,index)
return self
end,
length = function(self)
return #self
end
})
print(type(obj.randomIndex)) --> "table"
print(obj:length()) --> "0"
so to use self:func(), you would be simply calling another metamethod
1054 posts
Posted 04 February 2013 - 11:56 PM
here's how to use self:
local obj = {}
setmetatable(obj,{
__index = function(self,index)
return self
end,
length = function(self)
return #self
end
})
print(type(obj.randomIndex)) --> "table"
print(obj:length()) --> "0"
so to use self:func(), you would be simply calling another metamethod
That's not really what he was asking for I think. :/ But maybe your example could show how his question is hardly relevant to that. :P/> And that's quite a unfortunate example, because you define self explicitly everywhere. It could've as well been called 'foobar' instead of 'self'. :P/>
@OP: I'm not entirely sure what you're thinking of, but different type of turtles don't have different type of turtle api's or something. So they have no different reference of anything. Was that what you needed? You can use some 'hacks', like the fact that crafty turtles have the crafting peripheral and wireless ones have the modem peripheral…
404 posts
Location
St. Petersburg
Posted 05 February 2013 - 12:37 PM
–snip–
in order to use self, you have to define it as the first argument in a metamethod
otherwise, you're calling nil, both in vanilla and CC Lua
its just a handy tool so that you can copy classes to other variables and they will call their own metamethods instead of the original's
1054 posts
Posted 05 February 2013 - 12:55 PM
–snip–
in order to use self, you have to define it as the first argument in a metamethod
otherwise, you're calling nil, both in vanilla and CC Lua
its just a handy tool so that you can copy classes to other variables and they will call their own metamethods instead of the original's
You can use self without defining it explicitly when you use this notation:
function metatable:method()
self.member = 42
end
Self is defined implicitly in this case (it's literally the same as 'function metatable.method(self)' ) and that's what I meant. In your case you could've renamed self as foobar and it would've worked just fine, in my case it wouldn't have. Anyway, I was just pointing out that it wasn't his question to start with and that you rather explained the principle of references to objects in metamethods than the use of 'self'.
2 posts
Posted 05 February 2013 - 02:22 PM
Thanks for the replies. I'm fairly familiar with the self: reference per se, I'm just curious about the extent to which CC uses it, if at all, to expose key bits of of structure. From a cpp/ada/perl world I'd expect something like self:id (ok, yea, that should probably be 'self:__id', or more appropriately 'self:id()', "if you're going to do OO then do OO" 8-P ) if it existed at all. A cursory poke at the code didn't turn up anything that looked like a static const defining the name/class/etc of the item, and there doesn't appear to be a way for a CC item to ask for it's own block id, etc. (Um – there isn't a lua call for a CC item to return it's own block id, is there?)
Orwell's note on API overlap is precisely why I detest resorting to hacks in self-identity checks; when it's possible, *if* it's possible, it's still extremely fragile in the face of API deltas. Detecting peripherals itself isn't even a good halfway measure if you're looking for a common code base for CC items where two+ common items can have wireless modems.
Ok, so it looks like self-identification should probably be done with an external agent, say, sensors. Good enough for me, thanks!
1054 posts
Posted 05 February 2013 - 02:39 PM
I agree completely on detesting the hacky way. :)/> The main issue is probably that most of the turtle-type-specific code is purely Java-side.