I admit my c++ is very rusty. In python it would just be a simple matter to see if the NodePath had a get_velocity function and if so use that. In the c++ world this kind of thing would I think be most simply achieved with casting and some scary code, but I would rather avoid that. I’m tempted to just track get_pos over two frames and derive the velocity from that, but I don’t like the indirectness nor the 1 frame lag it would create. So do those more familiar with the engine code have any suggestions?
You can’t avoid casting and some scary code if you want to program in C++.
You’ll have to know what type of node you are expecting (you can’t ask simply whether it has a method or not), but once you know the type you can easily cast it to that type and call the appropriate method.
Don’t be afraid.
Thanks David. I used to be more confident with that kind of thing, but I’ve touched c++ very little in years. I actually made a 2d engine in c++ which initially had a ton of casting going on. It eventually evolved to use lots of crazy templates which made it a lot more flexible, and I think I ended up without a single cast in the entire engine.
Anyways, more to the point, I searched around the API and found the functions is_of_type and is_exact_type. That should make things fairly easy.