Is it bad practice to use structs' for constantly changing variables?

I’ve been considering using structs to group together a variety of variables into orderly collections (both for keeping things even more tidy than my categories and for a one stop shop to call variables from).

A small example would be a character struct containing the characters’ name, health, maximum health and maximum armour values. Also whether they’re crouched, walking, running etc.

This would mean through the course of the game constantly calling variables and setting the members within the struct when certain values changed.

However I’m wondering if this would cause excessive resource usage or if the overhead would be so minimal as to make little to no difference in the big scheme of things.

Looking online I’ve only found references to structs in coding where the websites I found say that changing struct values in runtime is a bad idea and that struct values should be immutable however, I also see that according to the unreal documentation the immutable flag is being withdrawn so does this mean it’s considered okay in Unreal?

I can think of an alternative which would be to have a struct of some default settings, use this in begin play to set variables in the character for such things as their health and likewise at the end of the game, set the characters’ variables back into the struct.

This way I’d still have the structs to call on as I’m looking for a way to put the variables in one place to call in a later save/restore system but I wanted to ask to see if my original idea of constantly getting/setting variables would be okay or if that way madness lay.

Thanks.

1 Like

There is nothing to worry about. You should be fine to keep on doing what you’re doing! Modern computers these days are plenty powerful to handle some structs!

That said, I’ll also tell you that using the unreal engine category system has zero performance hit, as far as I can tell, and neither does pulling the entire pawn and checking variables from there with a “cast to” node (no more than anything else), So if you did get into the habit of using categories and the normal casting, it would be better in the long run, possibly because you would not have to make so many structs. It would save you some work.

But to answer your question again, you have nothing to worry about!

This is the exact purpose for which a struct was devised :). If you need to operate on the struct with methods specific to it’s data, then the class was invented :slight_smile:

A struct is the same size as the data it holds, you’re not wasting any resources. There isn’t any overhead. You can change those variables during runtime, they don’t need to be immutable.

I’ve even started building self-contained systems in structs, such as my third person follow camera just has a few things tying into it such as it’s tick being called in the Character’s Tick, similar thing for my fighting game input/combo system and so forth. It’s cleaner, and it costs nothing. It saves time by making your systems easier to manage.

The worst thing you can do for your game is premature optimization, that’s how you spend a lot of time optimizing systems that don’t make it into the final build. Make the game then if there are performance issues and/or you can’t meet your target specs, then find out where the performance hits are and optimize those areas. The next time around, you’ll know what to avoid through experience and it builds up as you go :).

Thanks for all the replies, all of them were very helpful to me and since I can only choose one answer I figured the first response should get the accepted answer tick.

As to what IcemanX said about casting and categorising, this is how I’ve got all my code but I’m now looking at it and realising that a struct for a lot of it would be more beneficial for what I need to do.

Vaei, while I agree you don’t want to get bogged down with premature optimizations in this instance things are starting to look a tad unwieldy and likely to cause problems down the line for me which is why I’ve been looking at using structs to simplify things.