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

Timeline based event system with quirks.

Discussion in 'Scripting' started by TheOddPotion, Jun 25, 2017.

  1. TheOddPotion

    TheOddPotion

    Joined:
    Jul 12, 2016
    Posts:
    12
    Hi all,

    So I'm working on a for-purpose event system that is loosely based on a timeline. Something like this:



    So basically the left white node is the start node, the right white node is the end node. Let's say the end node is at 120 seconds or two minutes. As the game proceeds events are triggered for each red node, sometimes multiple events. At every point a blue diamond is reached an event happens. For example, a button appears. IF the player decides to push the button then all of the events that should have been triggered on the "parent" timeline should be ignored and instead the events on the new timeline should trigger. At the end of this new timeline the events on the main timeline should be triggered again. Something like this:



    In the image above the red X's mark events that were ignored and you can see that the green diamond is bypassed by the player. Like the blue diamond this event triggers an event, like spawning a button, but instead of "switching" to a new timeline when the button is pressed the timeline is additive. So both events on the main timeline and the triggered timeline are fired. Therefore, IF instead of bypassing the green diamond they activate it:



    All events on the green line should be triggered based on time.

    The way I see it is that there are two ways of doing this: Pure scripting or using Unity 5's built in Animator State Machine and Animation Events. Each separate timeline is a Animator State which is an animation with events strewn along it. Each alternate timeline is another State. So when the player hits the first blue diamond, which activates an alternate timeline, then a state transition occurs and everything is peachy. The problem is when the alternate timeline(animation state) ends I would like to return to the main timeline at the point in time that the alternate timeline ends. So if the player enters the alternate timeline at 30 seconds, and the alternate timeline lasts for 20 seconds, then the player should return to the main timeline at 50 seconds whenever the alternate timeline ends. *whew* As far as I know you can only restart the animation.

    The other way through pure scripting would be more logical and flexible, though I would have to write a custom inspector probably to edit the timelines more easily. The only real issue I have with just scripting is this.

    So I imagine having a couple of dictionaries probably, with the time to fire the event being the key and the event to be fired being the value. And then on some objects Update check the current time to the next entry in the dictionary and if the current time is >= to the key of the next entry then remove that entry and fire the event. The issue with this is that there is a possibility of an event being fired a few cycles after it's supposed to. So I'm a bit stuck on exactly how to do the time check through without being too inaccurate.

    If anyone has any resources on how to fire events at set times or even something like a tower defense game enemy spawning system where the levels ARE NOT random and instead level has a list of enemies and a list of times to spawn that enemy I would be incredibly grateful. All I am able to find are random spawn managers which is the closest development concept I can find to my idea.

    Thanks!