Scripting a tech tree

Hey guys,

I’m working on an RTS game, and I’m trying to figure out a good way to implement a tech tree, but I’m not sure what is a good way to determine when various techs become available.

As a simple example, here is part of a tech tree from Civ 5.

alt text

So here is the scripting solution I was able to come up with to implement this…

public bool Pottery_available;
    public bool AnimalHusbandry_available;
    public bool Archery_available;
    public bool Mining_available;
    public bool Sailing_available;
    public bool Calendar_available;
    public bool Writing_available;
    public bool Trapping_available;
    public bool Wheel_available;
    public bool Masonry_available;
    public bool BronzeWorking_available;
    public bool Optics_available;
    public bool Philosphy_available;
    public bool HorsebackRiding_available;
    public bool Mathematics_available;
    public bool Construction_available;
    public bool IronWorking_available;


	void Update () 
	{
		if(Pottery_available)
        {
            Sailing_available = true;
            Calendar_available = true;
            Writing_available = true;
        }

        if(AnimalHusbandry_available)
        {
            Trapping_available = true;
            Wheel_available = true;
        }

        if(Archery_available && Wheel_available)
        {
            Mathematics_available = true;
        }

        if(Mining_available)
        {
            Masonry_available = true;
            BronzeWorking_available = true;
        }

        if(Sailing_available)
        {
            Optics_available = true;
        }

        if(Writing_available)
        {
            Philosphy_available = true;
        }

        if(Wheel_available)
        {
            HorsebackRiding_available = true;
        }

        if(Wheel_available && Archery_available)
        {
            Mathematics_available = true;
        }

        if(Masonry_available)
        {
            Construction_available = true;
        }

        if(BronzeWorking_available)
        {
            IronWorking_available = true;
        }


	}

…but I’m CERTAIN there has to be a better, more elegant way to do it. Anyone know what it is?

There are many ways to do this. Start with the general idea of a skill tree and find a way to lay out the data. Each skill in the tree has something in common. For example, most, if not all, skill trees have at least:

  1. Points required to learn a skill

  2. Zero or more skills that each skill can lead to

Each skill has zero or more skills that it can lead to, and those child skills can lead into more skills. You take a path from the root skill and keep going deeper into a specialization. True to its name, a skill tree would be nicely represented by the ADT known as a tree!

A skill might look something like:

public class Skill
{
    int PointsRequired;
    List<Skill> ChildSkills;
}

Once you have the data for all the skills, find a way to put them together into this tree, then work on defining behavior for each one. Keep it simple and use the data from the tree and its nodes to handle your in-game implementation. I hope this helps; good luck!

you might find this tool useful.