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

variables in names

Started by blipman17, 10 June 2012 - 07:45 AM
blipman17 #1
Posted 10 June 2012 - 09:45 AM
hello, i'm having a program like this:

–[[

this programs dispences (how mutch) out of the chest
it still needs some adjustments

]]–
print( "starting program chestdispencer" )
sleep(1)
local quantity = 10
print("going to dispence ", quantity, " items in a row")
sleep(1)
print( "starting dispencing" )
repeat
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right",false)
quantity = quantity-1
print (quantity, " to go")
until quantity ==0
sleep(1)
print("all dispenced")
sleep(1)
print("ending program")

i'm using buildcraft and i'm going to use this program on a computer next to a storage chest.
i want another computer to let this computer use this program, but for that i want to make a variable after the name chestdispencer.
so if i say chestdispencer 43, i want it to dispence 43 items.
and how do i let it return the value true if it gets used in another program?
toxicwolf #2
Posted 10 June 2012 - 11:07 AM
By adding the line

return true
to the end of your code, the program would return the boolean value of true to whatever script ran it.
I think that is what you meant, sorry if it wasn't any help.
Lyqyd #3
Posted 10 June 2012 - 06:01 PM
You want to handle arguments passed to the program (similar to the way the edit program is passed a filename telling it which file you want to edit). To do this, use something like:


local tArgs = {...}
--this declares a table (tArgs) that holds all of the arguments passed to the program.
if #tArgs < 1 then
    --if the number of items in the table is less than one.
    print("Usage: chestdispenser <item count>")
    return false
    --print the usage message and end the program.
end

You'd want to place this at the beginning of your code. The variable containing the first argument (43 in your example above) would be tArgs[1]. You can look into some of the rom programs for more information.
blipman17 #4
Posted 11 June 2012 - 07:25 PM
you guys are realy helpful,
thank you for that
that return true is good to know.
but what if you have more than one argument?
i know what tables are, but i don't control them completely.

in this case i just need one argument, but with other scripts i need more.
OmegaVest #5
Posted 11 June 2012 - 07:45 PM
The opening arguments are stored in numerical order starting with one. So, to call them from the table, use tArgs where i is the slot number of the piece you are trying to find.

So, to start a GPS Host program, similar to the function of another, pre-loaded program, the top of your program would look like this:


tArgs = {...}

x = tArgs[1]
y = tArgs[2]
z = tArgs[3]


Though, you could also use x, y, z = tArgs.
blipman17 #6
Posted 11 June 2012 - 08:05 PM
this is realy helpful, thank you all a lot
MysticT #7
Posted 11 June 2012 - 08:27 PM
you guys are realy helpful,
thank you for that
that return true is good to know.
but what if you have more than one argument?
i know what tables are, but i don't control them completely.

in this case i just need one argument, but with other scripts i need more.
If you want to return more than one value, just add them after the first one, sepparated by commas. Like this:

return value1, value2, value3, ..., valueN