Hi All,
Im stuck on how to yield between dealing cards for example once the dealer button is clicked deal out 4 cards with a 1 second delay between them.
I’ve been reading your not supposed to use yield WaitForSeconds in Update or FixedUpdate so that means I can’t use yield in OnGUI either because OnGUI is called twice per frame. So how would I solve this? I’ve tried to figure out as many different ways that I can think of but to no avail. I could use some expertise in figuring this out.
Here is my last attempt so far:
function OnGUI()
{
if (GUI.Button (Rect (0,0,100,35), "Deal"))
{
Card1();
}
}
function Card1()
{
var newCard1 : GameObject = DealCard();
Wait();
Card2();
}
function Card2()
{
var newCard2 : GameObject = DealCard();
Wait();
Card3();
}
//**** I did the same for Crad3 and Card4 ****//
function DealCard() : GameObject
{
if(cards.Count==0)
{
ResetDeck();
}
var card : int = Random.Range(0, cards.Count-1);
var go : GameObject = GameObject.Instantiate(cards[card]) as GameObject;
cards.RemoveAt(card);
return go;
}
Its generally a good idea to separate logic and UI.
Structurally you are unlikely to want different functions for different cards. Instead have a deal function that takes the number of cards to deal:
public List<Card> DealCards(int count) {
List<Card> cards = new List<Card>(); // Its probably worth making a class to represent your cards instead of using gameobjects
for (int i = 0; i < count; ++i) cards.Add(DealCard()); // Similar to existing but return the card not the GO
return cards;
}
After this function executes trigger a view type class which shows your cards:
private IEnumerator DoShowDeal(List<Card> cards){
foreach (Card card in cards){
PlayCardAnimation(card);
yield return new WaitForSeconds(animationDelay); // Or you could receive an event at the end of the animation which triggers the next animation
}
}
public void ShowDeal(List<Card> cards){
StartCoroutine(ShowDeal(cards));
}
And in your button (grr hate unity GUI):
if (GUI.Button (Rect (0,0,100,35), "Deal"))
{
List<Card> cards = DealCards(5);
tableView.ShowDeal(cards);
}
Just a shadow of an idea, but maybe it will get you thinking along the right path.
Greatly Appreciated JohnnyA!!!
Im trying it out right now, Again thank you for taking the time to write an excellent reply. Amazing (and smart) people in these forums.