Entitas-csharp: Component Listener is not working

Created on 24 Apr 2018  路  5Comments  路  Source: sschmid/Entitas-CSharp

Hi, I'm following the example Match One to creating an Unique global component and also created Listener for it.

My code is like this:

My ScoreComponent :

using Entitas;
using Entitas.CodeGeneration.Attributes;

[Game, Unique, Event(false)]
public class ScoreComponent : IComponent {
    public int value;
}

My ScoreSystem:

public class ScoreSystem : ReactiveSystem<GameEntity>, IInitializeSystem
{
    private Contexts _contexts;
    private IGroup<GameEntity> _scoreGroup;

    public ScoreSystem(Contexts contexts) : base(contexts.game)
    {
        this._contexts = contexts;
    }

    public void Initialize()
    {
        _contexts.game.SetScore(0);
    }

    protected override void Execute(List<GameEntity> entities)
    {
        int newScore = entities.Count;
        _contexts.game.ReplaceScore(_contexts.game.score.value + newScore);
    }

    protected override bool Filter(GameEntity entity)
    {
        return true;
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.AllOf(
            GameMatcher.Bucket,
            GameMatcher.BucketRemoved
            ));
    }
}

And my ScoreController:

using Entitas;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreController : MonoBehaviour, IScoreListener
{
    public Text scoreLabel;
    private Contexts _contexts;
    private Systems _systems;

    void Start () {
        Contexts.sharedInstance.game.CreateEntity()
            .AddScoreListener(this)
            ;
    }

    public void OnScore(GameEntity entity, int value)
    {
        scoreLabel.text = "Score " + value;
    }
}

In my tests, the system is working but OnScore in my Controller is not being called at all.

Where is the problem in my code?

Most helpful comment

have you put the GameEventSystems in your feature class ?

public sealed class GameSystems : Feature {

    public GameSystems(Contexts contexts) {

        // Input
        Add(new InputSystem(contexts));

        // Update
        Add(new GameBoardSystems(contexts));
        Add(new ScoreSystem(contexts));

        // Events
        Add(new GameEventSystems(contexts)); <-------------this

        // Cleanup
        Add(new DestroyEntitySystem(contexts));
    }
}

All 5 comments

have you put the GameEventSystems in your feature class ?

public sealed class GameSystems : Feature {

    public GameSystems(Contexts contexts) {

        // Input
        Add(new InputSystem(contexts));

        // Update
        Add(new GameBoardSystems(contexts));
        Add(new ScoreSystem(contexts));

        // Events
        Add(new GameEventSystems(contexts)); <-------------this

        // Cleanup
        Add(new DestroyEntitySystem(contexts));
    }
}

Oh! I didn鈥檛 note that! I see a quick search in Match One repo now, is that system generated by Entitas?

yes

but the Match One repo is using a old version of Entitas that the generated system named call EventSystems. The lastest version will generate [ContextName]EventSystems.

It's working! thanks you so much!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vaudevillian picture vaudevillian  路  3Comments

Noicon picture Noicon  路  4Comments

CCludts picture CCludts  路  3Comments

CuriousHub picture CuriousHub  路  5Comments

yanjingzhaisun picture yanjingzhaisun  路  4Comments