Hi there!
I am programming a little card game for learning purposes. I stepped into a little problem, where I don't know the best practice howto solve it.
If the player (me) throws a certain card, an additional dialog pops up and asks the player which face (spades, diamonds...) he wants from the opponent as the next card, so the opponent is forced to serve this face.
The pseudo code looks like this
function playerMove(playercards, opencardOnStack){
foreach(playercards as card){
if(card==opencardOnStack){ success=1; playerWantToThrowthisCard=card }
}
if(success==1){
if(playerWantToThrowthisCard == "Jack"){ -- the Jack is the special card which enables the player to wish a face from the opponent
doWait = true
dialog = Dialog
dialog:addEventListener("click", function(){ print("clicked"); doWait=false}
dialog:showDialog(wishedFaceFromOpponent = function(){ print("which face do you want from the opponent"); return function readFace() }
while doWait==true
end
print("the player wants the opponent to throw a" .. wishedFaceFromOpponent)
}
}
nextPlayersTurn()
} |
So I have implemented a kind of "active waiting" which is bad programming practise and additionally it doesn't work, because it hangs my simulator.
Of course I could pack the further statements, which should be executed after "clicked" into that event listener, but that doesn't work with my code well, because it consists of many nested subroutines, beside that it would branch my code even more. For this to work, I'd had to completely redesign my code only for this single purpose, that cannot be the only solution.
I googled but cannot seem to find the right way to do this. The function I'm seeking for is something like waitForEventCompleted(), which halts the further execution as long as the user clicked something in the dialog. This functions should substitute the "while doWait==true end" active wait statement. I guess this "waiting" thing is something different in event-driven environments.
Comments
https://deluxepixel.com
Likes: qxmat2
Likes: antix, SinisterSoft
https://play.google.com/store/apps/developer?id=razorback456
мій блог по гідерос https://simartinfo.blogspot.com
Слава Україні!
Likes: antix
https://deluxepixel.com
http://docs.giderosmobile.com/reference/gideros/Core/asyncCall#Core.asyncCall
You can have multiple fake threads running, when it reaches the end of the code it's automatically removed. It's akin to time slices, not parallel.
Likes: oleg
https://deluxepixel.com
Likes: SinisterSoft
http://giderosmobile.com/forum/discussion/comment/48666#Comment_48666
Likes: oleg
https://github.com/gideros/gideros
https://www.youtube.com/c/JohnBlackburn1975