Jump to content

  • Log In with Google      Sign In   
  • Create Account


Little help needed


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 BaneTrapper   Members   -  Reputation: 1140

Like
0Likes
Like

Posted 08 March 2013 - 02:59 PM

Hello.


I have a class "Input", that many other classes need( Would use ) and i need only one instance of class "Input" and i don't want to pass it around as reference all over the place.

Suggestions please.

 

Currently the class looks like this

 

class Input

{

public:

    void SetupInput(fonts &objFon);


    sf::RectangleShape recBackground;

    sf::Text txt;

};

Edited by BaneTrapper, 08 March 2013 - 03:10 PM.

Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


Sponsor:

#2 CryoGenesis   Members   -  Reputation: 494

Like
3Likes
Like

Posted 08 March 2013 - 03:59 PM

Well, if you don't want to pass it around as a reference and you only need one instance of it then you can just make a static Input object. Then, all you have to do is use the static object instead of passing it as a reference everywhere.

I'm not sure how you would do it in C++. 

Have a main.cpp or something then declare a namespace, then inside that namespace have a static Input object then all you have to do is: namespaceName::input.something().

Not sure if that'll work though, I'm not very proficient in C++.



#3 Damian.   Members   -  Reputation: 272

Like
2Likes
Like

Posted 08 March 2013 - 04:06 PM

Singleton (not the best choice? but still can be used for what you want) or Global variable in namespace.



#4 Nercury   Crossbones+   -  Reputation: 766

Like
2Likes
Like

Posted 08 March 2013 - 04:38 PM

Singleton suggestion (single, global class) is most likely a correct answer to your question.

 

However, I wonder what your input class actually do. Maybe this particular case can be elegantly solved without a global variable.



#5 ultramailman   Prime Members   -  Reputation: 1561

Like
3Likes
Like

Posted 08 March 2013 - 05:40 PM

I would assign an Input to a global variable inside one .cpp file, and have the header of that cpp file declare the variable as extern. Then only the files that include that header can see it.

example:
main.cpp:
#include "main.hpp"
#include "input.hpp"
Input in;
int main(int argc, char ** argv)
{
//initialize in
}
main.hpp:
#ifndef MAIN_HPP
#define MAIN_HPP
#include "input.hpp"
extern Input in;
#endif
other.cpp:
#include "main.hpp"
void function(void)
{
//do things with the variable in
}


#6 AgPF6   Members   -  Reputation: 161

Like
3Likes
Like

Posted 08 March 2013 - 07:31 PM

You can create a base class the has static members which pertain to the input aspects.  Because static memebers are visible to all the members of the class, these static members only get intialized once, but are available to all classes that derive from the input base class. 

 

To destroy this class you could do several things.  Call a function called TerminateInput() that shuts down all the static members, or out the code into the destructor. 

 

In all honesty though, input is usally just  a player thing.  I don't see why you would need to do this.



#7 BaneTrapper   Members   -  Reputation: 1140

Like
0Likes
Like

Posted 09 March 2013 - 04:12 AM

I should describe what the class will actually do.
Basically what it will be used for is to get input that will open files.

In class Map a user presses "OpenMap" Input class should get called to get input, when user is done typing the file name i need to get the string that holds the file name and open it in map.

Same will happen in class TileSheet or class Music.
 

I will probably go for singleton class.

Am looking for other ways to do this for learning. Thanks on all the posts


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/


#8 EWClay   Members   -  Reputation: 659

Like
1Likes
Like

Posted 09 March 2013 - 04:46 AM

One way to avoid having a global for this is to use the Model-View-Controller pattern.

In this scheme the Map would not handle user input but would simply have a LoadMap(name) function. A controller class would handle the UI. You can store any UI state (such as input strings) in the controller class, and the Map would not need access because the controller would call the LoadMap() function.

#9 BaneTrapper   Members   -  Reputation: 1140

Like
0Likes
Like

Posted 09 March 2013 - 11:26 AM

One way to avoid having a global for this is to use the Model-View-Controller pattern.

In this scheme the Map would not handle user input but would simply have a LoadMap(name) function. A controller class would handle the UI. You can store any UI state (such as input strings) in the controller class, and the Map would not need access because the controller would call the LoadMap() function.

Interesting.


Current projects:
The Wanderer, 2d turn based rpg style game

www.gamedev.net/topic/641117-check-up-the-wanderer/





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS