This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
[API]Have I done this right?
Started by Appleeater, 24 March 2013 - 03:34 AMPosted 24 March 2013 - 04:34 AM
Posted 24 March 2013 - 04:37 AM
yeh I don't see why not…. just be aware that anything that is made local will not be able to be accessed via any other file or program…
Posted 24 March 2013 - 04:38 AM
ok, how can i let my variables accsessable by other program's?
Posted 24 March 2013 - 04:44 AM
no no, your functions are usable, I was just pointing out that if you do at a later stage add a function and put local in front of it, only the api can use it, nothing else can.
Posted 24 March 2013 - 04:48 AM
So if I just do
variable = "test"
instead of
local variable = "test"
Posted 24 March 2013 - 04:51 AM
ok. ignore what I said. all of it………. pretend you have only read the following line…
your code will work perfectly as an api. as for if it will function how you want it to, maybe not (I can see a bug in getRSinputs).
your code will work perfectly as an api. as for if it will function how you want it to, maybe not (I can see a bug in getRSinputs).
Posted 24 March 2013 - 05:08 AM
(I can see a bug in getRSinputs).
and what is that bug?
Posted 24 March 2013 - 05:11 AM
You are using local in front of the variables, meaning that after the function has run all those values will be forgotten…and what is that bug?
after a closer look I found some more bugs, here is a version fixing them, have a read of all the comments after the words 'NOTE:' and I explain more about the bug/problem http://pastebin.com/enVZrPAp
Posted 24 March 2013 - 05:22 AM
thanks, this fixes some of my unknown errors and will stop me getting them wrong again. the only thing I'm confused about is how it detects the getRSinput() section?
Posted 24 March 2013 - 05:27 AM
no problems.
rs.getSides() returns a table containing all the sides of the computer. so what that code does is it iterates through those sides and saves into that table at the top, under the key of the side its checking, a boolean of if the rs is on or off. make sense?
rs.getSides() returns a table containing all the sides of the computer. so what that code does is it iterates through those sides and saves into that table at the top, under the key of the side its checking, a boolean of if the rs is on or off. make sense?
Posted 24 March 2013 - 05:29 AM
so it looks all of the sides and stores in the table what their state are
Posted 24 March 2013 - 05:31 AM
exactly, and it gets the sides from the rs.getSides function, which quite litterally does this
return { 'top', 'bottom', 'front', 'back', 'left', 'right' }
Posted 24 March 2013 - 05:40 AM
nice so is there any way I could do that for modems so with the api you could do
rednet.open(mind.getModems)
rednet.open(mind.getModems)
Posted 24 March 2013 - 05:43 AM
Of course, it is after all just a string, like normal…
openAllModems opens the modems :P/>
so all you would need to do is call
mind.openAllModems()
function getModems()
local m = {}
for _, side in pairs(rs.getSides()) do
if peripheral.getType( side ) == 'modem' then
table.insert( m, side )
end
end
return m
end
function openAllModems()
for _, side in pairs( getModems() ) do
rednet.open( side )
end
end
getModems would return a table of all the sides the modem are onopenAllModems opens the modems :P/>
so all you would need to do is call
mind.openAllModems()
Posted 24 March 2013 - 05:48 AM
A chunk is also a block, and so local variables can be declared in a chunk outside any explicit block. The scope of such local variables extends until the end of the chunk. This means that any other program/chunk/block cannot use this variable if it is defined as 'local'. Defining something local at the top of the script will allow it to be used throughout the entire script, instead of being limited to that chunk/block, however it still cannot be used by any other script/file/program. It will simply return nil.
Just to clear any confusion.. ^_^/>
Example:
A different program:
Just to clear any confusion.. ^_^/>
Example:
local foo = "something"
print(foo)
A different program:
print(foo)
->attempt to index (a nil value?)
Posted 24 March 2013 - 05:51 AM
Oh look at you talking all professional like with the correct terminology now ;)/> was this a copy/paste/slightly-change from the PIL? :P/>A chunk is also a block, and so local variables can be declared in a chunk outside any explicit block. The scope of such local variables extends until the end of the chunk. This means that any other program/chunk/block cannot use this variable if it is defined as 'local'. Defining something local at the top of the script will allow it to be used throughout the entire script, instead of being limited to that chunk/block, however it still cannot be used by any other script/file/program. It will simply return nil.
Just to clear any confusion.. ^_^/>
Posted 24 March 2013 - 05:52 AM
It may have been. I simply spiced it up a bit :P/> See what you can learn from resources outside of the forums ^_^/>Oh look at you talking all professional like with the correct terminology now ;)/> was this a copy/paste/slightly-change from the PIL? :P/>A chunk is also a block, and so local variables can be declared in a chunk outside any explicit block. The scope of such local variables extends until the end of the chunk. This means that any other program/chunk/block cannot use this variable if it is defined as 'local'. Defining something local at the top of the script will allow it to be used throughout the entire script, instead of being limited to that chunk/block, however it still cannot be used by any other script/file/program. It will simply return nil.
Just to clear any confusion.. ^_^/>
Posted 24 March 2013 - 05:56 AM
Haha thought so… I thought it didn't match your normal vocab :P/>It may have been. I simply spiced it up a bit :P/> See what you can learn from resources outside of the forums ^_^/>Oh look at you talking all professional like with the correct terminology now ;)/> was this a copy/paste/slightly-change from the PIL? :P/>
Yeh I love the PIL. it and the tutorials on lua-users.org are my goto places when I've forgot a functions args or something…
Posted 24 March 2013 - 05:58 AM
Pfft :P/>Haha thought so… I thought it didn't match your normal vocab :P/>
*cough* people should go here *cough*Yeh I love the PIL. it and the tutorials on lua-users.org are my goto places when I've forgot a functions args or something…
Posted 24 March 2013 - 06:00 AM
I've found it to be very vague on a lot of subjects, where the PIL and lua-users tutorials are not.*cough* people should go here *cough*
Edited on 24 March 2013 - 05:03 AM
Posted 24 March 2013 - 06:01 AM
should the local m be at the start of the api?Of course, it is after all just a string, like normal…getModems would return a table of all the sides the modem are onfunction getModems() local m = {} for _, side in pairs(rs.getSides()) do if peripheral.getType( side ) == 'modem' then table.insert( m, side ) end end return m end function openAllModems() for _, side in pairs( getModems() ) do rednet.open( side ) end end
openAllModems opens the modems :P/>/>
so all you would need to do is call
mind.openAllModems()
Posted 24 March 2013 - 06:04 AM
Meh, their Coroutine explanation is fairly decent. PIL is def better in most casesI've found it to be very vague on a lot of subjects, where the PIL and lua-users tutorials are not.*cough* people should go here *cough*
Defining it local will only allow the table to be used in that function. So, noshould the local m be at the start of the api?
Posted 24 March 2013 - 06:04 AM
nope, not in this case, we don't need to remember it for later.should the local m be at the start of the api?
I like the PIL's explanation more. its more depth into it.Meh, their Coroutine explanation is fairly decent. PIL is def better in most cases
Posted 24 March 2013 - 06:14 AM
Oh, wow. I never saw their exlpanation. Umm, that is quite in depth isn't it..I like the PIL's explanation more. its more depth into it.
Posted 24 March 2013 - 06:29 AM
indeed.Oh, wow. I never saw their exlpanation. Umm, that is quite in depth isn't it..