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
0
Question by Thelulz · Oct 26, 2020 at 06:27 AM · turn-based

Is there a way i can make a function that gets called every round in a turn based RPG?

I'm currently making a turn based RPG and i want a way to track how many rounds have passed since a certain action.

void Update(); gets called every frame and you can access it from any script.

I want to do the same thing but it'll be something like NewRound(); and gets called every round.

Maybe there's a better way to do this idk. Any answers would be much appreciated :)

Comment
Add comment · Show 1
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 Klarzahs · Oct 26, 2020 at 08:22 AM 0
Share

How do you start a new round in your game? Do you wait for e.g. a "space" press? If so, you can create a global script RoundHelper and put your NewRound() there. Then you just call RoundHelper.NewRound() if someone presses space.
$$anonymous$$aybe this post can give you a better idea of whats possible otherwise

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Duckocide · Oct 26, 2020 at 10:34 AM

@Thelulz

Look at Coroutines. They are a great way of running things in parallel to the main unity lifecycle.

So, in your Monobehaviour component add the following to the "OnEnabled" method, something like (all this code is untested and typed in to the forum straight!)...

 StartCoroutine(MyTurnEngineCR());

Then add a private IEnumerator method with something like...

 public bool turnComplete = false;
 public int turnCount = 0;
 private bool _turnEngineActive = true;
 private IEnumerator MyTurnEngineCR()
 {
    while(_turnEngineActive)
    {
       yield return new WaitForEndOfFrame();
       turnCount += 1;
       Debug,Log($"Turn {turnCount} started...");
       // Has the turn completed?
       while (!turnComplete)
       {
           yield return new WaitForEndOfFrame();
       }
       turnComplete = false;
       Debug,Log($"Turn {turnCount} Completed!");
 }

This will sit there waiting every frame to see if the boolean "turnOver" has been set. If it has, it will clear that flag and increment the turnCount. The yield lines are really important as they hand processing back to the main unity thread. Generally (other than what you do in them), Coroutines have little performance cost (I often use them for enemy AI's).

The private _turnEngineActive bool is simply to keep the coroutine looping (if you set it false, it will stop once a turn is over).

Hope this helps. Coroutines are important for anything that can happen outside of the Update() etc unity cycle loop. Recommend you read up on them. Once you get used to them, they are so so useful for all sorts of things like AI, monitoring and decision making in a game.

Comment
Add comment · 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

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

139 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

Related Questions

Turn-based vehicle physics 1 Answer

Is this the right program 0 Answers

Business simulation in Unity 1 Answer

Detect the same variable from multiple scripts 2 Answers

how to create turns between 2 players 0 Answers


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