Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by osje44 · 6 days ago · coroutineeventcoroutineseventscallback

Waiting for and using output of a UnityEvent within coroutines


I would like to wait for a UnityEvent in my coroutine AND use the output from said event in follow up execution of my code. Below is the draft code for my coroutine that waits until a card is selected by the player, triggered by an UnityEvent, and uses said card to do something (discarding / destroying / copying the card).


 public IEnumerator SelectionRequest()
 {
     Card card = null;

     // Wait until user has selected appropriate cards
     yield return WaitForSelection(returnValue =>
     {
         card = returnValue;
     });

     // Check if cards where selected
     if (card == null)
     {
         // Selection is aborted by user, any following code should not be executed
         yield break;
     }

     // Selection is performed correctly, 
     // Interact with the selected card in some way ...
 }


The problem lies in the WaitForSelection method that i have defined, which i would like to return the Card object that was passed as input argument to a UnityEvent.


I've already set up a class to expose the UnityEvent for easy editing in the editor, however i have not been able to wait for the event and use this variable from within the coroutine shown above.


 [System.Serializable]
 public class CardEvent : UnityEvent<Card[]>
 {
 }


The goal of this code is to be able to implement cards with more exotic effects in my card game. From cards that allow the player to discard other cards in their hand, to cards that allow players to destroy a card in hand and select a card that costs 2 more to add to their deck.


I'm open to other suggestions if you think coroutines for the card effects, and UnityEvents for selection are not the way to go for this functionality.


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 Hellium · 6 days ago 0
Share

Why using a coroutine instead of listening to the event directly?

avatar image osje44 Hellium · 6 days ago 0
Share

But how do i then set up the different effects of different cards? My idea was to have these different effects in unique coroutines where needed, and hook up to the event to get the card selected when this is needed. I don't see how i can do this without events or bloating all of the code...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · 6 days ago

You can use something similar to what I've implemented over here. Specifically a custom yield instruction that simply checks a boolean value that is set to true when the event has happened. My WaitForUIButtons class also takes care about registering and removing the callback from the button on click event. You could implement a similar class for wrapping around a UnityEvents (or multiple in case there are several choices).

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 osje44 · 6 days ago 0
Share

Thanks, that does seem useful. But after some consideration i realized i could perhaps more easily do everything i need using unity events. Or at least it seems more easily scalable then your solution.

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

152 People are following this question.

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

Related Questions

When to use 'delegate', 'event' or 'Action' ? 1 Answer

How to change this using a coroutine 1 Answer

StartCoroutine by another class (Coroutine inside Coroutine - Instance class) 1 Answer

Argument 1: cannot convert from 'method group' to 'UnityAction' 1 Answer

Coroutine is jerky after exiting trigger zone 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