加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

是否可以使用Lua / Javascript脚本扩展新的变量来扩展C#对象?

发布时间:2020-12-14 21:46:36 所属栏目:大数据 来源:网络整理
导读:可以说我有C#类: class Player { string Name; int HitPoints} 我想为我的游戏添加modding / scripting支持,用户可以使用自己的变量扩展它. (让我们说“bool StartedKill5RatsQuest”),然后对他来说同样可以访问他的默认参数. 用户脚本: player.HP = 10;pl
可以说我有C#类:

class Player {
  string Name;
  int HitPoints
}

我想为我的游戏添加modding / scripting支持,用户可以使用自己的变量扩展它. (让我们说“bool StartedKill5RatsQuest”),然后对他来说同样可以访问他的默认参数.

用户脚本:

player.HP = 10;
player.StartedKill5RatsQuest = true;

是否可以使用任何众所周知的脚本语言来完成它?

解决方法

你不能直接这样做.但是,通过引入一组内部“变量”,可以获得类似的功能:

Dictionary<string,object> _scriptVariables = new Dictionary<string,object>();

有了这个,你可以为你的玩家提供一套创建/获取/设置他们的“变量”的方法,比如:

public void CreateVariable<T> ( string name,T defaultValue );
public void Set<T> (string name,T value );
public T Get<T> ( string name );
etc...

这些方法将访问您的字典并操纵其值,因此您的用户可能会写:

public void Initialize()
{
    player.CreateVariable<int>("HP");
    player.CreateVariable<bool>("StartedKill5RatsQuest");

    player.Set("HP",10);
    player.Set("StartedKill5RatsQuest",true);
}

public void Update()
{
     ...
     if(player.Get<bool>("StartedKill5RatsQuest"))
     {
         ...
     }
}

这比直接成员操作更冗长一点,在类中实现支持方法时,你应该对类型很聪明,但是它可以完成这项工作.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读