# Pointer to a user defined object?

This topic is 4916 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Wow, i've got lots of dumb questions now i've started on my game again...Ah well. Anyways, I couldn't find this in any of my books, so i thought i should ask here. I have 2 classes: 1 is my main GameEngine, and the other is my Sprite class. Neither has any relation to the other. Now my sprite class needs to use some variables that are defined in my GameEngine class. The problem lies in the fact I want to keep my game engine independant from the game. Ie, i want to be able to use this game engine on another game, w/o having to rewrite any part of it, so that means I can't just extern the gameEngine variable[delaced in my main .cpp file] within the Sprite class. So I was thinking I needed to use a pointer to my gameEngine...but how? I'm tottal at lose with this problem...I hope i made myself clear, but i'm not tottaly sure how to descript my situation. Cheers! Chris

##### Share on other sites
Quote:
 Neither has any relation to the other. Now my sprite class needs to use some variables that are defined in my GameEngine class.

You might want to take a few long hard thoughts about your engine architecture if your sprites need to access the internal variables of the game engine..... That sounds more like a problem in design, as opposed to implementation. However, if you refuse to improve what would seem to be a bad design (although admittedly, I know nothing else about it), then your only recourse is to use, as you said, a pointer to the GameEngine class. If you need a pointer (or reference) to a specific GameEngine, you might want to look into Singletons (google it).

[EDIT] Oh, and, as Majin said, I'd pick up the Design Pattern's book, I got it a few days ago, and it is a real treasure trove of information!

##### Share on other sites
Quote:
 Original post by _SigmaI have 2 classes: 1 is my main GameEngine, and the other is my Sprite class. Neither has any relation to the other. Now my sprite class needs to use some variables that are defined in my GameEngine class.

Can you tell us what variables are you talking about? Screen resolution, for example?
Quote:
 The problem lies in the fact I want to keep my game engine independant from the game. Ie, i want to be able to use this game engine on another game, w/o having to rewrite any part of it, so that means I can't just extern the gameEngine variable[delaced in my main .cpp file] within the Sprite class. So I was thinking I needed to use a pointer to my gameEngine...but how? I'm tottal at lose with this problem...I hope i made myself clear, but i'm not tottaly sure how to descript my situation. Cheers! Chris

Give us more details so we can answer more accurately. But I usually stay away from the "extern" keyword :) Also, independent of your answer, you should try "Design Patterns". [google] it, and buy the book :) It is a must read for me, and would enlighten your mind in respect to good game design.

##### Share on other sites
hahah, yes SirLuthor, that does sound bad doesn't it? I of course am being retarded. I really need to not program at night :P You see the, the problem lay in the fact that the Sprite does indeed have relation to the engine class. I was just declaring it globally outside of my class for testing purposes, then forgot thats where i had put it. So of course if my Sprite is part of the engine, everything is solved. and its not a bad design(i hope) either way, thanks for the singleton idea, i think i have a use for that soon. Sorry to be so retarded. Damn noob-ness!

@majin, i'll check that book out. cheers!

//edit. This is the book i assume? Linky
//edit2: ah great. my uni's library has it. i'll pick it up, if in fact it is the right book :P

##### Share on other sites
Quote:
 Original post by _Sigmahahah, yes SirLuthor, that does sound bad doesn't it? I of course am being retarded. I really need to not program at night :P You see the, the problem lay in the fact that the Sprite does indeed have relation to the engine class.

Quote:
 thanks for the singleton idea, i think i have a use for that soon. Sorry to be so retarded. Damn noob-ness!

Please, do a lot of research about singletons or you can have some serious problems with it, or else overuse it.

Quote:
 //edit. This is the book i assume? Linky//edit2: ah great. my uni's library has it. i'll pick it up, if in fact it is the right book :P

_Sigma, this is DA BOOK, and is almost impossible to talk seriously about object oriented game design without even touch some of the design patterns.

##### Share on other sites
Oh, realation only in the fact that Sprite is a member variable of the GameEngine
GameEngine{Sprite Me;//other stuff here};

Quote:
 Please, do a lot of research about singletons or you can have some serious problems with it, or else overuse it.

Sounds fair. I'll do that tomorrow. Right now its sleeping time :P
Thanks for all the help, i appreciate it.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 15
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
634136
• Total Posts
3015756
×