Download

How to create Variants of UObjects - Need ideas

I need a system that allows me to create variants of uobjects.

The idea would be similar to how a c++ class can inherit from another, and be changed though the use of virtual functions.

However, i want this to work on an instance level, not a class level.

Example: The parent has a uporperty.
The child has the same upropety, and should by default maintain the same property value as its parent.

Im not sure how i should do this - I could simply create a bool for every UProperty to tell the property to inherit or over-ride, but this seems un-elegant.

So, Unreal doesn’t really have any notion of data-inheritance that allows for that sort of thing between instances. However, I’m not really sure you need that anyway. It sounds like you just need to use blueprints.

For example: Let’s say you’ve got a C++ class for a unit. This would be any old unit. Then you could make a blueprint of that class for an enemy Skeleton and configure all the values that make it that sort of enemy. From there you could make a blueprint of Elite Skeleton that changes just the health to be larger. All the other values would be used from the Skeleton blueprint and if you changed Skeleton, Elite Skeleton would automatically get those values.

Ultimately this is the same as c++ inheritance, but shifts it all into content for easier configuration. It also keeps them opaque to code so that there is no lure to trying to cast to the derived types in ways you shouldn’t.

If that’s not sufficient I’d have to hear more about your particular use case. At my work we were able to solve nearly all our data-inheritance needs through a few strategic blueprints.

My use-case is a little different, as the variants need to be defined by the user, at runtime.

At runtime, the user can create assets.
I wanted to add the option to create asset-variants.
The user would choose whether to set, or inherit any given field/variable in the asset variant.

Ah, I see! User generated content. Fun times. Yeah, blueprints won’t help you out much there.

In that case, the bool for every UProperty would work but it’s probably going to be a pain in the butt adding those flags and maintaining them.

One alternative is to not track that information directly and only worry about it during serialization similar to how Unreal works with assets: Assuming that you’ve got an Asset (A) and Asset-Variant (AV), then when you write out AV, you skip all the members that have the same value as the same member in A. Then when you read it in, you initialize all the values of AV with A before applying the values from the file which will only overwrite those properties that were modified.

I want to say Unreal has some utilities to help with this (I assume they must for their own serialization) but haven’t dug around enough to know where it is (sorry). At work we had some code that worked this way in UE3, but when I was converting it to UE4 I couldn’t find the exact match. It’s an optimization for our save games I still want to get back to doing.

One more alternative that I just thought of (if the serialization time one isn’t workable) would be to keep an array/set of property names instead of the bools. This would eliminate the need to make a parallel set of booleans for each property. You would just add and remove property names from the collection instead of setting the bool.

ok thanks, i liked the idea of using an array of property names instead of manually declaring a bool for every variable in the class.

you can access the name of a Uproperty with FProperty->GetName();
You can also retrieve a Uproperty using FindPropertyByName(FName);
This would allow me yo keep track of the override status of my properties.

However, if i were to re-name a variable this would break existing savegame data. Also storing all these propertyName strings will make my save-files larger than id like.

If I could manually define a secondary name/identifier on my Uproperties, this would be perfect. But i havent found anything like that yet.

You need a state machine to swap things with bulion variables.

Like I have been thinking the smallest such state machine involves one bulion variable, two functions and two pairs of if and else statements.

First you make two functions after you declared your bulion variable and say, if bulion equals true run this function, else run this function, this is the selector with the functions.

Then you need another if an else statement for the variable it’s self that is conditional and turns the bulion on or off true or false.

If I’m here for example turn the bulion on, esle leave it false, and once the bulion turns on it will run the function with true, otherwise the function with false.

So the if and else with the bulion to turn it on or leave it off and then if and else of the functions where these if and else conditions see if it’s on or off then pick the if if true the else if bool is off, you got for if true and else false.

You set it to false initialy that is the defaul value let’s say and turn it on with the conditioning system that is dependant on what is happening, when that switches the bulion on then the other if and else statement will see the bulion is on and run the true if and not the else, when it’s off it will run the else.

And you can run this way variants , versions and so on, you can run if if if if, you can have 100 if’s in one if, I’m just giving an example of simple two state being.

It could be really simple for beging, like with key press, press key turns the Bool var on and the function with else turns off and the other with if turns on, and this is basicly a state machine, a function switch where functions turn off and some turn on and you can have variations of things, like slight offsets, slight difrences on things.

State machine my friend, they have tutorials on complex ones in C++ that I’m trying to grasp :grinning: for unreal engine, this is I guess the most basic concept.

If you manage to create variants of things please post here, I would be intrestid too.

That’s very true, but there are things that you could do to minimize the downsides of renaming. Something like the CoreRedirectors the engine has. You could also ignore it, the expectation being that after you ship you wouldn’t really be changing those names. That makes it a dev only problem that you could decide to just live with.

As for the string size problem, you could store the FNames instead. Those are functionally two integers.

1 Like

They really, really, really don’t need a state machine to do any of what they want to do.

If you look at his post it’s his initial idea, I just elaborated on it a little bit that is all.

The idea would be similar to how a c++ class can inherit from another, and be changed though the use of virtual functions.
============================
I could simply create a bool for every UProperty to tell the property to inherit or over-ride, but this seems un-elegant.

So he wanted to switch virtual functions and use bool, why not build a state machine to do that.
Maybe there is a better idea I don’t know. I posted here because I also want this with variants running of objects, now it’s pink, now it’s blue, now it’s red but it’s the same object, you get the idea and it changes based on where you are maybe, what you are doing around the world in the game.

To do this you need to switch functions, turn off a function , turn on a function, otherwise they all run at the same time

This is very good to know, i did not realize FNames were so small.
With this information, I might also redo my file save system to work based on variable names. This would make my object saving/loading 100% automatic.

Because, classically, a state machine operates over a single state. The state of the machine. In this case there is state, whether or not the value if overridden, for every property. That’s N states, not one. So you’d actually have too create N state machines, one for each property. Which I suppose you could do, but then there’s the fact that the state itself is a boolean so there are only two states. Combined with the fact that there’s nothing to do for the boolean except choose which value, makes it ultimately a trivial state machine that is just encumbered by whatever the machine framework is more than it’s helped. His original idea is purely a procedural problem:

float GetValue( )
  if (bIsOverridden)
     return MyValue;
  else
     return Parent->MyValue;

Now that sounds like a state machine. But that is not what the OP was asking about. They’d have a pink one and a blue one but they are separate objects.

It could be different objects Mag, but slightly difrent yet almost identical, like clones and the functions switch between them, between these objects, one is red one is blue but they look the same, isn’t it what he wanted to switch between clones.

If not, my bad.

What I understand by variants, like objects that are the same yet slightly difrent.