[Bf-committers] Using GameObject IDs when calling from Python, or changing names of added objects?

Toni Alatalo bf-committers@blender.org
Mon, 15 Sep 2003 17:00:33 +0300


Greetings,

this is also to answer Maci's call for game engine developers, even 
though I probably won't be able to do anything with the core, but have 
planned trying to work on little bits an pieces on the Python API.

First a very basic question: can gameobjects be called with IDs instead 
of names from Python?

The itch is that (e.g.) the trackTo actuator can currently take only 
the name of the object as a parameter, and for objects added with the 
addObject actuator that is a problem 'cause they all have the same 
name..

Looking at that actuator source 
(/source/gameengine/Ketsji/KX_TrackToActuator.cpp) and the SetObject 
method, this seems to do the thing currently:

CValue* gameobj = 
SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_Str
ing(nameArg));

I thought of the possibility of not changing the method call at all, 
just adding  a check whether the Arg is an ID number or a name string, 
and acting accordingly. Only then I realized that the Python side 
probably does not know the ID that is the pointer in the C++ side .. or 
does it? I don't really know yet how these bindings work. Asked a 
programming-knowledgeable friend (Cc'd) who, not knowing Blender, 
suspected that what id(gameobject) returns in Python might actually 
work as the right pointer in the C-side, but also noted that it's 
probably not a good way of doing it if the object is deleted and 
carbage collected and there's a remaining pointer somewhere etc.

Looking at the non-gameengine side for hints, from how 
Scene.link(object) deals with it .. this seems to me that the object 
reference gotten from python can be used in C allright (from Scene.c), 
also gave an example of argument type checking for the case where both 
name and id would be allowed:

if (!PyArg_ParseTuple (args, "O!", &Object_Type, &bpy_obj))
       return EXPP_ReturnPyObjError (PyExc_TypeError,
               "expected Object argument");

   else { /* Ok, all is fine, let's try to link it */
     Object *object = bpy_obj->object;

Another way of course would be to add a setName actuator to be able to 
rename the added objects, but can the names be changed when the engine 
is running? Things might go awry..

source/gameengine/GameLogic/SCA_LogicManager seems to keep the registry:
GEN_Map<STR_HashedString,CValue*>       m_mapStringToGameObjects;
CValue* gameobj = 
SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_Str
ing(nameArg));

Also the addObject actuator could be modified to e.g. add a sequence 
number to the name of the newly added object, preferably returning the 
name it got. This probably would not harm even if the ID passing 
worked? And might be the best solution to this problem otherwise too, 
or?

Any insight and hints about the different possibilities welcome, 
implementation as well, as my scratching will probably take some time 
still..

~Toni

BTW, a workaround for this problem has been to always track to an empty 
and move that to match the position of the actual target object, works 
ok, but I thought this would be a trivial enough thing to try what 
working in the engine would be like.