appccelerate / statemachine

A .net library that lets you build state machines (hierarchical, async with fluent definition syntax and reporting capabilities).
Apache License 2.0
481 stars 128 forks source link

Accessing Event Argument not working? #39

Closed cpsaez closed 4 years ago

cpsaez commented 5 years ago

Hi there.

Congrats for your great work here. I have an issue but maybe Im doing something wrong.

Im trying to access the Fire event content inside the execute method, but I'm always receiving null. I expected to see "hola" on Console. Is that the desired behavior?

using System;
using Appccelerate.StateMachine;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            var fsm = new PassiveStateMachine<string, string>();
            fsm.In(States.Lobby)
                .On("Hola").Goto(States.Play)
                .Execute<string>(MyMethod) // or .Execute<string>(x=>MyMethod(x)) 
                .Execute((object x) => MyMethod2(x)) // or .Execute<object>(x => MyMethod2(x));
                .Execute<object>(MyMethod2)
                ;
            fsm.Initialize(States.Lobby);
            fsm.Start();

            fsm.Fire("Hola");
        }

        private static void MyMethod2(object o)
        {
            Console.WriteLine(o);
        }

        private static void MyMethod(string s)
        {
            Console.WriteLine(s);
        }
    }

    public static class States
    {
        public const string Lobby = "Lobby";
        public const string Play = "Play";
    }
}
ursenzler commented 5 years ago

Hi

You do not pass an argument into the Fire method. "Hola" is used as the event to be triggered.

Try fsm.Fire("Hola", "myArgument") and you should get "myArgument" passed to MyMethod.

Let me know if that works for you.

As a side note: I prefer using enums for states and events as long as they do not have to be dynamically specified. Makes the code a bit cleaner.