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

parallel.waitForAny() issue

Started by thedriver87, 09 June 2013 - 11:36 PM
thedriver87 #1
Posted 10 June 2013 - 01:36 AM
i have two functions running at the same time in a parralel.waitForAny() and the first draws an animated screensaver on the monitor like an advertisment sort of while the other listens for an rs.getInput() and then returns when it does.
problem is that the advertisement just keeps running and even when the input is left on it never goes to the rest of the code almost like its a waitforall not a waitforany. i tried ordering them in reverse and the opposite happens the rsgetinput hangs then when it gets the input the advertisment plays and never goes on to the rest of the code… very strange here is the paste bin
its the coined(psides)
and the advert() respectively im running is this some simple screw up? or did i find a bug in the code?
http://pastebin.com/QtaRn6XF
thanks for taking a look
valithor #2
Posted 10 June 2013 - 01:53 AM
I attempted to run the program to just see the problem myself and i got error nil on line 42, is there another file that it needs or does it need to be named something special?

Looked through the code and saw that the monitor needed to be on the top



Don't consider me a expert or even knowing what im talking about… but from a quick glance over the code i noticed that on line 285 you have

local function advert(speed,col1,col2,col3,col4,col5)


while on line 357 you call the variable "speed" "spd"

parallel.waitForAny(advert(spd,cl1,cl2,cl3,cl4,cl5),coined(pside))

I really don't know if this means anything as this is more advanced then anything i do
Edited on 10 June 2013 - 12:09 AM
thedriver87 #3
Posted 10 June 2013 - 02:37 AM
yes valithor this might be a little over your head. on line 285 it is a declaration of the arguments for that function. in laymens terms speed is a variable used in the function and any value placed in that arguement will become used for all instances of speed in the function an example of that in this case is
spd=4
advert(spd,cl1,etc)
will set the speed for 4 while'
advert(4,cl1,etc)
would do the same. the reason i call the spd variable is for configuration on the user end. they can change the variable spd and it will be used in place of speed in the function when its called. hope that clears it up
valithor #4
Posted 10 June 2013 - 02:37 AM
yea i noticed that was fixing to write a reply and basically completly erase that post
valithor #5
Posted 10 June 2013 - 02:44 AM
I have found something though currently it is not even running your coined(pside)) function
I tested this by adding print commands through out the function
1 before the while
1 after
and 1 after the os.pull
none of these went off, but i then changed your payed = false to payed = "false" and payed = true to payed = "true" and the while payed == true to while payed == "true" and now 2 of them go off, after i hit a key the other 1 goes off and then the first 2 again
Although when i added redstone to one of the sides nothing happened
This probably doesn't help much but i tried :P/>
theoriginalbit #6
Posted 10 June 2013 - 02:45 AM
Well your problem is actually on this line here

parallel.waitForAny(advert(spd,cl1,cl2,cl3,cl4,cl5),coined(pside))
the parallel function requires that you give it function pointers. As it stands you're giving it the return result of the functions. The reason that one runs and the other doesn't is because before parallel.waitForAny will run it first must evaluate the functions inside its function call, so whichever function is the first parameter will run, when it finishes the other will run, then once that one finishes the parallel.waitForAny will finally run, off the return values of the advert and coined functions, which I would say are both nil…
Obviously because you need to pass the function pointer it means that you cannot specify any arguments for the function either. So to resolve this problem we use anonymous functions, like so…

parallel.waitForAny( function() advert(spd,cl1,cl2,cl3,cl4,cl5) end, function() coined(pside) end)


Don't consider me a expert or even knowing what im talking about… but from a quick glance over the code i noticed that on line 285 you have

local function advert(speed,col1,col2,col3,col4,col5)
while on line 357 you call the variable "speed" "spd"

parallel.waitForAny(advert(spd,cl1,cl2,cl3,cl4,cl5),coined(pside))
I really don't know if this means anything as this is more advanced then anything i do
Since this is actually not a bug, and in fact just how programming languages work, I suggest that you read up on Lua using the PIL (google it `Lua PIL`) and the ComputerCraft wiki, before attempting to make suggestions that even you admit you have no idea about…
The reason this is not the problem

local function name( id, time, balance )
This line tells Lua that you're creating a function called `name` and then you define the names of the variables it can accept and what you will refer to them by in the function.
Then when you're calling the function

name( 5, "14:30", 56.7 )
As you can see by this we are passing hard coded values to the function, the first value (5) we refer to in the function by `arg1` etc etc for the other values. Now obviously in the sense of what thedriver has done, he has given variables to the function. Which is the same thing as what we did above, except that a variable is not hard coded, it can be changed and used all over the place.

someVar = "hello"
print( someVar )
name( someVar, "14:30", 23 )

EDIT: Also, I suggest that you take my previous suggestions to you and try to use functions to reduce the amount of duplicate code that you type, for example your entire advert function I refactored for you and it comes up with this http://pastebin.com/Vksi0ZbR now the good thing about splitting out these functions, is it also made you able to use these in other sections of your code too. this concept is known in programming as Functional Decomposition and it is a very handy thing to get used to doing! Also notice how to make it more compatible with the rest of your script I made all the calls to `term.` instead of `mon.`, now all that is needed to print on the monitor is a terminal redirect, which can be seen at the bottom of the example script. Hope this helps, and as always, any questions just ask here or in the PM we have going.
Edited on 10 June 2013 - 01:26 AM
Azzar #7
Posted 16 June 2013 - 12:58 AM
Well your problem is actually on this line here

parallel.waitForAny(advert(spd,cl1,cl2,cl3,cl4,cl5),coined(pside))
the parallel function requires that you give it function pointers. As it stands you're giving it the return result of the functions. The reason that one runs and the other doesn't is because before parallel.waitForAny will run it first must evaluate the functions inside its function call, so whichever function is the first parameter will run, when it finishes the other will run, then once that one finishes the parallel.waitForAny will finally run, off the return values of the advert and coined functions, which I would say are both nil…
Obviously because you need to pass the function pointer it means that you cannot specify any arguments for the function either. So to resolve this problem we use anonymous functions, like so…

parallel.waitForAny( function() advert(spd,cl1,cl2,cl3,cl4,cl5) end, function() coined(pside) end)


Don't consider me a expert or even knowing what im talking about… but from a quick glance over the code i noticed that on line 285 you have

local function advert(speed,col1,col2,col3,col4,col5)
while on line 357 you call the variable "speed" "spd"

parallel.waitForAny(advert(spd,cl1,cl2,cl3,cl4,cl5),coined(pside))
I really don't know if this means anything as this is more advanced then anything i do
Since this is actually not a bug, and in fact just how programming languages work, I suggest that you read up on Lua using the PIL (google it `Lua PIL`) and the ComputerCraft wiki, before attempting to make suggestions that even you admit you have no idea about…
The reason this is not the problem

local function name( id, time, balance )
This line tells Lua that you're creating a function called `name` and then you define the names of the variables it can accept and what you will refer to them by in the function.
Then when you're calling the function

name( 5, "14:30", 56.7 )
As you can see by this we are passing hard coded values to the function, the first value (5) we refer to in the function by `arg1` etc etc for the other values. Now obviously in the sense of what thedriver has done, he has given variables to the function. Which is the same thing as what we did above, except that a variable is not hard coded, it can be changed and used all over the place.

someVar = "hello"
print( someVar )
name( someVar, "14:30", 23 )

EDIT: Also, I suggest that you take my previous suggestions to you and try to use functions to reduce the amount of duplicate code that you type, for example your entire advert function I refactored for you and it comes up with this http://pastebin.com/Vksi0ZbR now the good thing about splitting out these functions, is it also made you able to use these in other sections of your code too. this concept is known in programming as Functional Decomposition and it is a very handy thing to get used to doing! Also notice how to make it more compatible with the rest of your script I made all the calls to `term.` instead of `mon.`, now all that is needed to print on the monitor is a terminal redirect, which can be seen at the bottom of the example script. Hope this helps, and as always, any questions just ask here or in the PM we have going.

Above all of this, I just wanted to add that the Parallel function seems to be a bit buggy at times. I have to put 1 sec delay in some fuctions to avoid a call without yielding error even when the function yields. Not sure if this is a result of the way events are pulled when calling the function or just the speed at which the two functions are executed.
theoriginalbit #8
Posted 16 June 2013 - 01:09 AM
Above all of this, I just wanted to add that the Parallel function seems to be a bit buggy at times. I have to put 1 sec delay in some fuctions to avoid a call without yielding error even when the function yields. Not sure if this is a result of the way events are pulled when calling the function or just the speed at which the two functions are executed.
Chances are you have a problem somewhere in your code, I've used parallel/coroutines heaps and never has it been `buggy`.