ID:140311
 
mob/verb/Vote()
var/Yes=0
for(var/mob/P in Players)
spawn if(P)
switch(input(P,"Do you want pizza?") in list("Yes","No"))
if("Yes") Yes+=1
spawn(100) world<<"There were [Yes] 'Yes' answers"


Ok so this is just an example not my real code but it still reproduces the problem.

It is supposed to pass a prompt to all players and they vote if they like pizza. Then 10 seconds later the answer is output.

The problem is, no matter how many people click yes, it always says the answer is 0. It says the Yes=0 at the end even when many people chose Yes as the answer.

This is not a problem with the Players list, people ARE getting the prompts, and they are choosing their answer before the spawn(100) is completed.

if I replace the spawn(100) with sleep(100) the result is still the same so that is not it either.

Help is appreciated.

Even without the prompt like below it STILL says 0.
mob/verb/Vote()
var/Yes=0
for(var/mob/P)
spawn if(P)
Yes+=1
spawn(30) world<<"There were [Yes] 'Yes' answers"

I can't find any libraries for voting systems either.
I'm thinking that the local var/Yes is losing scope via the spawn() calls. This may very well be a bug as it isn't intuitive, but it's a rather confusing process if you think about the order of operations here.

Try making the var/Yes a global (by moving it outside of the block). That's a bit hacky but it should fix your problem.
In response to Tom
Tom wrote:
I'm thinking that the local var/Yes is losing scope via the spawn() calls. This may very well be a bug as it isn't intuitive, but it's a rather confusing process if you think about the order of operations here.

Try making the var/Yes a global (by moving it outside of the block). That's a bit hacky but it should fix your problem.

That would mean only 1 voting process could be going on at once which I was hoping to avoid.

That was my last resort but I'll resort to it for now.

So should I report this as a bug or is your awareness of it good enough?

Thanks
In response to Dragonn
Dragonn wrote:
That would mean only 1 voting process could be going on at once which I was hoping to avoid.

You can workaround this by instead using a temporary structure to pass the votes around, eg:
vote
var/Yes

mob/verb/Vote()
var/vote/V = new
for(var/mob/P)
spawn
V.Yes++
spawn(30) world<<"There were [V.Yes] 'Yes' answers"


I think that'd work.

So should I report this as a bug or is your awareness of it good enough?

Go ahead and report it (and the workaround if it indeed resolves the issue) so we have a record. Most likely we'll just have to defer it but we can at least correct the documentation to indicate this behavior for automatic variables. I consider Dan's spawn and sleep system one of the more innovative parts of the language but it's also not well understood.
mob/verb/Vote()
var/Yes=0
for(var/mob/P in Players)
if(P)
switch(input(P,"Do you want pizza?") in list("Yes","No"))
if("Yes") Yes+=1
world<<"There were [Yes] 'Yes' answers"


This works for me. What I think is happening is since you are using spawn() in the for loop it is going directly to what is suppose to be executed after the for() loop, before the variable can be changed. If you take the spawn() off of the output, but keep the one in the for() loop you will see it goes straight to the output and ignores the for() loop.

If you must use spawn in the for() loop, use a variable add 1 to it after every vote. Then check if it equals players.len, if so then display the output.

mob/verb/Vote()
var/Yes=0
var/No=0
for(var/mob/P in Players)
spawn if(P)
switch(input(P,"Do you want pizza?") in list("Yes","No"))
if("Yes") Yes++
if("No") No++
if(Yes+No==Players.len)
world<<"There were [Yes] 'Yes' answers"
In response to Ulterior Motives
The spawn is necessary to make all players get the prompt at once. Otherwise it only prompts 1 player at a time and won't prompt another til the former one chooses an answer.

I'll look over the rest of your post but for now gtg...Thanks
In response to Dragonn
Although this may not a solution to your code snippet, I suggest you use Shadowdarke's sd_Alert for timed inputs.
In response to Axerob
Thanks I never knew that existed.