I decided to try to write an IF engine from scratch. It's difficult

I thought people here might be interested to hear about my attempts to write an IF engine from scratch.

I’m writing it in C (because it’s the only language I know well enough to attempt something like this) and things went well at first. It took me maybe 15 minutes to figure out how to make an extremely basic parser. At maybe the half hour mark, I had a basic setup with rooms you could walk around in.

Things got more difficult after that. The first bug I discovered was if you don’t enter a command and just press enter, it repeats the last thing you entered. (so, for instance, if you move north and start pressing enter repeatedly, it’ll keep trying to move north.) I was able to fix this, but it’s not an optimal fix. (This is such early development, I don’t care about optimal fixes right now.)

Next, I started trying to implement items. Items can’t do anything at the moment, I’m just trying to make them exist and then worry about making them actually be useful. They show in rooms and you can pick them up, though I did have some issues with the “get” command. This one took me a while to make work right. Right now, you have to type the whole item name and that needs to be changed. (“get sword” should work for an item called “rusty sword” and it currently doesn’t, you have to type “get rusty sword”)

My current roadblock is dropping items; that code currently does not work correctly. You can drop an item, but everything in the room disappears, including the item you dropped, when you do that and I haven’t been able to figure out why yet. The other issue I’m having is I have a maximum number of items allowed in a room and, if you try to drop something in a room that doesn’t have any items in it, it says there’s no room to drop anything. (which shouldn’t be happening, obviously.)

I’m also not sure this is worth fixing because I already realize I need to change how items work, which is necessitating a rewrite of everything I’ve done so far. One of the features I want to include is procedural item generation for generic (non-plot) items and the way I’m currently doing things is not conducive to that. I’m attempting to make an open world type of game (think Elder Scrolls but IF) and would like to have various locations and items be procedurally generated.

I’m most likely not going to be able to get anywhere close to my goal, but it’s fun to try. I’m sure I could probably do something like what I’m thinking of with Inform or some other system (and it’d save me the mental trauma of trying to code the fundamental parts of IF most people don’t think about, like what the data structure of an item looks like) but sometimes I’m stubborn, so I’ve decided I have to do this on my own.

Anyway, after maybe 8-10 hours of work, that’s where I’m currently at. You can move between rooms with no issues, pick up items (with no known issues), view your inventory, and drop items if you don’t mind everything in existence disappearing.

If there’s interest, I can post additional updates on my progress.

12 Likes

Now try writing an IF engine in Scratch.

6 Likes

That is true!

Doing your own system is fun and a great coding exercise. Just remember that the well-established systems, like Inform, took years of development to reach their current polished state.

6 Likes

Well, building such an engine from scratch is indeed quite an undertaking. I would recommend this book, though:

https://craftinginterpreters.com/

It’s free for on-line reading. It discusses crafting an interpreter for a programming language but there are things generally useful like how to tokenize an input and with some thought you could craft your own syntax.

The book starts with Java (I used Golang for that part) and then also moves to how to do it in C.

Definitely much more approachable than flex/bison combination we did in my CS classes.

4 Likes

It’s not required that you build a compiler or new syntax so I think using C is okay.

But I do think you need to attack this from some kind of object-oriented programming perspective.

Since C is a procedural language, you’ll miss some very helpful things like inheritance and interfaces.

The hoops you’ll need to jump through to build anything complex are not trivial.

No matter what, building an IF platform is one of the greatest challenges one can attempt. And it’s a lot of fun.

5 Likes

I’m gonna be the guy that says a new platform could be built in a fraction of the time using GenAI.

You still need deep IF knowledge, but the barrier to entry has been significantly lowered.

@dormammu

You may want to look at how other engines work and how they setup their locations and objects. For example, a lot can be gleaned from looking at the scripting language and example code available in Adventuron. It’s a great way to understand the fundamentals of parser IF under the hood. Food for thought.

https://adventuron.io/

1 Like

Apologies for tooting my own horn, but you might find this series of posts I wrote a few years ago about doing something similar in Python interesting.

3 Likes

Part of me feels like pressing enter on a blank line to repeat the last action would be a feature, not a bug.

But yeah, writing a game engine from scratch, almost regardless of genre, is not something for the faint of heart.

2 Likes

I was partly through that (Snap!, not Scratch, which is more powerful and made by Berkeley instead of MIT) but I hit a bug I can’t figure out how to fix. I had rooms, navigation, and examining objects. Maybe sometime I’ll come back.

The incredible Griffpatch did create IF in Scratch, though.

2 Likes

Part of me feels like pressing enter on a blank line to repeat the last action would be a feature, not a bug.

It’d be easy enough for me to toggle the repeating because of how I fixed it. (just an If block that checks if there was any input or not and doesn’t process anything if there wasn’t.) I actually could make that a toggleable command that stops checking.

Now I klnd of want to do that.

2 Likes

This is true, and even changing to C++ would probably be helpful due to its OOP nature. It’s something I thought about and I do know a bit of C++, but not all that much. It would halt work while I get up to speed, but you’re right. It may be beneficial to do so. Since C is a subset of C++, I may be able to use some of what I’ve already written if I switch.

I’ll have to give that some thought. It’s not like I’m working on a deadline or anything.

1 Like

Griffpatch has also made Scratch…in Scratch! Maybe the next thing is Inform 7 in Inform 7, or TADS in TADS?

1 Like

I put this in all my games.

-Wade

3 Likes

Think of what you’ve done as a prototype. Look at what worked and what didn’t. Try to come up with a more general design way to handle the bits that were previously messy or over complex. see how that fits with the bits that previously worked and combine ideas.

best of luck.

3 Likes

I have done the same, also in C as it’s the only language I know :slight_smile: . It’s here (click).

Am at the point where I must add functionality to play online, but I sorely lack the knowledge.

2 Likes

Also many memory management bugs of C can be fixed with unique_ptr and shared_ptr. Since a world model can get quite complicated, I would recommend against manual memory management.

1 Like

It seems like you want a text-based RPG engine, which sits on the edge of what different people still consider IF. That has a lot more requirements, like procedural generation, probably combat or even crafting systems, etc. It sure is a lot more work than just an IF engine.

I had a funny problem with my first item system in Java: I created item prototypes, but instead of cloning the prototype to create a new item, I just referenced it, which kind of broke everything in the most confusing way.

1 Like

Is it wrong that I’m enjoying this more than some of the comp games I’ve played?

Or that just that I’m not keeping that thought to myself?

1 Like

Choosing the ‘best’ language is influenced by the scope of your ambitions.
It’s fair to say that an IF parser does a lot of text processing. Some languages are better suited to that than others.

Also, there are certain open source technologies that have become best-in-class solutions for specific applications. If you choose to use them, you might prefer a language that has good supporting libraries for them. Some that come to mind are:

  • SQLite database for saving games or projects.
  • TOML (or similar) as a format for capturing structured data.
  • Markdown for defining passages of text.

Coincidentally, I released an update to my own project Balladeer this morning. It’s quite a capable framework now. I’m continuing to evolve it as I work on a game I can show as an exemplar for it.

I wish there was more discussion of this topic. It seems a lot of people start to write their own framework and give up after a while. There are scant resources to start them off on the right track, and no obvious way for them to share their efforts so that others can build on them.

3 Likes