简体   繁体   English

如何将if语句存储为字符串? C#

[英]How do I store if statements as strings? C#

I am create a game engine in C# using OpenTK (OpenGL). 我使用OpenTK(OpenGL)在C#中创建一个游戏引擎。 My game's update system works off of an event queue, where when something happens, it is added to the event queue. 我的游戏的更新系统不在事件队列中工作,事件发生时将其添加到事件队列中。 Each event has a set of triggers and a set of actions. 每个事件都有一组触发器和一组动作。

Let's say the player walks. 假设玩家走路。 The 'PlayerWalked' trigger runs. “ PlayerWalked”触发器运行。 Every event in the game with the 'PlayerWalked' trigger in their list gets added to the queue. 游戏列表中具有“ PlayerWalked”触发器的每个事件都将添加到队列中。

Now, the update function on the game is called several times a second. 现在,游戏的更新功能每秒被调用几次。 Every time it is called, the oldest event in the queue is run. 每次调用它时,都会运行队列中最旧的事件。 Every action in the event's action list is run. 运行事件动作列表中的每个动作。

Now, I want to add a condition system. 现在,我想添加一个条件系统。 On the trigger 'PlayerWalked', rather than just adding it to the list, it checks the trigger, checks the conditions, and THEN adds it too the queue. 在触发器“ PlayerWalked”上,不仅将其添加到列表中,还检查触发器,检查条件,然后将其也添加到队列中。

Example: 例:

Event Name: event1 事件名称:event1
Triggers: 'ChestOpened', 'PlayerDigs' 触发器:“ ChestOpened”,“ PlayerDigs”
Conditions: Player Position is at 1, 1 (2D game). 条件:玩家位置为1、1(2D游戏)。
Actions: Give player 10 gold. 行动:给玩家10金币。

Now, I have the trigger checking and actions working. 现在,我可以进行触发器检查和操作了。 Now all I need is to add the conditions. 现在,我需要添加条件。 Here is my dilemma: 这是我的难题:

When I am storing the map, I need a way to store the conditions in the event's conditions file. 在存储地图时,我需要一种将条件存储在事件的条件文件中的方法。 I was thinking the file would contain this: 'playerPosition|(1,1)' But that is very broad, the variable 'playerPosition' might not exist, and I would have to parse it weird. 我以为文件将包含以下内容:'playerPosition |(1,1)'但这很广泛,变量'playerPosition'可能不存在,因此我不得不将其解析得很奇怪。

Question: How would I store conparisions (conditions, if statements) in a map file (string)? 问题: 如何将比较条件(条件,if语句)存储在映射文件(字符串)中?

More Information: The map is a directory stored in a map storage folder next to game's executable. 更多信息:地图是存储在游戏可执行文件旁边的地图存储文件夹中的目录。 In the map folder, there is a folder for events. 在地图文件夹中,有一个事件文件夹。 In the events folder, there is a folder for each event. 在事件文件夹中,每个事件都有一个文件夹。 In each event folder there is a file for triggers, a file for conditions, and a file for actions. 在每个事件文件夹中,都有一个用于触发器的文件,一个用于条件的文件和一个用于操作的文件。 The triggers and actions are working now, but the conditions is what I am trying to add. 触发器和操作现在正在工作,但条件是我要添加的条件。

File Structure: 档案结构:

Maps (Directory)
    Map1 (Directory)
        Events (Directory)
            Event1 (Directory)
                Triggers (File)
                Conditions (File)
                Actions (File)
            Event2 (Directory)
                Triggers (File)
                Conditions (File)
                Actions (File)
            Event3 (Directory)
                Triggers (File)
                Conditions (File)
                Actions (File)

I am using OpenTK as a wrapper to OpenGL, but there is no sound engine yet. 我正在使用OpenTK作为OpenGL的包装,但是还没有声音引擎。 It is a 2D RPG game. 这是一个2D RPG游戏。

Edit: Someone has brought to my attention that it looks like i'm asking how to write to a file in C#. 编辑:有人引起了我的注意,好像我在问如何在C#中写入文件。 I know that. 我知道。
What i'm asking is: How would 我要问的是:

if (playerPosition == new Point(0, 0)) { }

translate into a text file that I would parse and store in a variable? 转换成我将解析并存储在变量中的文本文件? How would I store an If statement in a variable, and how would I store that variable in a file? 如何将If语句存储在变量中,以及如何将该变量存储在文件中?

Solve: 解决:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;

namespace Entropy
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            a b = new a { s = ":D" };

            string json = new JavaScriptSerializer().Serialize(b);

            Dictionary<string, object> c = (Dictionary<string, object>)new       JavaScriptSerializer().DeserializeObject(json);
            MessageBox.Show(c["s"].ToString());
            //Out: ':D'
         }
    }

    public class a
    {
        public string s = string.Empty;
    }
}

Storing comparisons in a string, and then turning them into code.... that is called writing a compiler! 将比较存储在字符串中,然后将它们转换为代码...。这称为编写编译器! Computer science has your back on this! 计算机科学对此表示支持!

So you're asking, what's the simplest compiler I can get away with, for my domain specific language? 所以您问,对于我的领域特定语言,最简单的编译器是什么?

You're mini-language is very mini: it only has predicates (possibly nested predicates) 您的迷你语言非常小:它只有谓词(可能是嵌套谓词)

You can look at C# compiler parser/generators. 您可以查看C#编译器解析器/生成器。 You've got some fun reading ahead of you. 您前面有一些阅读乐趣。

Alternatively, you could create a class structure that holds these nested predicates, and then serialize them into json, and store the json. 或者,您可以创建一个包含这些嵌套谓词的类结构,然后将它们序列化为json,并存储json。

so you might have 所以你可能有

var condition = new Condition { 
    Object = Objects.Player, 
    Comparison = Comparisons.Equals, 
    Value = new Value { type = Position, innerValue = "2,1" }
};

And then you serialize it into a string you can save... 然后将其序列化为可以保存的字符串...

var json = new JavaScriptSerializer().Serialize(obj);

And then you can load it up again later, by deserializing the json back into an object. 然后,您可以稍后通过将json反序列化回一个对象来再次加载它。

There's some 'Evaluate' method you call on the comparison object and it rips through the object hierarchy, calculating an answer for you. 在比较对象上调用了一些“评估”方法,该方法遍历对象层次结构,为您计算答案。

I've done this sort of thing in the past by instead referencing a scripting language, Lua is popular for this type of activity in games, but Python is good for interoperability as well. 过去,我是通过引用脚本语言来完成这种事情的,Lua在游戏中的这种活动中很流行,但是Python也具有良好的互操作性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM