Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Ping Tutorial Question  (Read 1980 times)

0 Members and 1 Guest are viewing this topic.

codelyoko373

  • Newbie
  • *
  • Posts: 8
    • View Profile
Ping Tutorial Question
« on: August 26, 2017, 02:57:45 am »
I'm quite new to SFML and the first tutorial I watched for SFML was how to basically make pong, this tutorial:

It this tutorial he taught me how to create this basic menu system. All menu's such as the Main Menu and the actual game would inherit this "BaseState" subclass: https://image.prntscr.com/image/Jx0fTP5SRGySmurrJ-_img.png
And then the actual class had code to manage these menus:
https://image.prntscr.com/image/radyTH_xRkuqrbcmEIHMuw.png
https://image.prntscr.com/image/7Mgd1AxhTpmxMJ6RVkixAg.png

The way you would change state is using the SetState function like this:
CoreState.SetState(new MainMenu());
but I've noticed that the new keyword is used but the created MainMenu object shown is this example isn't actually deleted anywhere, the only things that are deleted are the pointers used within these menus. It would have to be deleted wouldn't it or am I missing something?
I've tried deleting the menu with "delete this" in the destroy function that is called when the menu changes but it doesn't seem to work.

Sorry for the long post
« Last Edit: August 26, 2017, 03:05:46 am by codelyoko373 »

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: Ping Tutorial Question
« Reply #1 on: August 26, 2017, 10:14:14 am »
For the state stack it's understandable to use heap allocation as it stores the states as pointer to base class - although modern C++ would prefer a smart pointer (aka RAII) which can be stored in a vector using std::move() and takes care of the new/delete problem. Members such as font and text have no need for heap allocation, and can be initialised via the constructor's initialiser list rather than using double initialisation (which is still true even if you do decide to use smart pointers). It seems unusual to write 'this' everywhere (to me at least), although it appears to be a preference thing - many people instead prefix members with m_ or _. If you can, check out the first SFML game development book, it has a great example of a state stack, and is probably the best of the bunch.

codelyoko373

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Ping Tutorial Question
« Reply #2 on: August 26, 2017, 04:22:48 pm »
For the state stack it's understandable to use heap allocation as it stores the states as pointer to base class - although modern C++ would prefer a smart pointer (aka RAII) which can be stored in a vector using std::move() and takes care of the new/delete problem. Members such as font and text have no need for heap allocation, and can be initialised via the constructor's initialiser list rather than using double initialisation (which is still true even if you do decide to use smart pointers). It seems unusual to write 'this' everywhere (to me at least), although it appears to be a preference thing - many people instead prefix members with m_ or _. If you can, check out the first SFML game development book, it has a great example of a state stack, and is probably the best of the bunch.

Apparently the guy used "this" a lot to just show the viewers what variables he was referencing. And that state pointer is a raw pointer so surely it's bad that It's not being deleted anywhere

 

anything