Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

List of Timers

Discussion in 'Scripting' started by HalDevOne, Mar 5, 2019.

  1. HalDevOne

    HalDevOne

    Joined:
    Apr 12, 2014
    Posts:
    67
    Hi guys!

    So i would like to create something like this. When my enemy gets killed it goes to a qued list that gets into a timer and when the time is reached it spawns a new enemy. And if another enemy gets killed it goes to the list that starts another counter.

    Basicly i just want the time delay on every enemy that gets killed to spawn it again and and iam trying to do this from a central script becouse the actully enemy is destroyed.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    Create a simple data class that holds a float and a Action delegate.

    On death stick an instance of these into a list, setting the float to the duration, and the action to what occurs at the end of the timer.

    On update decrement the timers, any less than 0 get removed from the list and the Action called.

    ...

    There are optimizations from here, such as ordering your list so that you don't have to loop the entire thing. In this you'd set the float to the 'endtime', insert it sorted into the list, and then on update you only have to check if the first entry is > current time.

    You can see a rough version of this here:
    https://github.com/lordofduct/space...lob/master/SpacepuppyBase/Timers/BulkTimer.cs
     
  3. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Or just disable your enemies instead of destroying them so you can reuse them.
     
  4. HalDevOne

    HalDevOne

    Joined:
    Apr 12, 2014
    Posts:
    67
    Yeah that wasnt even the question here but thx dude
     
  5. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    If you dont destroy your enemy you can call the timer on him, just a suggestion... sorry bout suggesting :D

    Edit: or you can just start a coroutine and pass that enemy to it to be reactivated after some yield time. Attention! Another suggestion ;)
     
  6. Zeldarck

    Zeldarck

    Joined:
    Feb 8, 2017
    Posts:
    10
  7. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
  8. Zeldarck

    Zeldarck

    Joined:
    Feb 8, 2017
    Posts:
    10
    Hum, you think is better to have for example one timer manager with a list of timer and only one update on this timermanager which increment timers?
     
  9. HalDevOne

    HalDevOne

    Joined:
    Apr 12, 2014
    Posts:
    67

    All suggestions are welcome! Are the coroutines a good solutions for this, garbage collection? And how would you set that up with multiple dead enemies calling the same coroutine?
     
  10. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    well the way i would do it is to have a struct that contains the enemy object and a float value for a timer. simply said when a enemy dies it goes into a list/array depending on requirement of unknown amount of enemies or if you have known amount.

    enemy dies > goes into collection > starts a thread that manage only that specific instance. and for every time a enemy dies a thread is then started.

    so two collections
    one for threads. pre filled for example if u have 50 enemies. you know you want to have 50 threads ready.
    one for the enemies structs that contains a float and the object.

    basicly we are using object pools here to make it far more efficent, since starting/destroying objects are fairly slow and requires alot of uncessory resources

    the thread obviously shouldn't care about unity objects at all cause its not thread safe. all it should care about it updating the float value timer.

    this way its fairly efficent and multiple threads will be used to handle it.
     
    Last edited: Mar 8, 2019
    HalDevOne likes this.
  11. HalDevOne

    HalDevOne

    Joined:
    Apr 12, 2014
    Posts:
    67
    When you say thread do you mean a method? I like the idea do you have a simple quick example just so that i can picture it?
     
  12. simonlvschal

    simonlvschal

    Joined:
    Nov 17, 2015
    Posts:
    266
    no i mean threading. basicly a thread allows you to handle more things at once. but just remember threads in unity are not thread safe if they are used on Unity Objects.

    i can try and make a example of it
     
    HalDevOne likes this.