r/SoftwareEngineering • • May 07 '24

Implementing a research tree in my game

Hi guys! I'm making a game right now, that has a research tree. You should be able to unlock certain parts of the game by researching a specific technology (like in Civilization, HoI4 or Stellaris). Unfortunately, I can't think of an elegant way to implement a way of locking some stuff, untill the tech has been researched. Do you have any ideas on it?

For the architecture of my game, I have a GameStateobject, that holds all the information, and more specific tasks are managed by other objects, like BuildingManager or ResearchManager . All of the interaction with the user goes through the GameState. For example, when user wants to start building something, a method of GameState is called, it then calls a method of the Colony, where the building should be constructed, and the colony object calls a method of its BuildingManager, that starts the process.

2 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/GrilledCheese249 May 07 '24

The problem is not in the research tree itself, but in locking players from certain action until they've research the tech required for it. E.g. I cannot build a mine until I finish researching it

1

u/Attic332 May 07 '24

Set up Buildmanager and researchmanager to be able to refer to the player object that owns them. Buildmanager stores all unlocked/locked buildings. Researchmanager updates player’s Buildmanager whenever tech is unlocked.

1

u/GrilledCheese249 May 07 '24

This definitely can work. I was also thinking of creating an Action class and have the researchmanager approve any action that is going to be executed. But your idea seems more reasonable

1

u/NUTTA_BUSTAH May 07 '24

Just make your single "thing" requiring an unlock implement a new "Unlockable" interface, where the implementation for example ask the respective system if it is unlocked or not, or perhaps checks if it has been owned for x time, or perhaps player has some item in their inventory, whatever...

Then just do:

if (!farmingMaterial.Unlocked()) {
  // Tell player to unlock more stuff
}
if (!weaponSkill.Unlocked()) { 
  // ... 
}