Skip to main content
GameDev.net gamedev.net
🔒 Locked

A Struct That Contains A Delegate With Unknown Signature

Started by sooner123 Jul 18, 2016 at 10:22 AM 3 replies 2.8k views
Original Post
sooner123
sooner123

I'm creating a little command line interface for the player. They will be able to execute commands similar to in DOS.

The commands themselves are structs:


public struct Command {
    public string name;
    public string helpDoc;
}

And the program has a list of commands:


List<Command> commands = new List<Command>();

I initialize all the commands like this:


commands.Add(new Command { name = "exit", helpDoc = "exits the OS" });

I also want to be able to pass in a function pointer when adding an element to the commands list.

Let's say I have a function like this:


public static void helpCommand() {
    Console.Write("here is a list of commands: etc.");
}

And I'd like use a delegate to bind this function to the command when I add the command to the commands list above.

What would the syntax for the struct's delegate member be? And how would I pass the delegate into the commands.Add line?

Not all command's functions will have the same signature.

sooner123
sooner123

I got it working with this struct:


namespace commandLineOS {
    public struct Command {
        public string name;
        public string helpDoc;
        public Delegate commandDelegate;
    }

This instantiation:


commands.Add(new Command { name = "help", helpDoc = "lists this help file", commandDelegate = new Action<List<Command>>(commandHelp) });

And this call:


commands[0].commandDelegate.DynamicInvoke(commands);

I don't really understand what I did here or exactly why it's working but I am guessing I will need to use Func instead of Action if I want my command to return a value or take in more than one parameter.

ChaosEngine
ChaosEngine
Stop and think about what you want to achieve.
So you want to have commands that invoke delegates that potentially take arguments and return some values.
Where are you going to get the argument values from?
What are you going to do with the return values?
IF you can explain what you want to achieve better, than we might be able to help you.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Shaarigan
Shaarigan

I assume this is C#? Then I did something similar some time ago using ina real time script processing environment to our game.

You should simply split your commands into two spares of commands where as I understand you will have 2 sorts, either with or without parameters.

The you could work arround the System.Action constructs.

The program handles any command (parameterized or not) as if it was a public void DoSomething(); function that could be mapped as System.Action object.

This works with any parameterless function without return type (or with return type just use System.Func) but you will ask yourself how to get parameters in.

That is simple as writing another struct


public struct ArgumentCall
{
  public Action<object[]> command;
  public object[] args;

  public ArgumentCall(object[] args)
  {

  }
  public DoJob()
  {
    command(args);
  }
}

And to use this just pass it as Action too by writing


Action com = new ArgumentCall(args).DoJob();

and anything else is handled by the GC when you discard the function handle to DoJob. Principal was tested and considered to work at least inside .NET 3.5 compatible with Unity 4+, not tested with Unity 5

This is way more safe than fiddling arround with pure delegates because you wont stay under the well worthing of the compiler and JIT when doing this

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.