用不同的字符串名称调用功能

发布于 2025-02-13 18:14:16 字数 225 浏览 1 评论 0原文

因此,我正在尝试制作一个2D游戏(C#),其中玩家具有不同的技能。问题是我不知道如何使用包含技能名称的字符串调用函数。总有另一种方法可以做到这一点,这是通过使一个非常长的列表充满if statements来检查技能是否被命名为某件事,但这似乎远非理想。

假设一项技能称为“ Skill1”。是否有任何方法可以使用字符串“ Skill1”调用称为Skill1()的函数?这确实有助于使代码看起来不错。

提前致谢

So I'm trying to make a 2D game (C#) where the player has different skills that they can use. The problem is that I don't know how to call a function with the string containing the skill name. There is always another way to do it, which is by making a really long list full of if-statements to check if the skill is named something, but this seems far from ideal.

Let's say a skill is called "Skill1". Is there any way to call a function called Skill1() by using the string "Skill1"? It would really help with making the code look good.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛起拖把扫天下 2025-02-20 18:14:16

您正在寻找的是代表(更具体地说是代表词典)。

void Main()
{
    var things = new Dictionary<string, Action>
    {
        {"Thing1", DoThing1},
        {"Thing2", DoThing2},
        {"Thing3", DoThing3},
    };
    
    things["Thing1"]();
    things["Thing3"]();
    things["Thing2"]();
}


public void DoThing1()
{
    Console.WriteLine("In Do Thing 1");
}

public void DoThing2()
{
    Console.WriteLine("In Do Thing 2");
}

public void DoThing3()
{
    Console.WriteLine("In Do Thing 3");
}

有关更多信息,请搜索代表,行动和功能。

What you're looking for are delegates (and more specifically a dictionary of delegates).

void Main()
{
    var things = new Dictionary<string, Action>
    {
        {"Thing1", DoThing1},
        {"Thing2", DoThing2},
        {"Thing3", DoThing3},
    };
    
    things["Thing1"]();
    things["Thing3"]();
    things["Thing2"]();
}


public void DoThing1()
{
    Console.WriteLine("In Do Thing 1");
}

public void DoThing2()
{
    Console.WriteLine("In Do Thing 2");
}

public void DoThing3()
{
    Console.WriteLine("In Do Thing 3");
}

For more information, search for Delegates, Actions and Funcs.

画尸师 2025-02-20 18:14:16

委托是一个不错的选择,但是如果您想简单地保持它,则可以使用Switch语句代替IF语句,并且可以易于维护。

这是基本的,但也许会有所帮助

static void Main(string[] args)
        {
            Console.WriteLine("call skill");
            string _skill = Console.ReadLine();
            CallSkills(_skill);
        }

        public static void CallSkills(string skillname)
        {
            switch (skillname)
            {
                case "skill1":
                    //call skill1 method
                    break;
                case "skill2":
                    //call skill2 method
                    break;
                case "skill3":
                    //call skill3 method
                    break;
                case "skill4":
                    //call skill4 method
                    break;
                case "skill5":
                    //call skill5 method
                    break;
                default:
                    break;
            }
        }

Delegates are a good option but if you want to keep it simple you can use switch statement instead of if statements and it will be easy to maintain.

It's basic but maybe it will help

static void Main(string[] args)
        {
            Console.WriteLine("call skill");
            string _skill = Console.ReadLine();
            CallSkills(_skill);
        }

        public static void CallSkills(string skillname)
        {
            switch (skillname)
            {
                case "skill1":
                    //call skill1 method
                    break;
                case "skill2":
                    //call skill2 method
                    break;
                case "skill3":
                    //call skill3 method
                    break;
                case "skill4":
                    //call skill4 method
                    break;
                case "skill5":
                    //call skill5 method
                    break;
                default:
                    break;
            }
        }
梦晓ヶ微光ヅ倾城 2025-02-20 18:14:16

使用代表使用字典的一种替代方法是使用a 开关表达式与委托。

一种可能的方法是创建一种将所有相关技能名称与适当的技能*方法相关联的方法,并使用丢弃模式_ =&gt; ...;在这里:_ =&gt; null)定义不匹配的任何Skill> Skill> Skill> Skill> Skill>表达式中的任何定义的技能名称:

private static Action GetSkillAction(string skillName)
{
    return skillName switch
    {
        "Skill1" => Skill1,
        "Skill2" => Skill2,
        _ => null
    };
}

为了使使用该方法更易于使用,您可以创建一种实际执行技能(按技能名称)的方法。如果操作与null不同,此方法仅通过调用关联的操作来处理不存在的技能:

public static void PerformSkillAction(string skillName)
{
    var action = GetSkillAction(skillName);
    
    if (action != null)
    {
        action();
    }
}

现在,调用

PerformSkillAction("Skill1");

将导致调用scrize1(),而呼叫打电话

PerformSkillAction("Banana");

不会打电话。

示例小提琴在这里

An alternative to using a dictionary with delegates is to using a switch expression with delegates.

One possible approach is to create a method that associates all relevant skill names with the appropriate Skill* method, as well as using the discard pattern (_ => ...; here: _ => null) to define a default value for any skillName that does not match any defined skill names in the expression:

private static Action GetSkillAction(string skillName)
{
    return skillName switch
    {
        "Skill1" => Skill1,
        "Skill2" => Skill2,
        _ => null
    };
}

To make it easier to use that method, you could create a method to actually perform a skill (by skill name). This method handles receiving a non-existing skill by only calling the associated action if the action is unlike null:

public static void PerformSkillAction(string skillName)
{
    var action = GetSkillAction(skillName);
    
    if (action != null)
    {
        action();
    }
}

Now, calling

PerformSkillAction("Skill1");

will result in a call to Skill1(), whereas calling

PerformSkillAction("Banana");

will not call anything.

Example fiddle here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文