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

Obvious Question :S (Paralell API)

Started by LucasUK, 13 August 2012 - 05:39 PM
LucasUK #1
Posted 13 August 2012 - 07:39 PM
Here's my code so far, I want to be able to use (at the moment print to test but also other things) either the message typed in, or the message received as a variable basically. (assigned to a)

Currently it returns either 1 (Received message) or 2 (Input message). The send or receive bit work fine!!

Please guide me!



function initialise (vRednetSide, vElevatorSide)
rednet.open (vRednetSide)
redstone.setOutput(vElevatorSide, false)
end
function Listen ()
senderId, message, distance = rednet.receive()
print (message)
return message
end

function selectFloor ()
write ("Enter floor: ")
vFloor = read()
rednet.broadcast(vFloor)
return vFloor
end

initialise ("back", "bottom")
a = parallel.waitForAny (Listen, selectFloor)
print (a)
ardera #2
Posted 13 August 2012 - 08:01 PM
If i understood (<– don't know if this irregular verb is right) right: you want to get the return of the functions given to parallel.waitForAny ? then here is my solution:

ret=""
function initialise (vRednetSide, vElevatorSide)
rednet.open (vRednetSide)
redstone.setOutput(vElevatorSide, false)
end
function Listen ()
senderId, message, distance = rednet.receive()
print (message)
ret=message
return
end

function selectFloor ()
write ("Enter floor: ")
vFloor = read()
rednet.broadcast(vFloor)
ret=vFloor
return
end

initialise ("back", "bottom")
a = parallel.waitForAny (Listen, selectFloor)
print (a)
-- return of functions is variable ret
LucasUK #3
Posted 13 August 2012 - 08:06 PM
Works a charm thanks!, any way of doing this as an object without creating a global variable?

No rush as above solution is good workaround :P/>/>
ardera #4
Posted 13 August 2012 - 08:19 PM
only replace ' ret="" ' with ' local ret="" ' :P/>/>
LucasUK #5
Posted 14 August 2012 - 05:54 PM
I mean can I read the return values of the functions directly? Rather than create a variable and have the function edit this.

This means I can code functions independently of these variables.
bjornir90 #6
Posted 28 November 2012 - 09:11 AM
I mean can I read the return values of the functions directly? Rather than create a variable and have the function edit this.

This means I can code functions independently of these variables.
I'm very sorry for the very old thread deterring but I want to get an answer to that same question and don't want to create a new topic because I think I have already made alot.