Hello all! I’m well-versed in gameplay logic thanks to scripting/modding experience but NOT well-versed in some C++ and general good coding principles, so I wanted to see if some of you who actually learn things in effective ways could enlighten me a bit…I have two really important questions I need answered before moving forward and I’m having trouble finding anything in my research. I’ll summarize it at the end of my post as a TL;DR example. I’m also well-aware I just need a better foundation for my coding knowledge, but I have limited time and have to learn things on the fly for now. I try to keep my head down and work on stuff instead of trolling through the forums for answers to everything, so please don’t be bothered by the length of this post – it’s repressed things I’ve been trying to wrap my head around for months haha. So here we go:
1. Syntax or Sin-tax?
I’m quite fond of getting references to specific objects in this manner:
AMyCustomClass* ObjectRef = ((AMyCustomClass*)ArrayContainingSomeStuff[CurrentItr]); ObjectRef->DoStuffOnlyThisClassCanDoWhoopee;
I’m of course referring specifically to the right side of that first line – the ‘((AActor*)TheActorI’mPointingTo’ bit. Is this casting? I am under the impression that this is just getting a direct reference to something I already have a hold of, or is this just nonsense I’m telling myself and it’s just another way of casting? **I am performing this operation VERY frequently, too – will that catch up to me in performance or memory usage? **And if it’s not casting, am I headed for disaster if the object I’m trying to assign a reference to ends up not being the class I put on the left side?
2. The Casting Connudrum
Okay, here’s number two – how do I go about casting efficiently? I’ve heard that ANYTHING you cast to at ANY point in your code will be held in memory if the casting actor is present in the world – is this true? If so, when is casting ever viable at all and not just a train-wreck eventually? For instance, I heard a story of someone referencing a boss enemy class with a cast deep in their player animation BP code, and because the player has access to the BP from the get-go, the memory for the boss was being allocated in the menu. I know I should be adhering to event-based scripting, but how do I do event-based scripting without casting in the case of, say, needing to cast to a vehicle class when a player overlaps it and I want them to possess it to provide input, or not even possess it and just call input functions on it? I think if someone were able to give me a concrete example of the best way to handle this situation, it would clear up a lot of my other misunderstandings****.
In this example, what should I do? Have a UInterface for all interactables in my game (it’s VR)? Or could I just do this with a universal ‘Use’ function in a parent ‘interactables’ class that they all derive from, and have that Use function triggered in each one with their correct response through a virtual void, like this?