Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by stereosound · Aug 11, 2012 at 02:13 PM · javascriptcoroutinepausing

'pausing' a game to wait for user to select object before proceeding

I'm creating a card game, and I have run into an issue of 'pausing' my code while I wait for a player to make a decision in-game. As a background, I create a list of all the plays for the turn, and they are stored (and sent to opponent) with six pieces of information -- who's playing it, the card itself (so I know what the card does), the location of the play (hand/field/etc), the position in that location (card #1 in hand, etc), as well as where it's being played and that position. This is all saved in a list, and I evaluate the list when until it's empty.

So for example: (true, card1, Hand, 1, Field, 1) (true, card2, Hand, 2, Field, 2) (false, card1, Hand, 1, Field, 3)

Would play the first card in my hand to first field position, then second card in my hand to field position, and finally the first card in an opponent's hand to position #3.

This works great if I know all plays ahead of time -- I just have a laundry list of what to do and I evaluate through it, no problem.

BUT I'd like to add in the ability to have responsive effects -- say for example, I have a card that when it dies I can deal 3 damage to target unit. I'd need to 'interrupt' the flow on both my client and the opponent's client when we reach this point until the controlling player makes his decision on who'd he'd target, send it to the opponent, and then we can both proceed. As sample code, here's what I currently do

 function EvalStack() : IEnumerator {
  //step through the order
  while (stack.length > 0) {
  var toplay : GameObject = stack.Shift();
  yield StartCoroutine(sAbilities.EvalCard(toplay));
  yield WaitForSeconds(0.75);
  }
 }

This will play out every card in the stack array (it has a script component that stores all relevant information) one at a time. EvalCard snippet looks like this:

 function EvalCard(card : GameObject, isplayer : boolean) : IEnumerator {
  CV = card.GetComponent(cardvars);
  if (CV.ID == 1) {
  //If you have less than 4 resources, draw a card.
  if (isplayer) {
  if (Pvars.Rtotal < 4) {
  sDeck.DrawCard();
  }
  }
  else {
  if (Pvars.oRtotal < 4) {
  sDeck.oDrawCard();
  } 
  }
  }
 }

What I would like to do is something like this:

 var choice : int;
 var opponent_choice : int;
 var choicemade : boolean;
 var opponent_choicemade : boolean;
 
 function EvalCard(card : GameObject, isplayer : boolean) : IEnumerator {
     CV = card.GetComponent(cardvars);
     if (CV.ID == 1) {
         //When you play, choose a unit and deal 3 damage to it.
         if (isplayer) {
             yield StartCoroutine(WaitForChoice());
             yield StartCoroutine(DealDamage(choice));
         }
         else {
            yield StartCoroutine(WaitForOpponentChoice());
            yield StartCoroutine(DealDamage(choice));
     
         }
     }
 }
 
 function WaitForChoice() : IEnumerator {
    choicemade = false;
    while (!choicemade) {
       var hit : RaycastHit;
       var itsaray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
       if (Physics.Raycast (itsaray, hit) && Input.GetMouseButtonDown(0)) {
          choice = hit.collider.gameObject.GetComponent(cardvars).slot;
          choicemade = true;
       }
    }
 }

But this will freeze unity due to the while loop without 'foreseeable' end. So, is there any way to feasibly make that function work?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Aug 11, 2012 at 02:25 PM 0
Share

You should concentrate on what you want to achieve and not on your attempts which doesn't work ;)

A Corouine can't return anything. That's mainly because a coroutine isn't a function, it's an object. The coroutine-function acts like a constructor for the coroutine. It always returns an IEnumerator.

As i said, it would be more important to know what exactly you wanna do. You want to evaluate card and pause a coroutine, but what the coroutine does or what you actually want to evaluate isn't clear at all.

Ask specific and detailed questions ;)

Feel free to edit your question at any time.

avatar image stereosound · Aug 11, 2012 at 08:10 PM 1
Share

I'll post here before I edit -- you are totally right I can't return from a coroutine, that was a bad error on my part. I'm going to put up a hypothetical scenario and code, and where my issue arises. Let me know if it's unclear still.

Edit: done

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TheDavil86 · Aug 11, 2012 at 03:23 PM

Put 'yield;' in the end of your while loops, this allows other processes to continue running without waiting for the while loops to finish.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image stereosound · Aug 11, 2012 at 09:54 PM 0
Share

haha i should have looked at the comments before I updated! this works perfectly -- so simple!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unityscript Coroutine Not Running When Called 1 Answer

Coroutine gets skipped in javascript/unityscript 0 Answers

yield behaves oddly with anonymous functions 1 Answer

Make a coroutine run when Time.timeScale = 0; 3 Answers

yield works, but yield WaitForSeconds does not? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges