Jump to content
  • Advertisement
Wush

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

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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);
        }
    }

 

Share this post


Link to post
Share on other sites
Posted (edited)

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

Edited by Nypyren

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!