代表名单及调用

发布于 2024-10-05 00:46:56 字数 1522 浏览 1 评论 0原文

我正在 ASP.NET 中创建一个较小的 RPG 游戏。在这个游戏中,我有一个项目架构,其中每个项目都有一些方法。例如,所有项目都应该共享“Drop”、“Examine”和“Use”等方法。有些项目必须使用“操作”、“计算”等方法进行扩展。

到目前为止,我已经创建了以下对象 GameActionList:

public delegate void MyDelegate();

public class GameActionList
{
    public List<MyDelegate> Items = new List<MyDelegate>();

    public void Add(MyDelegate del)
    {
        Items.Add(del);
    }

    public void CallDelegates()
    {
        foreach (MyDelegate myDelegate in Items)
        {
            myDelegate();
        }
    }
}

我有一个 BaseItem 类,其中包含此 GameActionList。 BaseItem 类中 this 的 get 属性如下:

    public GameActionList Actions 
    { 
        get
        {
            GameActionList actions = new GameActionList();
            actions.Add(this.Drop);
            actions.Add(this.Examine);
            return actions;
        }
    }

这很好,但是...我有一些问题!

我的问题

我需要一种更通用的 GameActionList。我不仅需要一个包含 void 的列表,还需要一个包含函数的列表。此外,我需要带参数和不带参数的方法。

例如: Drop 方法需要一个 Player 对象,这样他就可以 Drop 项目。 Examine 方法需要返回描述该项目的字符串。

另外,我需要一些在初始化 GameActionList 时不知道的数据:当我调用该方法时,我首先知道这些数据...

所以我有两个问题:

  1. 如何扩展 GameActionList,以便它可以包含一个列表空隙和函数,而且它们都可以有参数或没有参数..(它甚至有意义吗?)
  2. 我如何在循环的稍后阶段向方法提供一些数据,例如调用时?

另外......这可能是一种非常愚蠢的方法,所以如果你有一些更优雅的解决方案......我非常愿意听到它

多谢...! 拉尔斯

I am creating a smaller RPG game in ASP.NET. In this game I have an items architecture, where each item has some methods. For instance, all items should share a method like "Drop", "Examine" and "Use". Some items will have to be extended with methods like "Operate", "Calculate" and such.

So far, I have created the following object GameActionList:

public delegate void MyDelegate();

public class GameActionList
{
    public List<MyDelegate> Items = new List<MyDelegate>();

    public void Add(MyDelegate del)
    {
        Items.Add(del);
    }

    public void CallDelegates()
    {
        foreach (MyDelegate myDelegate in Items)
        {
            myDelegate();
        }
    }
}

I have a BaseItem class, which has this GameActionList. The get property of this in the BaseItem class is like this:

    public GameActionList Actions 
    { 
        get
        {
            GameActionList actions = new GameActionList();
            actions.Add(this.Drop);
            actions.Add(this.Examine);
            return actions;
        }
    }

This is fine, BUT... I have some problems!

My problem

I need a way more generic GameActionList. I need to have a list of not only voids, but also functions.. Also, I need both methods with parameters and without parameters.

For instance: The Drop method will need a Player object, so he can Drop the item. The Examine method will need to return a string descriping the item.

Also, I need some data which I don't know when I Initialize the GameActionList: I first know these data when I invoke the method...

So I have two questions:

  1. How do you extend the GameActionList, so it can contain a list of both voids and functions andalso these both can have parameters or not.. (AND DOES IT EVEN MAKE SENSE??)
  2. How can I give some data to the method later in the cycle, like when invoking?

Also... This might be a very stupid way to do it, so if you have some way more elegant solution.. I'm more than ready to hear it!

Thanks a lot...!
Lars

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

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

发布评论

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

评论(1

我乃一代侩神 2024-10-12 00:46:56

您很可能需要 Action 、 Func 委托

Func

操作

you most probably need Action , Func delegates

Func

Action

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