I suppose this is a general coding question, but I’m new to all this so I figured asking here is as good as anywhere.
I get that a variable that is “public” means that the variable is accessible to other objects outside of its owner, but I’m not seeing the practical use of this.
For instance, I have casting going on all over the place. I would assume that to cast a variable between two blueprints, I would need it to be public, but it seems like it works whether its public or private (Then again, my scripting isn’t that reliable, and maybe that is the reason why). When should I make variable public?
I’m personally using public variables all the time since:
- Casting is particularly expensive (if you can avoid it in any way, don’t ever cast on tick for instance)’
- Exposing variables (making them public) also means they will be listed in the “defaults” tab, so they can be easily edited within the editor.
- It’s also (as i understand and also experienced) good practice to “expose on spawn” various variables - especially references - in order to avoid the need of other less efficient/clumsy alternatives like getting all actors of class or - as mentioned earlier - casting.
These are the main reasons that come to mind quickly, there are other benefits but it really depends on the situation/code.
What I would also advise against is just messing with other BPs variables: setting them (changing their values) from other blueprints. If you need to do that, just make a function/event etc., and have the owning blueprint change it’s own variable(s), which means it doesn’t need to be public.
Anyway, idk why blueprints are “messy as they are” but probably others better equipped at this than me know what they speak of. I’m just telling you what I’ve learned so far about public variables and how I personally use them.
When should I make variable public?
You shouldn’t, BPs are messy as they are. If you can get away without using public variables, that’s great! On the other hand…
it seems like it works whether its
public or private
… pretty much, yeah:
Thank you, this is good to know.
Ha, ok. I guess if it works, it works.
Although that post is about functions not variables…
If the answer satisfied the question pls mark it as an answer and the post solved. Thanks!