Creating Entities

Problems building or running the engine, queries about how to use features etc.
Post Reply
Jarwulf
Greenskin
Posts: 109
Joined: Wed Apr 28, 2010 4:40 am

Creating Entities

Post by Jarwulf »

Hi,

My goal is to make an object with an attached light that moves around randomly. I couldn't decide whether I wanted to make an object that inherited off Entity, Node, or Light so I simply made an object that encloses them all. Since I want to create several of them I want an object that will take care of the code for creating the entity, node, and light automatically. Would this constructor work? Or is there some sort of problem like clashing names?

Code: Select all

Object::Object()
{

//node
	Ogre::SceneNode* SphereNode = mSceneMgr->getRootSceneNode()->createChildSceneNode(Ogre::Vector3(25,10,0));
	
//entity
Ogre::Entity* Mass = mSceneMgr->createEntity("Sphere", "lightsphere.mesh");
    SphereNode->attachObject(Mass);
    Mass->setMaterialName("Template/Red50");

	//light
	Ogre::Light* l = mSceneMgr->createLight("Light");
        l->setAttenuation(600, 1, .007, 0.0002);
       SphereNode->attachObject(l);

}

User avatar
jacmoe
OGRE Retired Moderator
OGRE Retired Moderator
Posts: 20570
Joined: Thu Jan 22, 2004 10:13 am
Location: Denmark
x 179
Contact:

Re: Creating Entities

Post by jacmoe »

Big problems. :)
But none you can't overcome:
Put an ID, or generate a unique ID in the constructor or a utility class, and append it to the names of the scene node, entity and light.
/* Less noise. More signal. */
Ogitor Scenebuilder - powered by Ogre, presented by Qt, fueled by Passion.
OgreAddons - the Ogre code suppository.
Jarwulf
Greenskin
Posts: 109
Joined: Wed Apr 28, 2010 4:40 am

Re: Creating Entities

Post by Jarwulf »

jacmoe wrote:Big problems. :)
But none you can't overcome:
Put an ID, or generate a unique ID in the constructor or a utility class, and append it to the names of the scene node, entity and light.

How do I autogenerate the names and assign them to the entity? I don't know how to phrase it in google to get good results...


Also what needs to be unique? Just the names of the node, entity, and light or the variable names of each of these as well?
User avatar
vitefalcon
Orc
Posts: 438
Joined: Tue Sep 18, 2007 5:28 pm
Location: Seattle, USA
x 13

Re: Creating Entities

Post by vitefalcon »

A simple class like this would do the job.

NameGenerator.h

Code: Select all

#include <string>

/**
 * \brief	A utility class to generate unique names.
**/
class NameGenerator
{
public:

	/**
	 * \brief	Gets the next unique name.
	**/
	static std::string Next();

	/**
	 * \brief	Gets the next unique name for a given prefix.
	**/
	static String Next(const std::string& prefix);

	/**
	 * \brief	Counts the number of unique auto-generated names.
	**/
	static size_t Count();

	/**
	 * \brief	Counts the number of names generated for a given prefix.
	 * \param	prefix	The prefix of the generated names.
	**/
	static size_t Count(const std::string& prefix);
private:
	NameGenerator(void);
	~NameGenerator(void);

	typedef std::map<std::string, uint32> NameCountMap;
	static NameCountMap s_nameCount;
};
NameGenerator.cpp

Code: Select all

#include <sstream>

const std::string DEFAULT_NAME_PREFIX = "AutoGeneratedName";
NameGenerator::NameCountMap NameGenerator::s_nameCount;
//---------------------------------------------------------------------
NameGenerator::NameGenerator(void)
{
}
//---------------------------------------------------------------------
NameGenerator::~NameGenerator(void)
{
}
//---------------------------------------------------------------------
String NameGenerator::Next()
{
	return Next(DEFAULT_NAME_PREFIX);
}
//---------------------------------------------------------------------
String NameGenerator::Next( const std::string& prefix )
{
	if (s_nameCount.count(prefix) == 0)
	{
		s_nameCount[prefix] = 0;
	}
	std::stringstream ss;
	ss<<prefix<<"_"<<s_nameCount[prefix]++;
	return ss.str();
}
//---------------------------------------------------------------------
size_t NameGenerator::Count()
{
	size_t count = Count(DEFAULT_NAME_PREFIX);
	return count;
}
//---------------------------------------------------------------------
size_t NameGenerator::Count( const std::string& prefix )
{
	size_t count = (s_nameCount.count(prefix) == 0)?0:s_nameCount[prefix];
	return count;
}
Usage:

Code: Select all

std::string autogenerated_name = NameGenerator::Next(); // Generates (from the above code) 'AutoGeneratedName_x', where x is a number
std::string new_entity_name = NameGenerator::Next("Entity"); // Generates 'Entity_x', where x is a number
Hope this solves your problem regarding generating unique names.

About the which ones should have unique names, all objects of a particular group should have unique names. Like every entity should have a unique name, which doesn't clash with another entity and the same goes for lights, scene nodes and so on. But between the groups they can have the same name, like an Entity can have the name "MyActor", while *a* scene node also has the same name.
Image
Jarwulf
Greenskin
Posts: 109
Joined: Wed Apr 28, 2010 4:40 am

Re: Creating Entities

Post by Jarwulf »

vitefalcon wrote:A simple class like this would do the job.


Usage:

Code: Select all

std::string autogenerated_name = NameGenerator::Next(); // Generates (from the above code) 'AutoGeneratedName_x', where x is a number
std::string new_entity_name = NameGenerator::Next("Entity"); // Generates 'Entity_x', where x is a number
Hope this solves your problem regarding generating unique names.

About the which ones should have unique names, all objects of a particular group should have unique names. Like every entity should have a unique name, which doesn't clash with another entity and the same goes for lights, scene nodes and so on. But between the groups they can have the same name, like an Entity can have the name "MyActor", while *a* scene node also has the same name.



Alright so could I use it like this? Or would I have to declare a Namegenerator object explicitly?

Code: Select all

//header.h
#include "NameGenerator.h"

class Object
{
Object:Object()
{
Ogre::Entity* Mass = mSceneMgr->createEntity(NameGenerator::Next("Entity"), "lightsphere.mesh");
}
}

>>>>>>>>>>
//main.cc
#include "header.h"

int main()
{
Object Object1, Object2;
}
User avatar
vitefalcon
Orc
Posts: 438
Joined: Tue Sep 18, 2007 5:28 pm
Location: Seattle, USA
x 13

Re: Creating Entities

Post by vitefalcon »

Yea, just like that.
Image
Post Reply