Blueprint editable static variable

battle-tank

#1

I have a peculiar question.
I am trying to implement Tank architecture where the Barrel has reference to the Projectile instead of Aiming Component having reference to Projectile.

The question is, is it possible to edit static variables in Blueprint.

Why would I need a static variable?
Well, the Aiming component needs a projectile speed to determine the trajectory of the projectile. The easiest way to do that is to declare a static variable that is accessible without instantiating the Projectile class and is equal for all tanks.

Why can’t you put the Projectile Speed value in the Aiming component like Ben does?
Well, I do not want to do that, what if I decide to use a different projectile and I want different properties for that projectile, e.g. Armour piercing, High Explosive, etc… It would be really inconvenient to have two variables to be mindful of, it is always better to have one place for variable definition.

Why do you want then, to change the variable value in Blueprint, if it is projectile type specific?
Well, what if the game designer wants to play with the value when he is creating a game. I don’t want the game designer to go to C++ code, or what if he doesn’t know to program. It is far easier to have the variable exposed in the Blueprint, so he could tweak it.

This is not necessary for this game, why do you bother?
Well, I am learning a lot about Unreal Engine through this course. I need this for my professional career. I work for the company that requires me to know Unreal Engine well, I might come across this issue during the development.

Thank you for your help in advance.
Keep on rocking guys.


#2

Instead of putting that data in the Barrel class as a static member put it in the Projectile class as an instance variable. Then you can subclass the Projectile with that and whatever other properties you want that are specific to the projectile, so each type of Projectile could have its own speed, or damage value, etc. When you spawn a projectile in the Fire() method, you can look up the projectile’s launch speed from the newly spawned object.

This preserves encapsulation by keeping the responsibility for the class’ behavior in the class (the Projectile) not on some unrelated or only slightly related other class (the Barrel). You could have different ProjectileBP’s to set different speeds and then have the Tank or the Aim Component reference the class type for spawning.

Using a static variable may seem convenient but from what I understand it is bad practice in that it is a kind of global value and those should only be used where absolutely necessary and appropriate. By making it global, if you were to edit it then ALL projectiles would have the new speed. You could not have a speed specific to each type of the projectile, or set by the individual tank or whatever.