Spawn Entity with Behavior already set?

Get answers to all your basic programming questions. No Ogre questions, please!
putty174
Gnoblar
Posts: 4
Joined: Mon Apr 15, 2013 8:24 pm

Spawn Entity with Behavior already set?

Post by putty174 »

I am trying to build a Spawner that automatically creates copies of a mesh, hook them up to entities and scenenodes and drop them into the world. In addition to this, I want to save a behavior function so that when the entity is spawned, it will engage in that behavior as soon as it is dropped into the world. I was wondering if this was possible to be done in Ogre3d. I was entertaining the thought of reassigning function pointers with a signature like this:

void SceneManager::setBehavior(void (*behavior)(Ogre::SceneNode))

and then save the behavior somewhere. Any ideas how I might accomplish this?
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: Spawn Entity with Behavior already set?

Post by areay »

I think your questions are general programming issues as opposed to Ogre3D ones. Anyway, this spawner that you speak of sounds like a a Factory-pattern.

Here's what your factory declaration might look like (not actual code)

Code: Select all


#include "all the things that it might spawn; enemies, weapons, animals etc.h"

class myFactory
{
public:
  GameObject* createEnemy(Ogre::Vector3 spawnLocation, std::string meshName, int behaviour_id); 
  GameObject* createWeapon(Ogre::Vector3 spawnLocation, std::string meshName, GameObject *owner, void (*behaviour)(int some_other_info_maybe));
}
Your implementation of the factory could be a singleton that had a pointer to the scenemanager which could then handle all of the messy details of creating nodes, attaching the mesh and then optionally call member functions on the newly created objects. Then you just return a base class (I've called it GameObject) pointer to the client. The two methods I outlined above take a different method of specifying the behaviour; the first one uses an integer ID (could be from an enumeration) and then in its definition does a case, or if/else, type check to determine what it should actually do. The second method would just use the passed in function pointer.

Apologies if this is all old-hat to you.
putty174
Gnoblar
Posts: 4
Joined: Mon Apr 15, 2013 8:24 pm

Re: Spawn Entity with Behavior already set?

Post by putty174 »

areay wrote:I think your questions are general programming issues as opposed to Ogre3D ones. Anyway, this spawner that you speak of sounds like a a Factory-pattern.

Here's what your factory declaration might look like (not actual code)

Code: Select all


#include "all the things that it might spawn; enemies, weapons, animals etc.h"

class myFactory
{
public:
  GameObject* createEnemy(Ogre::Vector3 spawnLocation, std::string meshName, int behaviour_id); 
  GameObject* createWeapon(Ogre::Vector3 spawnLocation, std::string meshName, GameObject *owner, void (*behaviour)(int some_other_info_maybe));
}
Your implementation of the factory could be a singleton that had a pointer to the scenemanager which could then handle all of the messy details of creating nodes, attaching the mesh and then optionally call member functions on the newly created objects. Then you just return a base class (I've called it GameObject) pointer to the client. The two methods I outlined above take a different method of specifying the behaviour; the first one uses an integer ID (could be from an enumeration) and then in its definition does a case, or if/else, type check to determine what it should actually do. The second method would just use the passed in function pointer.

Apologies if this is all old-hat to you.
In this context, what I'm trying to do would be a weapon-factory, where it takes in a behavior and saves it somewhere so it can pump out more copies of the weapon with the same behavior by itself, without having the client call the function over and over again. I was wondering how I would save that behavior, or is the only solution is to have the client use the createWeapon function over and over again?

***Sidenote: Can I get a Mod to see where this topic should go and move this if necessary?
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: Spawn Entity with Behavior already set?

Post by areay »

putty174 wrote: In this context, what I'm trying to do would be a weapon-factory, where it takes in a behavior and saves it somewhere so it can pump out more copies of the weapon with the same behavior by itself
Well another member function for the factory might be 'void setDefaultWeaponBehaviour(int behaviour_type) ' so that when a weapon is created (without passing in some optional behaviour parameter) the factory uses this default.
putty174 wrote: without having the client call the function over and over again.
I'm a bit confused about this; the client is going to have to call the factory over and over again if it wants more weapons right?
putty174
Gnoblar
Posts: 4
Joined: Mon Apr 15, 2013 8:24 pm

Re: Spawn Entity with Behavior already set?

Post by putty174 »

I was planning for this spawner to automatically generate items after setting its initial conditions, without further user input.
User avatar
areay
Bugbear
Posts: 819
Joined: Wed May 05, 2010 4:59 am
Location: Auckland, NZ
x 69

Re: Spawn Entity with Behavior already set?

Post by areay »

sounds like you need an update(Ogre::Real timeSinceLastFrame) function then.