🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Events, Messages, Alternatives? How to handle the communication layer?

Started by
6 comments, last by DMDropOut 4 years, 10 months ago

As a hobby programmer I am slowly working my way up project by project to make a game. However I am currently stumped on what to do with what I call the communication layer.

To explain: you have an ability, a technology or a building first you need to communicate to anyone effected to apply the effects. Also abilities might need to know when a certain activity is started or ended to trigger or alter their effects.

Events are a tool to handle this, but:

1) You still need an effective filter, so only those recipient are called that fulfill certain requirements(nationalities, race, class, locality etc)

2) You need to know/write beforehand what  method  needs to trigger an event.

I would be thankful for any advice on these two points, is there any way to elegantly append a methodcall with another methodcall? 

I mean I could write some kind of wrapper containing a delegate  of a method  and then only call the wrapper, having the wrapper contain a list of additional method calls and their inputs to be executed?

When you have nothing to say,I advise you talk nonsense :D
Advertisement

From what I'm understanding, you want to create a system where there are events and these events are only called when certain conditions are met, is that right?

As for the delegates thing, you can reference multiple methods using only one delegate as long as the methods have similar signatures.


    class Program
    {
        private delegate void testDeleg(int testInt);
        static void Main(string[] args)
        {
            testDeleg tDeleg = testMethod1; // create an instance of testDeleg delegate. Make sure to use the operator '=' when assigning a method to a delegate for the first time.
             tDeleg += testMethod2; // use += if you wish to add a reference to another method on the same delegate.

            tDeleg.Invoke(1);
            Console.ReadKey();
        }

        static void testMethod1(int i)
        {
            Console.WriteLine("testMethod1 and " + i);
        }

        static void testMethod2(int i)
        {
            Console.WriteLine("testMethod2 and " + i);
        }
    }

 

Beware that having a very large number of subscribers on an event/delegate can cause performance issues when adding or removing more handlers.  MulticastDelegate's internal "invocation list" is an array which is treated immutably like the internal char array in strings; this means that the entire invocation list is recreated every time one handler is added or removed.

If you have a significantly large number of handlers, it's better to use a List<DelegateType> instead (similar to how StringBuilder can improve performance when building large strings out of many small parts)

See: https://referencesource.microsoft.com/#mscorlib/system/multicastdelegate.cs,fcbf8bdc05d28aeb

One way to communicate is not to explicitly communicate at all.

Instead have a black-board like store where everybody can find the information it needs to make its decision. Everybody also updates its own part of the blackboard. Advantages are that it is simple, it is always consistent information (nobody has to store state about others locally). The disadvantage of that solution is that the blackboard is a central point of information. You can somewhat reduce that by splitting the blackboard into several sub-blackboards, but how well that works heavily depends on the application.

Thanks for the advice so far and forget the part with the wrapper, I scrapped that.

For now I have  simply added event call to the end of methods and wrote my own eventargs object to allow necessary information to be stored, I am still torn what arranges the handlers(I tried some form of matchmaker in a previous project), and might to have to write my own list class so whenever something is added to a list it throws an event.

I still feel this all to be inelegant, creating eventcalls when there is no listener, so I still hope to improve this.

When you have nothing to say,I advise you talk nonsense :D

Have interested entities subscribe to the events they are interested in. You may want to dive into the Publish-Subscribe pattern: https://en.wikipedia.org/wiki/Publish–subscribe_pattern

23 hours ago, Wush said:

... I am still torn what arranges the handlers(I tried some form of matchmaker in a previous project), and might to have to write my own list class so whenever something is added to a list it throws an event. ...

System.Collections.ObjectModel.ObservableCollection<T> has this functionality if you want to use a built-in class.

This topic is closed to new replies.

Advertisement