BGE: Added getActionName() function to KX_GameObject()

It works similar to getActionFrame(), you have to give a layer or not (for layer 0) as the argument and it returns the name of the animation that the object is currently playing.

Example:

```
import bge
own = bge.logic.getCurrentController().owner
own.playAction("SomeAction",0,20)
print(own.getActionName())
```
>> SomeAction

Here is an example file, just open the blend file with the terminal opened
and press P, you can see how the current animation is being printed:
{F217484}

Reviewers: moguri, hg1, panzergame, campbellbarton

Reviewed By: panzergame

Subscribers: campbellbarton, hg1, #game_engine

Projects: #game_engine

Differential Revision: https://developer.blender.org/D1443
This commit is contained in:
Mateo de Mayo 2015-08-03 23:47:46 +02:00 committed by Porteries Tristan
parent 73522e1157
commit 23f54076db
Notes: blender-bot 2023-02-14 08:47:27 +01:00
Referenced by issue #45728, Snap to face performance drops down over 50%
7 changed files with 65 additions and 4 deletions

View File

@ -972,6 +972,16 @@ base class --- :class:`SCA_IObject`
:return: The current frame of the action
:rtype: float
.. method:: getActionName(layer=0)
Gets the name of the current action playing in the supplied layer.
:arg layer: The layer that you want to get the action name from.
:type layer: integer
:return: The name of the current action
:rtype: string
.. method:: setActionFrame(frame, layer=0)
Set the current frame of the action playing in the supplied layer.

View File

@ -301,6 +301,18 @@ float BL_Action::GetFrame()
return m_localtime;
}
const char *BL_Action::GetName()
{
if (m_action != NULL) {
return m_action->id.name + 2;
}
else {
return "";
}
}
void BL_Action::SetFrame(float frame)
{
// Clamp the frame to the start and end frame

View File

@ -112,6 +112,8 @@ public:
// Accessors
float GetFrame();
const char *GetName();
struct bAction *GetAction();
// Mutators

View File

@ -68,6 +68,12 @@ float BL_ActionManager::GetActionFrame(short layer)
return action ? action->GetFrame() : 0.f;
}
const char *BL_ActionManager::GetActionName(short layer)
{
BL_Action *action = GetAction(layer);
return action ? action->GetName() : "";
}
void BL_ActionManager::SetActionFrame(short layer, float frame)
{
BL_Action *action = GetAction(layer);

View File

@ -84,6 +84,11 @@ public:
*/
float GetActionFrame(short layer);
/**
* Gets the name of the current action
*/
const char *GetActionName(short layer);
/**
* Sets the current frame of an action
*/

View File

@ -494,6 +494,11 @@ float KX_GameObject::GetActionFrame(short layer)
return GetActionManager()->GetActionFrame(layer);
}
const char *KX_GameObject::GetActionName(short layer)
{
return GetActionManager()->GetActionName(layer);
}
void KX_GameObject::SetActionFrame(short layer, float frame)
{
GetActionManager()->SetActionFrame(layer, frame);
@ -1957,6 +1962,7 @@ PyMethodDef KX_GameObject::Methods[] = {
KX_PYMETHODTABLE_KEYWORDS(KX_GameObject, playAction),
KX_PYMETHODTABLE(KX_GameObject, stopAction),
KX_PYMETHODTABLE(KX_GameObject, getActionFrame),
KX_PYMETHODTABLE(KX_GameObject, getActionName),
KX_PYMETHODTABLE(KX_GameObject, setActionFrame),
KX_PYMETHODTABLE(KX_GameObject, isPlayingAction),
@ -3921,7 +3927,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, stopAction,
"stopAction(layer=0)\n"
"Stop playing the action on the given layer\n")
{
short layer=0;
short layer = 0;
if (!PyArg_ParseTuple(args, "|h:stopAction", &layer))
return NULL;
@ -3937,7 +3943,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, getActionFrame,
"getActionFrame(layer=0)\n"
"Gets the current frame of the action playing in the supplied layer\n")
{
short layer=0;
short layer = 0;
if (!PyArg_ParseTuple(args, "|h:getActionFrame", &layer))
return NULL;
@ -3947,11 +3953,25 @@ KX_PYMETHODDEF_DOC(KX_GameObject, getActionFrame,
return PyFloat_FromDouble(GetActionFrame(layer));
}
KX_PYMETHODDEF_DOC(KX_GameObject, getActionName,
"getActionName(layer=0)\n"
"Gets the name of the current action playing in the supplied layer\n")
{
short layer = 0;
if (!PyArg_ParseTuple(args, "|h:getActionName", &layer))
return NULL;
layer_check(layer, "getActionName");
return PyUnicode_FromString(GetActionName(layer));
}
KX_PYMETHODDEF_DOC(KX_GameObject, setActionFrame,
"setActionFrame(frame, layer=0)\n"
"Set the current frame of the action playing in the supplied layer\n")
{
short layer=0;
short layer = 0;
float frame;
if (!PyArg_ParseTuple(args, "f|h:setActionFrame", &frame, &layer))
@ -3968,7 +3988,7 @@ KX_PYMETHODDEF_DOC(KX_GameObject, isPlayingAction,
"isPlayingAction(layer=0)\n"
"Checks to see if there is an action playing in the given layer\n")
{
short layer=0;
short layer = 0;
if (!PyArg_ParseTuple(args, "|h:isPlayingAction", &layer))
return NULL;

View File

@ -276,6 +276,11 @@ public:
*/
float GetActionFrame(short layer);
/**
* Gets the name of the current action
*/
const char *GetActionName(short layer);
/**
* Sets the current frame of an action
*/
@ -1037,6 +1042,7 @@ public:
KX_PYMETHOD_DOC(KX_GameObject, playAction);
KX_PYMETHOD_DOC(KX_GameObject, stopAction);
KX_PYMETHOD_DOC(KX_GameObject, getActionFrame);
KX_PYMETHOD_DOC(KX_GameObject, getActionName);
KX_PYMETHOD_DOC(KX_GameObject, setActionFrame);
KX_PYMETHOD_DOC(KX_GameObject, isPlayingAction);