How do I create a family tree?

Hi. First time posting. I’ve been trying to create a Dynasty system like in Crusader Kings 2 but I cannot figure out how to create a new variable and have it appear in an array without me adding it manually, as well as visually adding it to the widget blueprint images, shown here,

Crusader Kings 2 Image for reference.
I was wondering if anyone else has the answer to the previous question?

Hi, thanks for the response, unfortunately that method is manual and limited by the amount of elements I add myself, I want to create a system that adds them automatically so when a new child is born it creates a new component that can be viewed from a menu.

Ah got it.This is not easy and it’s not unreal specific. First I though of a binary tree but this won’t solve your problem.

You will need to handle a complete graph model what may require a deep dive into data structure theory and search algorithms…

Is this the answer you are looking for?

For sure you can do this automatically, but you have to use a decent data structure to hold the info.

I don’t think an array is going to cut it. It would probably have to be some sort of dictionary / structure.

When somebody is body, as part of the ‘birth’ event, you can add them to the tree.

Yes, had a little look at this. I think you’re in for a world of pain if you try structs. Because the structs have to be recursive ( because each tree member has descendants ), which you can’t do.

You can impersonate it with arrays, but then you spend most of your time managing the indexing to recover your data structure.

One easy way is to make a data only blueprint. It just holds variables, one of which is the list ( array ) of the descendants, which are also tree members:

288730-familytreebp.jpg

288741-variables.jpg

288742-descendants.jpg

The only slight downside is you have to spawn them. But the don’t really exist in the world…

Okay, so I think I know what you’re talking about, I need to create the new children as spawned entities and add them to a list, but how would I get them to appear in a list as structured as the Crusader Kings 2 image I used as a reference? Would I have to use a scroll box within a widget blueprint, and cast to the parent of the children.

Yes, you have to spawn then as if you’re creating something that goes in the world, except they never get seen :slight_smile:

After spawn you connect them appropriately to the rest of the tree.

As for the display, that’s a whole other game. Widgets, probably yes, HUD etc. But I’d concentrate on getting the data structure working first… ( you’ll probably have to log a new question about the display ).

Tell me if you need any help with the structure…

Hey, thanks for the offer. I’m going to give it a try and hope it works, I’m not very knowledgeable about structures so I appreciate the offer I’ll post an update when I’ve finished trying.

Ok, cool. But remember, it’s not structures I’m talking about ( I know I used the word up there, sorry ), it’s blueprints, basically. The tree is made out of blueprint actors, because it’s the only way you can have a recursive tree.

Hi, I was wondering if I could pick your brain about how to create it using blueprints as I’ve tried to wrap my head around it but I can’t seem to figure out how to start it as a blueprint.

So here’s a setup. First make an actor blueprint, called FamilyTreeMember which contains these variables:

289069-screenshot-2019-10-03-at-124349.png

Ascendant and Descendants are of type FamilyTreeMember reference.

Then make a function in the level BP ( or where you want ) called MakeTreeMember:

It spawns the tree member structure in the world ( that’s ok because it has no ‘substance’ ) and set the actor TAG to be the same as the name of the person. We can use this later to quickly find a person. I just made this up as an example, you can do it any way you want…

Then, at some point in the main code of the level BP, you can make some tree members:

and connect them to each other:

So, you can see the structure is growing. We can directly find anyone with one BP node GetAllActorsWithTag, but you might want to use some sort of ID if you’re really going for this.

Also I don’t know how heavy this will be if you have thousands of people in the tree. You might want to do an experiement on that.

As far as I know, this is the only way you can get recursive data structures in BP. If large numbers do turn out to drag down performance then maybe you do have to go for just plain arrays. But then you have to cope with representing the recursion with a linear array. Don’t fancy coding that…

Does it make sense?

Yes, thank you man. I’ll give this a shot when I’m free and try to expand on it, you’ve been a huge help! :smiley:

No worries. When you want to put the tree in the savegame, you can just make a big array of them all with GetAllActorsOfClass. And when you want to get them back out again, you’ll need to get the array from the savegame and spawn them all…