Hou*_*oup 4 javascript c# scripting lua
可以说我有C#类:
class Player {
string Name;
int HitPoints
}
Run Code Online (Sandbox Code Playgroud)
我想为我的游戏添加modding/scripting支持,用户可以使用自己的变量扩展它.(让我们说"bool StartedKill5RatsQuest")然后对他来说同样可以访问他的默认参数.
用户脚本:
player.HP = 10;
player.StartedKill5RatsQuest = true;
Run Code Online (Sandbox Code Playgroud)
是否可以使用任何众所周知的脚本语言来完成它?
你不能直接这样做.但是,通过引入一组内部"变量",可以获得类似的功能:
Dictionary<string, object> _scriptVariables = new Dictionary<string, object>();
Run Code Online (Sandbox Code Playgroud)
有了这个,你可以为你的玩家提供一套创建/获取/设置他们的"变量"的方法,比如:
public void CreateVariable<T> ( string name, T defaultValue );
public void Set<T> (string name, T value );
public T Get<T> ( string name );
etc...
Run Code Online (Sandbox Code Playgroud)
这些方法将访问您的字典并操纵其值,因此您的用户可能会写:
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"))
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
这比直接成员操作更冗长一点,在类中实现支持方法时,你应该对类型很聪明,但是它可以完成这项工作.