Jump to content
  • Advertisement
Sign in to follow this  
sooner123

A Struct That Contains A Delegate With Unknown Signature

This topic is 727 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Edited by sooner123

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


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

Share this post


Link to post
Share on other sites

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

Edited by Shaarigan

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!