Jump to content
  • Advertisement
Sign in to follow this  
speciesUnknown

Class to store in game events... please advise?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

My event object can store strings, ints and floats, and I wish to add more. Here is my header file:
class GUI_event
{
private:
	std::map<std::string, std::string> string_properties;
	std::map<std::string, int> int_properties;
	std::map<std::string, float> float_properties;
public:
	unsigned int num_properties(); 
	std::string name;
	GUI_event(std::string D);
	void dump_properties();


	//methods for storing and retrieving strings
	void add_string_property(std::string name, std::string data);
	bool find_string_property(std::string name, std::string & value);

	//methods for storing and retrieving ints
	void add_int_property(std::string name, int data);
	bool find_int_property(std::string name, int & value);

	//methods for storing and retrieving floats
	void add_float_property(std::string name, float data);
	bool find_float_property(std::string name, float & value);	
};


and the implementation:
/*
 *  GUI_event.cpp
 *  game_engine
 *
 *  Created by shotgunnutter on 21/01/2008.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "GUI_event.h"

GUI_event::GUI_event(std::string D)
{
	name = D;
}

unsigned int GUI_event::num_properties() 
{
	std::map<std::string,std::string>::size_type i;
	std::map<std::string,int>::size_type j;
	 
	i = string_properties.size(); 
	j = int_properties.size();
	return (int)i + (int)j;
};

void GUI_event::dump_properties()
{
	if(string_properties.size() > 0)
	for(std::map<std::string,std::string>::iterator it = string_properties.begin(); it!= string_properties.end(); ++it)
	{
		std::cout<<" Property " <<it->first<<"=string\""<<it->second<<"\"" <<std::endl;
	}
	
	for(std::map<std::string,int>::iterator it = int_properties.begin(); it!= int_properties.end(); ++it)
	{
		std::cout<<" Property " <<it->first<<"=int:"<<it->second<<std::endl;
	}

	for(std::map<std::string,float>::iterator it = float_properties.begin(); it!= float_properties.end(); ++it)
	{
		std::cout<<" Property " <<it->first<<"=float:"<<it->second<<std::endl;
	}

}

//methods for storing and retrieving strings
void GUI_event::add_string_property(std::string name, std::string data)
{
	if(string_properties.find(name) != string_properties.end())
		std::cout<<"Warning, string property "<<name<<" already exists with value "<<string_properties[name]<<std::endl;
	string_properties[name]=data;
}
bool GUI_event::find_string_property(std::string name, std::string & value)
{
	value = string_properties[name];
	return true;
}

//methods for storing and retrieving integers
void GUI_event::add_int_property(std::string name, int data)
{
	if(int_properties.find(name) != int_properties.end())
		std::cout<<"Warning, int property "<<name<<" already exists with value "<<int_properties[name]<<std::endl;
	int_properties[name]=data;
}
bool GUI_event::find_int_property(std::string name, int & value)
{
	value = int_properties[name];
	return true;
}

//methods for storing and retrieving floats
void GUI_event::add_float_property(std::string name, float data)
{
	if(float_properties.find(name) != float_properties.end())
		std::cout<<"Warning, float property "<<name<<" already exists with value "<<float_properties[name]<<std::endl;
	float_properties[name]=data;
}
bool GUI_event::find_float_property(std::string name, float & value)
{
	value = float_properties[name];
	return true;
}



It's one of my first attempts at using stl containers, so I probably did a few things via the Dark Side. What are your general thoughts on this? I am not sure how to return false if the property was not found, in the find_*_property methods. Thanks for any feedback.

Share this post


Link to post
Share on other sites
Advertisement
A GUI_event is composed of three std::maps with congruent firsts, and disparate seconds. A GUI_event has a name. (Why?) A GUI_event can put some stuff into the console out stream. a GUI_event::num_properties returns the number of string and int properties, but does not add the number of float properties. (Eh?)

I don't know what this thing is for, but here is how you can tell if something is found in a std::map or not.


bool GUI_event::find_string_property(std::string name, std::string & value)
{
std::map<std::string,std::string>::iterator it = string_properties.find(name);
if ( it != string_properties.end() )
{
value = (*it).second;
return true;
}
else
{
return false;
}
}


I think that should work ...

The num_properties thing is pretty weird, I think you can do it much easier like this.

unsigned int GUI_event::num_properties()
{
return static_cast<unsigned int>(string_properties.size() + int_properties.size() + float_properties.size());
};


Share this post


Link to post
Share on other sites
Hmm, some interesting answers.

What is it for? Its the storage unit for the internal messaging system of my game. Its pushed onto a queue and used to communicate events between the subsystems. Its job is to store a series of property->value pairs. GUI widgets can be given one for each of their triggers. Its name is derived from the fact that originally I wanted to use it ONLY as a communication system between the GUI widgets, but I now plan to use it for communication between other systems, and will change the name.

Thank you Canto for demonstrating the easy way to do a couple of things. I was having trouble searching through an std::map, or example. (I'm new to STL but not programming in general).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!