How not to use casts [an ironical comment]

Get help with programming issues.

Moderator: PPS-Leaders

Post Reply
User avatar
x3n
Baron Vladimir Harkonnen
Posts: 810
Joined: Mon Oct 30, 2006 5:40 pm
Contact:

How not to use casts [an ironical comment]

Post by x3n » Sat Jan 02, 2016 9:59 pm

Recently I spent some time in Orxonox, when I noticed something strange:
healthbar_bug_1.PNG
healthbar_bug_1.PNG (70.71 KiB) Viewed 19451 times
What's that weird blurry thing on the left? Apparently it's supposed to be a health bar, just like the one on the right.

Only that, for some reason, one healthbar is blurred and the other is not.

Ok well, but what's up with that other thing over here:
healthbar_bug_2.PNG
healthbar_bug_2.PNG (77.54 KiB) Viewed 19451 times
Is that... a giant healthbar?

That must be one moth**fu**ing healthy portal!

But wait... aren't portals supposed to be things? Like... objects? Without health?

So let's check the code:
healthbar_bug_3.PNG
healthbar_bug_3.PNG (8.19 KiB) Viewed 19451 times
So uhm... apparently every WorldEntity is now a Pawn and can be safely converted with a C-style-cast?

Well, nope.

So I fixed it with an orxonox_cast (which is basically the same like a dynamic_cast):
healthbar_bug_4.PNG
healthbar_bug_4.PNG (5.88 KiB) Viewed 19451 times
And now portals look correctly (without a healthbar):
healthbar_bug_5.PNG
healthbar_bug_5.PNG (36.75 KiB) Viewed 19451 times
What do we learn from that? Never use C-style-casts!
(I already knew that!)


By the way, did you know that portals will also teleport your laser beams (and other projectiles)? It's pretty cool:
portals.PNG
portals.PNG (129.14 KiB) Viewed 19451 times
Fabian 'x3n' Landau, Orxonox developer

fvultier
Human Space Navy Sergeant
Posts: 12
Joined: Thu Feb 19, 2015 12:39 pm

Re: How not to use casts [an ironical comment]

Post by fvultier » Sun Jan 03, 2016 4:39 pm

Nice bugfix! I think a guy played around with these enemy health bars a year ago during PPS. Obviously we should have kept one more eye on him..... :twisted:

User avatar
beni
Baron Vladimir Harkonnen
Posts: 949
Joined: Tue Oct 03, 2006 9:15 am
Location: Zurich
Contact:

Re: How not to use casts [an ironical comment]

Post by beni » Mon Jan 25, 2016 12:37 pm

I'm currently looking more into modern C++ coding styles and I've got to say: Too many developers (not just random PPS students) write "C code with classes".

You might want to call old style C++ "C code with classes" but in the days of C++11 we should definitely start looking into more safe and clever ways of coding. A C-style cast is definitely no longer a safe and clever thing to do. Also simple things like C-style code is not necessarily faster than "complicated" C++ constructs, just because it's simple.
Instead there is often zero to almost no overhead when using new STL or even boost constructs to have more higher level code. Sometimes it's even faster than anything you could write yourself. Also the chance of writing something buggy is a lot smaller.

I would suggest we look into modern style C++ and start using it ourselves and thus be a good example for the students. Orxonox might very well be the first and only real coding experience for these students and it's our responsibility to give them the best learning experience as possible.

I've got some starting links you might want to look at:

https://www.youtube.com/watch?v=xnqTKD8uD64
https://isocpp.org/blog/category/articles-books
http://herbsutter.com/elements-of-modern-c-style/
http://herbsutter.com/2013/06/13/gotw-9 ... ways-auto/
http://channel9.msdn.com/Events/GoingNa ... pp11-Style


Anyway. Good find and nice fix, Fabian!
"I'm Commander Shepard and this is my favorite forum on the internet."

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests