Jump to content


Qix's Content

There have been 7 items by Qix (Search limited from 10-February 22)


By content type

See this member's

Sort by                Order  

#137167 Wrapper Snippet For Remote Peripherals

Posted by Qix on 31 July 2013 - 05:40 AM in Tutorials

View PostGopherAtl, on 31 July 2013 - 01:24 AM, said:

Ehrum. Dunno how to break this to you, but... peripheral.wrap works fine with remote peripheral names.

Wat, since when? I couldn't get it to work. :|

/wastedthread



#136885 Moving along contents in an array

Posted by Qix on 30 July 2013 - 06:06 AM in Ask a Pro

View Postjesusthekiller, on 30 July 2013 - 05:52 AM, said:

Read this about implementing FIFO and LIFO stacks in Lua

+1

Note that in some versions of Lua, table.remove() requires an index (though I believe CC uses a version of LuaJ that is high enough that it might not). Fair warning!



#136879 Locals? What Use Are They?

Posted by Qix on 30 July 2013 - 05:39 AM in Ask a Pro

View Posttheoriginalbit, on 30 July 2013 - 05:24 AM, said:

I would also like to point out that the local variables are not always deleted immediately after their scope has finished.

With LuaJ the Java garbagecollector will go around and destroy any variables that are not referenced by anything. The garbage collector will run whenever it is needed by the OS (or invoked by the programmer) and is not permanently running.

A definite advantage to localising variables is having functions and/or variables in APIs that no one can interact with, think of them as hidden internals.

Lastly if you like to read official documentation, you might want to read this...

Correct - since the objects are allocated as Java objects, they're subject to Java's garbage collector (since LuaJ is implemented 100% in Java). In C, they are deleted as soon as their reference count hits zero, unlike Java.

Also, good to know CC uses LuaJ :) I wasn't sure which library it used.



#136878 Wrapper Snippet For Remote Peripherals

Posted by Qix on 30 July 2013 - 05:35 AM in Tutorials

I came to the forums to share this little tidbit. I figured it could be of some use, seeing as how the modem's remoteXYZ() API didn't include a way to wrap remote peripherals.

Insert this somewhere in your script and call md.wrapRemote("name"). Make sure to setup which side the modem is on!

-- Wrap modem
--  Make sure to change the side
local md = peripheral.wrap("back")
assert(md)

-- Networked Peripheral Wrapper system
local net_mt = {}
local net_mtf = {}
function net_mtf:__call(...)
	md.callRemote(self.pname, self.name, ...)
end
function net_mt:__index(k)
	local func = {}
	func.name = k
	func.pname = self.name
	setmetatable(func, net_mtf)
	return func
end
md.wrapRemote = function(name)
	assert(net_mt)
	local wrapped = {}
	wrapped.name = name
	setmetatable(wrapped, net_mt)
	return wrapped
end

-- USAGE: md.wrapRemote("perhipheral_N")



#136877 Wimpy Question But I'm New

Posted by Qix on 30 July 2013 - 05:29 AM in Ask a Pro

Another alternative:

turtle._up = turtle.up
turtle.up = function(amt)
   for i=1,amt do
	  turtle._up()
   end
end

That will override the original turtle API function and provide a function similar to what Lord_Spelunky suggested. To me, it's cleaner when calling the function.



#136873 Moving along contents in an array

Posted by Qix on 30 July 2013 - 05:22 AM in Ask a Pro

Look up table.insert() and table.remove(). Instead of setting their values to nil, which causes issues when counting the table's elements, it shifts the elements within the table itself. All you have to know is the index at which to remove.

For a quick-tip, table.insert() with no index simply 'pops' it on to the end of the table (t[#t+1]=v)



#136872 Locals? What Use Are They?

Posted by Qix on 30 July 2013 - 05:18 AM in Ask a Pro

For the more hardcore Lua users who may understand this: Globals are stored in the table _G, whereas locals are stored as up-values. This is the same for scripts and other scoped "blocks" (closures, since scripts are simply closures).

Up-values are simply values held on the Lua stack and are given 'pseudo-indexes', or indexes that don't refer to an absolute index, but a relative one. This facilitates their removal from memory when the closure loses scope, instead of being stored in another table.

This usually doesn't matter when you're working within the Lua language, but it does when working with Java or, more commonly, the C API.