Add Object actuator, generate with name

Game Engine, Players & Web Plug-in, Virtual Reality, support for other engines

Moderators: jesterKing, stiv

Post Reply
colinn
Posts: 10
Joined: Sat Jan 28, 2012 3:26 am

Add Object actuator, generate with name

Post by colinn » Sat Jan 28, 2012 3:35 am

Hey, thought I would post my changes to KX_SCA_AddObjectActuator::PyInstantAddObject.


I needed this change to granularly send messages to my dynamically created objects.

KX_SCA_AddObjectActuator.h

Code: Select all

KX_PYMETHOD_DOC_VARARGS(KX_SCA_AddObjectActuator,InstantAddObject);
KX_SCA_AddObjectActuator.cpp

Code: Select all


PyMethodDef KX_SCA_AddObjectActuator::Methods[] = {
  {"instantAddObject", (PyCFunction) KX_SCA_AddObjectActuator::sPyInstantAddObject, METH_VARARGS,"instantAddObject(name) : immediately add object without delay\n"},
  {NULL,NULL} //Sentinel
};

PyObject* KX_SCA_AddObjectActuator::PyInstantAddObject(PyObject* args)
{
    PyObject* pystring;

	char* name = 0;

	if (!PyArg_ParseTuple(args, "s", &name))
		InstantAddObject();
	
	InstantAddObject(name);

	Py_RETURN_NONE;
}

void	KX_SCA_AddObjectActuator::InstantAddObject(const char* name)
{
	if (m_OriginalObject)
	{
		// Add an identical object, with properties inherited from the original object	
		// Now it needs to be added to the current scene.
		SCA_IObject* replica = m_scene->AddReplicaObject(m_OriginalObject,GetParent(),m_timeProp );
		KX_GameObject * game_obj = static_cast<KX_GameObject *>(replica);
		game_obj->setLinearVelocity(m_linear_velocity ,m_localLinvFlag);
		game_obj->setAngularVelocity(m_angular_velocity,m_localAngvFlag);
		game_obj->ResolveCombinedVelocities(m_linear_velocity, m_angular_velocity, m_localLinvFlag, m_localAngvFlag);

        // If we have a new name, assign it.
        if(name != 0)
        {
            game_obj->SetName(name);
        }

		// keep a copy of the last object, to allow python scripters to change it
		if (m_lastCreatedObject)
		{
			//Let's not keep a reference to the object: it's bad, if the object is deleted
			//this will force to keep a "zombie" in the game for no good reason.
			//m_scene->DelayedReleaseObject(m_lastCreatedObject);
			//m_lastCreatedObject->Release();

			//Instead we use the registration mechanism
			m_lastCreatedObject->UnregisterActuator(this);
			m_lastCreatedObject = NULL;
		}
		
		m_lastCreatedObject = replica;
		// no reference
		//m_lastCreatedObject->AddRef();
		// but registration
		m_lastCreatedObject->RegisterActuator(this);
		// finished using replica? then release it
		replica->Release();
	}
}

stiv
Posts: 3645
Joined: Tue Aug 05, 2003 7:58 am
Location: 45N 86W

Post by stiv » Sat Jan 28, 2012 9:53 pm

If this is generally useful, you might want to create a patch and add it to the patch tracker:

http://wiki.blender.org/index.php/Dev:D ... ss/Patches

colinn
Posts: 10
Joined: Sat Jan 28, 2012 3:26 am

Post by colinn » Tue Jan 31, 2012 7:39 pm

stiv wrote:If this is generally useful, you might want to create a patch and add it to the patch tracker:

http://wiki.blender.org/index.php/Dev:D ... ss/Patches
Done, thanks for the tip.

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests