0

im currently expanding my knowledge a little, and wanted to Create a little game for myself.

The Structure is as Following: Programm.cs creates an instance of Gamecontroller. This Gamecontroller is the lowest level i want to Access. It will create instaces of the Views, and from classes like config.

I want to implement an debug Console with Command Input. These Commands should always start at the Gamecontroller level, and should be able to interact with kinda everything i could do with C# code.

So i want to access the Objects, Member and methods withing Gamecontroller, or Within any nested object.

Currently i cant get to the Properties of an Child, because _member returns an "Type" which gets parsed to RuntimeProperty instead of the Class

Example on Parsing:

"objPlayer.name" > "GameController.objPlayer.name"
"objConfig.someSetting = 10" > "GameController.objConfig.someSetting=10"
"objConfig.functionCall()" > "GameController.objConfig.functionCall()"
"objConfig.objPlayer.setName("someName")" > "GameController.objConfig.objPlayer.setName("someName")"
"objPlayer.name" > "GameController.objPlayer.name"

this is what i got so far:

    private void parseComamnd(string Command)
    {
        var actions = Command.Split('.');
        var start = this.GetType();
        var last = actions[actions.Length - 1];
        foreach (var action in actions)
        {
            if (action.Contains("(") && action.Contains(")"))
            {
                _exec(start, action);
            }
            else
            {
                start = _member(start, action);
            }
        }
    }
    private Type _member(Type pHandle, string pStrMember)
    {
        return pHandle.GetProperty(pStrMember).GetType();
    }
    private void _exec(Type pHandle, string pStrFunction)
    {
        var Regex = new Regex(@"\(|,|\)");
        var FunctionParams = Regex.Split(pStrFunction);
        var FunctionName = FunctionParams[0];
        FunctionParams[0] = "";
        FunctionParams = FunctionParams.Where(val => val != "").ToArray();
        pHandle.GetMethod(FunctionName).Invoke(FunctionName, FunctionParams);
    }
3
  • So, what the question about? Do you want to implement own DSL for game? If you want to be able to run c#-like commands via debug console, take a look on Roslyn. If U want to parse own commands, U can use antlr or custom written parser. Commented Sep 12, 2016 at 11:55
  • I didn't understand what's your question/doubts. If you want to do your own DSL I suggest you have a look at => goldparser.org You can create you own language, check syntax and generate code based on the input. Commented Sep 12, 2016 at 12:10
  • For my Console i thought about smth like PHP eval, i dunno what a dsl is yet tbh, found a text about dsl, and im working through it right now :) - i would like it to behave like that Live Console in debug mode Commented Sep 12, 2016 at 12:23

1 Answer 1

1

If I understood right, you want to match some string commands with actions you want to perform. In this case you could use Dictionary as a storage for string-delgate couples to match your string commands to actions you want to perform. As an advantage of this approach, you can change matched couples during program runtime as you wish

class SomeClass
{
    delegate void OperationDelegate(string value);
    IDictionary<string, OperationDelegate> Operations = new Dictionary<string, OperationDelegate>();

    public SomeClass()
    {
        Operations.Add("objPlayer.name", SetName);
        Operations.Add("objConfig.someSetting", SetSetting); 
    }

    public void HandleNewValue(string command, string value)
    {
        try
        {
            if (Operations.ContainsKey(command))
                Operations[command](value);
        }
        catch (Exception e)
        {
            Logger.Error(e);
        }
    }

    private void SetName(string value)
    {
        // Some logic there
    }

    private void SetSetting(string value)
    {
        // Some logic there
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this could actualy be enought for me. I was bound to the eval thinking of PHP, so i might give this a try.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.