question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to implement behavior like StartCoroutine by Entitas?

See original GitHub issue

Hi there, I’m migrating my game to using Entitas, what I have now is HitComponent and my code rule is, when entity.isHit = true I need start a coroutine, waiting for 1 second and this coroutine’s going to set entity.isHit = false.

Since my system is not extending MonoBehavior, I can’t access StartCoroutine function, how can I do this by Entitas way?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
FNGgamescommented, Apr 19, 2018

You could try this:

Give HitComponent a field (float cooldownTime) then do entity.AddHit(cooldownTime). Then have a HitCooldownSystem : IExecuteSystem that iterates through the group of entities with hit components, subtracting the delta time from the timer value. When this hits 0, remove the hit component.

public class HitComponent : IComponent {
   public float cooldownTime;
}

public class HitCooldownSystem : IExecuteSystem {
   readonly IGroup<GameEntity> _hitGroup;

   public HitCooldownSystem(Contexts contexts) {
       _hitGroup = contexts.game.GetGroup(GameMatcher.Hit);
   }

   public override void Execute() {
       foreach (var e in _hitGroup.GetEntities()) {
           e.ReplaceHit(e.hit.cooldownTime - deltaTime);
           if (e.hit.cooldownTime <= 0) e.RemoveHit();
       }
   }
}
0reactions
sschmidcommented, Apr 23, 2018

@lai32290 @FNGgames summarized pretty good (as always 😉) first answer is a very common way to deal with timers and cooldowns. I’m just commenting to emphasize not to do the 2nd solution. Technically it works, but you would basically hide logic in a component, which you should avoid.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to implement behavior like StartCoroutine by Entitas?
Give HitComponent a field (float cooldownTime) then do entity.AddHit(cooldownTime). Then have a HitCooldownSystem : IExecuteSystem that iterates ...
Read more >
ScriptableObjects and Coroutines
The basic idea is that a ScriptableObject which wants to run a Coroutine spawns a GameObject in the Scene which will hold the...
Read more >
Manual: Coroutines
A coroutine allows you to spread tasks across several frames. In Unity, a coroutine is a method that can pause execution and return...
Read more >
Coroutine in non-monobehaviour script for a fsm. : r/Unity3D
The issue is I cannot use coroutines in any of my classes as they all inherit from the base state FSM class. Is...
Read more >
Coroutines in Unity (how and when to use them)
In this step by step guide, learn how pause functions and script sequences of events the easy way. With Coroutines in Unity.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found