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.

Why don't add "Update/Replace" GroupEvent?

See original GitHub issue

Hi,

It’s me again, I have a question want to ask. In game reactive system, we have to override the GetTrigger method to collect game entity while some components added or removed, but I cannot see the “Update/Replace” event.

I checked the source code, in Group.cs, there is some code holding the updating event:

        /// This is used by the context to manage the group.
        public void UpdateEntity(TEntity entity, int index, IComponent previousComponent, IComponent newComponent) {
            if (_entities.Contains(entity)) {
                if (OnEntityRemoved != null) {
                    OnEntityRemoved(this, entity, index, previousComponent);
                }
                if (OnEntityAdded != null) {
                    OnEntityAdded(this, entity, index, newComponent);
                }
                if (OnEntityUpdated != null) {
                    OnEntityUpdated(
                        this, entity, index, previousComponent, newComponent
                    );
                }
            }
        }

But, in Collector.cs where the OnEntityAdded and OnEntityRemoved delegate were assigned, there’s not any code to set Update/Replace:

        /// Activates the Collector and will start collecting
        /// changed entities. Collectors are activated by default.
        public void Activate() {
            for (int i = 0; i < _groups.Length; i++) {
                var group = _groups[i];
                var groupEvent = _groupEvents[i];
                switch (groupEvent) {
                    case GroupEvent.Added:
                        group.OnEntityAdded -= _addEntityCache;
                        group.OnEntityAdded += _addEntityCache;
                        break;
                    case GroupEvent.Removed:
                        group.OnEntityRemoved -= _addEntityCache;
                        group.OnEntityRemoved += _addEntityCache;
                        break;
                    case GroupEvent.AddedOrRemoved:
                        group.OnEntityAdded -= _addEntityCache;
                        group.OnEntityAdded += _addEntityCache;
                        group.OnEntityRemoved -= _addEntityCache;
                        group.OnEntityRemoved += _addEntityCache;
                        break;
                }
            }
        }

And there is not “Update/Replace” in GroupEvent.cs at all.

So, Is there some design reason or another reason to stop adding “Update/Replace” event in Collector?

Cheers

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
yuchtingcommented, Jan 25, 2018

@FNGgames I understand your meaning now, that’s to say, I can write CheckDoSomethingStatusSystem like this:

class CheckDoSomethingStatusSystem : IReactiveSystem{
    
    Filter(){
        entity.hasDoSomethingStatus && entity.doSomethingStatus == Done;
    }
    
    GetTrigger(){
        return CreateCollector(GameMatcher.ActionStatus);
    }
    
    Execute(entities){
        entities.foreach(e){
            //if(e.DoSomethingStatus.status == Done){
                e.DoSomethingIdx.idx += 1;
                if(e.DoSomethingIdx.idx >= e.DoSomthingList.list.Count){
                    // all behaviors are executed done
                    e.Destory();
                }else{
                    // add some thing by data
                    Utils.AddComponentForDoSomething(e,e.DoSomthingList.list[e.DoSomethingIdx.idx]);
                }               
            //}
        }
    }
}

and then, if some systems call

e.ReplaceDoSomethingStatus(Done)
or
e.AddDoSomethingStatus(Done)

I will collect these entity and process by this system. Thanks for your explanation, so patient.

I just want to ask another un-resolved problem again, why in Entitas source code Group.cs, there is OnEntityUpdated to handle replace event only, but haven’t realized it and remain a never used variables, it makes me confused.

And more, a method calling ReplaceDoSomethingStatus may raise two different events: Added if hasn’t any Component before or Removed and Added if has one.

Maybe, if you design it one relates one: AddDoSomethingStatus just raises Added event RemoveDoSomethingStatus just raises Removed event ReplaceDoSomethingStatus just raises Replaced event if it has already one, if you haven’t one, it raises Added event.

Is it a better design? Please don’t mind, I just make a suggestion.

@IDNoise thanks for your help, do you mean, you open a issue before, but @sschmid haven’t modified? And he suggests us do:

protected override Collector<GameEntity> GetTrigger(IContext<GameEntity> context) {
    return context.CreateCollector(
        GameMatcher.DoSomethingStatus.Removed(),
        GameMatcher.DoSomethingStatus.Added()
    );
}

rather than:

protected override Collector<GameEntity> GetTrigger(IContext<GameEntity> context) {
    return context.CreateCollector(GameMatcher.DoSomethingStatus.Updated());
}
0reactions
RealCosmikcommented, Jul 11, 2018

Perhaps in the filter make sure you state that entity does not have the component so then when the system is triggered it’ll only execute when the entity does not have the component which would be similar to reacting on remove but with replace. Or just actually use the component generated remove method and then just use the add method. I’ve even ran into sometimes where although using replace is just faster to do using removed and then add was just better for my system filtering

Read more comments on GitHub >

github_iconTop Results From Across the Web

CRUD: When to use Create, Replace, Update or Delete in ...
For major changes like migrations to new servers, I would use Replace, since update is much weaker (albeit not as weak as Create)....
Read more >
Group Policy Drive Map Win10 issue
Action: Replace, Reconnect checked, Remove item when no longer applied, ... Update will create a drive map if none exist and will update...
Read more >
Update calendar invite without sending 'meeting has been ...
Someone at my company asked if it was possible to update a calendar invite without spamming all the attendees that the meeting invitation ......
Read more >
Insert into a MySQL table or update if exists
REPLACE works exactly like INSERT , except that if an old row in the table has the same value as a new row...
Read more >
[GA4] Modify and create events in Analytics
Modifying an event overwrites an existing event by adding, changing, or removing parameters. Modified events are processed before created events are processed. ...
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