0

So, I'm making myself a small C# library for dialogues to use in an cRPG game.

The idea is, that the Dialogue object and it's fields (like DialogueNode and DialogueOption objects) are created based on an XML file, which I aim to make as simple as possible. The fields, except for lists or objects of types contained within the library, are - at best - string identifiers, to be acquired and parsed by outside means when needed.

I've basic funcionality implemented - XML serialization, running through the dialogue and exiting it - as well as basic, console based application interpreter and a WPF editor to create the dialogues, because writing the dialogue in plain XML is not the most comfortable thing in the world. (the last two are meant to be as much independent from the library as possible, except maybe for implementing what's inside to show/create)

All that being said, I've encountered a problem (actually two, the other one I'll cover in different question when I've the time after my exams).

After giving it some of my unexperienced 'noobish' thought, I've come to think, that I'd like to have some basic predicates stored either in my nodes or options - they would be later checked in game to determine, whether to display the node/option or leave it be (or whatever meddling with those to be honest). For example - an option is displayed if the player character have item X in his inventory, or the node is displayed when player has a certain minimum value of an attribute.

My idea of implementing is so far like that:

  1. Having a field PredicateScript in an object
  2. Having a bool method, that would be executed in runtime by the interpreter like this:
    public bool DisplayPredicate(string predicateCode)
    {
       bool result = FunctionExecutingCSharpCode(predicateCode);

       return result;
    }

I've read some topics about compiling on the fly very brielfy, but I'm not sure if it's exactly what I want - I'm not sure how it would affect the performance of application (either the interpreter or the game itself), if it would be recompiled every few seconds...

I'm not pasting any code of what I'm trying to do, because either I'm yet to do this (as I'm not writing the code I'm not sure it will work) or it's the library structure which I don't think would be of relevance aside from what I explained I aim to do. ^^

Thanks. ^^

  • [This](http://stackoverflow.com/a/14671145/4558029) might help you – lokusking Jun 21 '16 at 12:01
  • Have a look at [Roslyn](https://github.com/dotnet/roslyn). Having said that, I would caution going down this route and I would urge re-evaluating your design. I'm sure there is an easier way to achieve what you're trying to do. – The Bearded Llama Jun 21 '16 at 12:07
  • @TheBeardedLlama My idea was, that the designer of the dialogue puts the predicate bit of code into a WPF's editor textbox, which include it in a field of certain object. Any other idea, how I might implement such conditions in my dialogues? – Schwalbe Jun 21 '16 at 12:17
  • @Schwalbe that's a good question, however, and I don't mean to be disparaging, stackoverflow is meant to be a Q&A site, not a forum; the issue you have is perhaps why a lot of games use a scripting engine like Lua inside of their main engine, so they can run code the way you need to (NB I'm not a games developer, so this is all hearsay) – The Bearded Llama Jun 21 '16 at 13:25

1 Answers1

0

Given the standard .NET framework, there's no such thing as a C# "interpreter" you can feed code that gets executed. You could try to dynamically create C# code and have that compiled on the fly into an in-memory assembly, which you can then use using the .NET compiler services.

And example of this is given here: http://www.codeproject.com/Tips/715891/Compiling-Csharp-Code-at-Runtime.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • What about Roslyn? – The Bearded Llama Jun 21 '16 at 11:57
  • Roslyn is a compiler platform. As I said, you can achieve the same thing using the existing .NET framework standard means. Both, however, are not the same thing as an "internal C# interpreter". – Thorsten Dittmar Jun 21 '16 at 11:59
  • Indeed it is ("compiler as a service" seems to be the favourite term) and you could leverage it to compile on the fly, thus giving the appearance of an interpreter – The Bearded Llama Jun 21 '16 at 12:01
  • the OP is perhaps using the wrong term in this case, but he has stated that he is a novice – The Bearded Llama Jun 21 '16 at 12:03
  • also Roslyn has a REPL, which seems like an interpreter to me... just saying – The Bearded Llama Jun 21 '16 at 12:04
  • @TheBeardedLlama The same thing can be achieved using the already present compiler services. So yes, he could use Roslyn, or compiler services, while the latter is already part of the framework. – Thorsten Dittmar Jun 21 '16 at 12:07
  • That's what I was affraid. But you are saying, that only the newly created code will be compiled, right? Hmmm. For some reason I thought it might've been much more, thanks. ^^ And when I was saying interpreter, I meant MY interpreter of the whole dialogue - a part that displays the dialogue and allows user to make the conversation stored in that object. – Schwalbe Jun 21 '16 at 12:10
  • @ThorstenDittmar I would beg to differ. It's not quite the same thing. Why would there have been a need to have created Roslyn if it were the same thing? Roslyn's new on the fly features and compiler services are most definitely not the same thing. You can't drive a REPL using compiler services. In any case, it doesn't really matter. – The Bearded Llama Jun 21 '16 at 12:12
  • Also about me being a novice - I'm astudent of the second year of engineer degree Computer Studies, so I know SOMETHING. It's just that I haven't yet an opportunity to use my knowledge in bigger project (at least not the .NET/C# bits of my knowledge as I've two simple games created in Java), and for sure we haven't dynamic compilation and stuff like that covered during lecture. – Schwalbe Jun 21 '16 at 12:13
  • @TheBeardedLlama I'm not saying they are the same thing. I'm just saying that for what *he* wants to do, he can use either. – Thorsten Dittmar Jun 21 '16 at 12:14
  • @ThorstenDittmar - after I'm done with my exams, I'll look closely into both solutions. The reason I ask this NOW is that it's just bloody itching me in the back of my head because of that. Thanks. – Schwalbe Jun 21 '16 at 12:20
  • @Schwalbe it wasn't meant as an insult mate ;) no reason to get upset; I was going by your own comment about noobish thoughts ;) we're all novices one way or another :) – The Bearded Llama Jun 21 '16 at 13:19
  • @ThorstenDittmar ok cool, I read your comment as "they're the same" :) – The Bearded Llama Jun 21 '16 at 13:20
  • @TheBeardedLlama Nah, didn't take that as an insult. I was just trying to explain what I meant - english is my second language and not the language I study in (that one being polish), hence I felt I should PROBABLY clarify. Also... now I've come to little different idea, but I'll come back to that tomorrow after my exam. (the base thought: can one make an Expression based on string?) – Schwalbe Jun 21 '16 at 13:57
  • @Schwalbe see my comment about lua up top; it's quite common for games to use some kind of scripting engine; the possible reason for that could be to achieve what you're thinking of doing – The Bearded Llama Jun 21 '16 at 14:01