C
C#7mo ago
2spooky2play

Branching paths for a text based adventure game (language agnostic)

How would i create a system of branching paths for a text based adventure game? i dont want it to devolve into a ton of if-statements because i have done that before and it is a mess to keep track of and maintain
7 Replies
Angius
Angius7mo ago
Splitting stuff into methods can flatten the structure a lot and facilitate code reuse For example:
void Shop()
{
Console.WriteLine("Welcome to the shop");
}
void Temple()
{
Console.WriteLine("Welcome to the temple");
}

void City()
{
Console.WriteLine("You enter the city. Where do you go?");
Action place = Console.ReadLine().ToLower() switch {
"temple" => Temple,
"shop" => Shop
};
place();
}
void Shop()
{
Console.WriteLine("Welcome to the shop");
}
void Temple()
{
Console.WriteLine("Welcome to the temple");
}

void City()
{
Console.WriteLine("You enter the city. Where do you go?");
Action place = Console.ReadLine().ToLower() switch {
"temple" => Temple,
"shop" => Shop
};
place();
}
Instead of having everything in the City method
2spooky2play
2spooky2playOP7mo ago
oooh functional programming i like that idea ima experiment with that and come back to you
static class Input {
public static string? GetInput(string prompt) {
Console.Write($"{prompt}: ");

return Console.ReadLine()?
.Trim()?
.ToLower();
}
}

static class TestWorld {
public static void Shop() {
Console.WriteLine("Welcome to the shop");
}

public static void Arena() {
Console.WriteLine("Welcome to the arena");
}
}

class Program {
static void Main() {
var userInput = Input.GetInput("shop or arena?")!;
var place = userInput switch {
"shop" => TestWorld.Shop,
"arena" => TestWorld.Arena,
_ => throw new Exception(),
};
place();
}
}
static class Input {
public static string? GetInput(string prompt) {
Console.Write($"{prompt}: ");

return Console.ReadLine()?
.Trim()?
.ToLower();
}
}

static class TestWorld {
public static void Shop() {
Console.WriteLine("Welcome to the shop");
}

public static void Arena() {
Console.WriteLine("Welcome to the arena");
}
}

class Program {
static void Main() {
var userInput = Input.GetInput("shop or arena?")!;
var place = userInput switch {
"shop" => TestWorld.Shop,
"arena" => TestWorld.Arena,
_ => throw new Exception(),
};
place();
}
}
No best type was found for the switch expression.
No best type was found for the switch expression.
i have no idea what this error means looking up its code doesnt get me anywhere
Angius
Angius7mo ago
Means it could not automagically figure out what type to infer That's why my example specified the type to be Action instead of using var
2spooky2play
2spooky2playOP7mo ago
ah ok
Omnissiah
Omnissiah7mo ago
i believe a common way would be having engine+branching data, instead of the path being directly implemented in the code so the data would have a pointer to where to go next, a bit like the adventure card games where there's writter "if you answered A pick card 48"
2spooky2play
2spooky2playOP7mo ago
so with that idea it would be more like
Location location = GetNextLocationFromInput();
location.ResolveEvents()
Location location = GetNextLocationFromInput();
location.ResolveEvents()
?
Omnissiah
Omnissiah7mo ago
if the adventure is short i would maybe go for a simpler approach if there are scenes, chapters, plots, then i would move it to data, like
// example: { "text", Answer1Next = 312, Answer2Next = 11, ... }
// ...
answer = ReadAnswer();
nextId = GetNextFromAnswer(current, answer);
current = scenes[nextId];
print(current.Text);
// example: { "text", Answer1Next = 312, Answer2Next = 11, ... }
// ...
answer = ReadAnswer();
nextId = GetNextFromAnswer(current, answer);
current = scenes[nextId];
print(current.Text);
Want results from more Discord servers?
Add your server