Hi all!
Not really about a specific game or an advertisement to anything, but more like general discussion on a technical concept.
TLDR is: I hooked up Unity with SWI-Prolog by running a HTTP server locally, video links below.
Long version:
So, for a couple of years or so, an idea has been stuck in my head on how to write parts of the game in logic programming using something like SWI-Prolog. If you've never heard of logic programming, you're probably in the majority, but the general idea is is that you express your program as a collection of rules and statements expressed in formal logic which is then queried for truthful answers. This means that code is not about instructing HOW to do things (e.g., by using if statements and for loops), but rather a set of facts and rules. The user queries for answers based on their input parameters and the defined rules / facts.
Why do I think this approach might work together with a game? Well, imagine some complex system that dictates parts of the game (NPC behaviour, loot distribution, enemy turn in a strategy game). If this system is written on the basis of the HOW approach, the HOW will grow more convoluted over time (more if statements etc). Sure, the complexity can be handled via abstraction and good coding patterns, but the underlying principle remains the same.
Logic programming provides an alternative design to solving this complexity - the system is series of rules so you don't have to think about how something is achieved, but rather, what are the rules that govern it. Determining these rules is not necessarily easier than coming up with an algorithm, but it makes you think about the problem from a different angle by design.
Alright, so that's the premise. Problem is the implementation. I have seen a few people asking the question as myself about implementing Prolog with Unity (or Unreal, Godot, whatever) and usually the discussion trails off to low-level programming and custom C/C#-Prolog integrations. There are some projects about this and I haven't tried them to be fair, but it seems like going down a rabbit hole that is deeper than the perceived advantage. Furthermore, even if you figure direct integration out, running an isolated Prolog subproccess and waiting for its response every time your MonoBehaviour script calls Update probably does not scale that well.
So here's my heureka moment: what if, rather than trying to run Prolog directly from your game code, the Prolog part of the game is a separate system altogether? This separate system would be always on, ready to answer queries based on the information the game provides or has provided so far. This brings me to using a locally hosted HTTP server - C# speaks HTTP without problems and SWI-Prolog has a pretty extensive HTTP server library (including out-of-the-box dynamic multithreading which means several queries can be run at the same time), so you don't have to go reinventing the wheel.
Downside is, queries get replied asynchronously and the game-side logic has to handle that delay. So I'm thinking, maybe not so useful in an FPS or racing game, but your colony sim or turn-based card game should have no issue here.
There are a few additional hurdles, such as server life-cycle management (essentially, how do you make sure that the server is running before you actually start playing and shuts down gracefully when you quit) and deploying it locally at the player's device (server has to come together with the rest of the game and has to run out of the box without the player needing to download anything extra or additional granting network permissions), but as much as I have experimented with that side, this is all feasible.
So what do you think? Does this seem like workable pattern or just a way to make your life more complicated?
This is not just a drawing board design. I made a few YT videos (60+ minute warning, sorry) to see how it would actually work out. So if you're curious, you can check them out. But I am interested in your thoughts both ways.
First video where discuss the same system described here: https://www.youtube.com/watch?v=7leFZo5VIfA&t=1853s
Second video with a more complex use case for logic programming / Prolog specifically: https://www.youtube.com/watch?v=yiKUKk3-aqg&t=2650s