This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Appleeater's profile picture

[API]Have I done this right?

Started by Appleeater, 24 March 2013 - 03:34 AM
Appleeater #1
Posted 24 March 2013 - 04:34 AM
Hello, I'm trying to create my own api. Am I doing this correctly?


http://note.io/15Ebb1t
theoriginalbit #2
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…
Appleeater #3
Posted 24 March 2013 - 04:38 AM
ok, how can i let my variables accsessable by other program's?
theoriginalbit #4
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.
Appleeater #5
Posted 24 March 2013 - 04:48 AM
So if I just do


variable = "test"
instead of

local variable = "test"
theoriginalbit #6
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).
Appleeater #7
Posted 24 March 2013 - 05:08 AM
(I can see a bug in getRSinputs).

and what is that bug?
theoriginalbit #8
Posted 24 March 2013 - 05:11 AM
and what is that bug?
You are using local in front of the variables, meaning that after the function has run all those values will be forgotten…

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
Appleeater #9
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?
theoriginalbit #10
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?
Appleeater #11
Posted 24 March 2013 - 05:29 AM
so it looks all of the sides and stores in the table what their state are
theoriginalbit #12
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' }
Appleeater #13
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)
theoriginalbit #14
Posted 24 March 2013 - 05:43 AM
Of course, it is after all just a string, like normal…

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 on
openAllModems opens the modems :P/>

so all you would need to do is call
mind.openAllModems()
SuicidalSTDz #15
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:

local foo = "something"
print(foo)

A different program:
print(foo)
->attempt to index (a nil value?)
theoriginalbit #16
Posted 24 March 2013 - 05:51 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.. ^_^/>
Oh look at you talking all professional like with the correct terminology now ;)/> was this a copy/paste/slightly-change from the PIL? :P/>
SuicidalSTDz #17
Posted 24 March 2013 - 05:52 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.. ^_^/>
Oh look at you talking all professional like with the correct terminology now ;)/> was this a copy/paste/slightly-change from the PIL? :P/>
It may have been. I simply spiced it up a bit :P/> See what you can learn from resources outside of the forums ^_^/>
theoriginalbit #18
Posted 24 March 2013 - 05:56 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/>
It may have been. I simply spiced it up a bit :P/> See what you can learn from resources outside of the forums ^_^/>
Haha thought so… I thought it didn't match your normal vocab :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…
SuicidalSTDz #19
Posted 24 March 2013 - 05:58 AM
Haha thought so… I thought it didn't match your normal vocab :P/>
Pfft :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…
*cough* people should go here *cough*
theoriginalbit #20
Posted 24 March 2013 - 06:00 AM
*cough* people should go here *cough*
I've found it to be very vague on a lot of subjects, where the PIL and lua-users tutorials are not.
Edited on 24 March 2013 - 05:03 AM
Appleeater #21
Posted 24 March 2013 - 06:01 AM
Of course, it is after all just a string, like normal…

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 on
openAllModems opens the modems :P/>/>

so all you would need to do is call
mind.openAllModems()
should the local m be at the start of the api?
SuicidalSTDz #22
Posted 24 March 2013 - 06:04 AM
*cough* people should go here *cough*
I've found it to be very vague on a lot of subjects, where the PIL and lua-users tutorials are not.
Meh, their Coroutine explanation is fairly decent. PIL is def better in most cases


should the local m be at the start of the api?
Defining it local will only allow the table to be used in that function. So, no
theoriginalbit #23
Posted 24 March 2013 - 06:04 AM
should the local m be at the start of the api?
nope, not in this case, we don't need to remember it for later.


Meh, their Coroutine explanation is fairly decent. PIL is def better in most cases
I like the PIL's explanation more. its more depth into it.
SuicidalSTDz #24
Posted 24 March 2013 - 06:14 AM
I like the PIL's explanation more. its more depth into it.
Oh, wow. I never saw their exlpanation. Umm, that is quite in depth isn't it..
theoriginalbit #25
Posted 24 March 2013 - 06:29 AM
Oh, wow. I never saw their exlpanation. Umm, that is quite in depth isn't it..
indeed.
Appleeater #26
Posted 24 March 2013 - 06:42 AM
Is my updated version correct? Pastebin