function override

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

function override

Post by egizz983 » December 16th, 2016, 1:48 pm

Hi guys have issue with my GUI system , i am not great debugger but i think problem is inside function overriding , GUIElement class is and entity of all elements and I am making GUITextInput element inside GameStateLogin for some reason its not being displayed , and with debugger i track all the way where it should drawing begins and its seems that Element function are not override by GUITextInput draw function its seems that i did everything right so problem might be the way i create a TextInput inside GUIManager
Attachments
Game.rar
(1.16 MiB) Downloaded 126 times

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: function override

Post by albinopapa » December 16th, 2016, 7:17 pm

Here's my suggestion

GUIElement.h

Code: Select all

	virtual void Draw(Graphics& gfx) = 0;
GUITextInput.h

Code: Select all

	void Draw(Graphics& gfx) override;
Sometimes it seems that the only way to get the overridden function is to either declare the virtual function as = 0 or to specify override on the override functions. I recommend doing both, unless the parent function actually has purpose and isn't just left empty.

Keep in mind though, that by doing the above with the = 0 on parent virtual functions, you are required to make override functions for ALL child classes of that type.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: function override

Post by albinopapa » December 16th, 2016, 7:22 pm

Oh, and as a tip, perhaps instead of writing GUI in front of all GUI elements, I might suggest making a namespace called GUI and just have all your gui elements inside of that namespace;

Code: Select all

GUIElement.h
namespace GUI
{
     class Element
     {
     };
}

GUIElement.cpp
namespace GUI
{
     Element::Element()
}

or if you don't do the above, you could just fully qualify the names as such in the CPP files.
GUI::Element::Element()

GUITextInput.h
namespace
{
     class TextInput
     {
          TextInput();
     };
}
Just a suggestion, it doesn't do anything extra for you or your program, except organize things a little bit.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: function override

Post by egizz983 » December 16th, 2016, 8:03 pm

albinopapa did u test it ? did it worked ? cuz the other problem and might be the problem that is causing this , is that i am pushing child class on parent class vector and this cause slicing, and i should probably do

Code: Select all

std::vector<std::unique_pt<GUIElement>> Elements;

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: function override

Post by albinopapa » December 16th, 2016, 9:15 pm

No, I didn't test, and yes if you are trying to pass child classes as parent classes, there will be slicing. You must use references or pointers, so a vector<unique_ptr<base>> would be a good way to go.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: function override

Post by albinopapa » December 16th, 2016, 9:20 pm

Ok, so looking at your GUIManager class, which is where your vector is, yes, it should be std::vector<std::unique_ptr<GUIElement>>.

If you do what I posted above about the =0 and override stuff, you wouldn't be able to make GUIElement objects, so it would have told you that you were messing up. Since classes that have virtual functions that are declare = 0, (called pure virtual functions) can't be instantiated, the compiler would tell you during compile time, that something is wrong and GUIElement is an abstract class and can't be instantiated.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: function override

Post by egizz983 » December 16th, 2016, 9:37 pm

yea i did with

Code: Select all

std::vector<std::shared_ptr<GUIElement>> Elements;
now its seems to be working fine

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: function override

Post by egizz983 » December 17th, 2016, 10:48 am

I still need your advice here i kind a have issue with a mouse object inside GUIElement , till now i was passing it as a parameter for Update function and some of mouse functions but now i got a problem i want to draw something that called label , and i need mouse coordinates for that , i need pointer or a reference to a mouse , i cannot draw it inside Update function cuz then i would need reference to gfx and i cannot draw inside Draw function cuz then i would need mouse pointer or reference ,my GUIManager have mouse pointer , and i was able to think of two things , ether i make GUIElement friend of GUIManager so GUIElement could access mouse pointer , or i would have to pass a pointer for each element . not sure what other options i have
label need mouse coordinates i will probably make DrawLabel functions to draw label on its own and then call it inside virtual draw function but as i said i need mouse for this one , and could not think good ways of doing it , i also dont want to pass mouse to IsHover and IsLMB functions
Last edited by egizz983 on December 17th, 2016, 9:28 pm, edited 1 time in total.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: function override

Post by albinopapa » December 17th, 2016, 11:12 am

The IsHover and IsLMB functions, if you don't want to pass the Mouse object pointer, you can just send the coordinates the click status of the left mouse button;

bool IsLMB(int X, int Y, bool leftPressed);
bool IsHover(int X, int Y);

For the drawing issue, you can store the mouse coordinates during the update function as they won't change between the Update function and the Draw function, that way you won't have to change function signatures.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: function override

Post by egizz983 » December 17th, 2016, 9:21 pm

the thing i did is GUIManager calling element.UPdate function and pass mouse and keyboard there so i made bool Hover and bool LMB variables witch was set inside update function instead having they own calculations now update will check it for them and those functions will only return value of Hover and LMB .

Post Reply