diff options
author | Mateo de Mayo <mateodemayo@gmail.com> | 2015-08-04 00:47:46 +0300 |
---|---|---|
committer | Porteries Tristan <republicthunderbolt9@gmail.com> | 2015-08-04 01:10:33 +0300 |
commit | 23f54076db6d241af2a8f9404ab5f5b8072a4db0 (patch) | |
tree | c13d269da7a6414e0d121f1254afcf16b8541864 | |
parent | 73522e1157eba1f475454c153f7c027f5dd097ea (diff) |
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
-rw-r--r-- | doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst | 10 | ||||
-rw-r--r-- | source/gameengine/Ketsji/BL_Action.cpp | 12 | ||||
-rw-r--r-- | source/gameengine/Ketsji/BL_Action.h | 2 | ||||
-rw-r--r-- | source/gameengine/Ketsji/BL_ActionManager.cpp | 6 | ||||
-rw-r--r-- | source/gameengine/Ketsji/BL_ActionManager.h | 5 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_GameObject.cpp | 28 | ||||
-rw-r--r-- | source/gameengine/Ketsji/KX_GameObject.h | 6 |
7 files changed, 65 insertions, 4 deletions
diff --git a/doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst b/doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst index a24aa546cb9..88b1d7da076 100644 --- a/doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst +++ b/doc/python_api/rst/bge_types/bge.types.KX_GameObject.rst @@ -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. diff --git a/source/gameengine/Ketsji/BL_Action.cpp b/source/gameengine/Ketsji/BL_Action.cpp index 9bae119107e..12a1caee221 100644 --- a/source/gameengine/Ketsji/BL_Action.cpp +++ b/source/gameengine/Ketsji/BL_Action.cpp @@ -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 diff --git a/source/gameengine/Ketsji/BL_Action.h b/source/gameengine/Ketsji/BL_Action.h index dd1cd1f69ff..379dd52df5b 100644 --- a/source/gameengine/Ketsji/BL_Action.h +++ b/source/gameengine/Ketsji/BL_Action.h @@ -112,6 +112,8 @@ public: // Accessors float GetFrame(); + const char *GetName(); + struct bAction *GetAction(); // Mutators diff --git a/source/gameengine/Ketsji/BL_ActionManager.cpp b/source/gameengine/Ketsji/BL_ActionManager.cpp index 975f9ea6da8..9e4690548d3 100644 --- a/source/gameengine/Ketsji/BL_ActionManager.cpp +++ b/source/gameengine/Ketsji/BL_ActionManager.cpp @@ -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); diff --git a/source/gameengine/Ketsji/BL_ActionManager.h b/source/gameengine/Ketsji/BL_ActionManager.h index 00e536655c7..97d6d88cf22 100644 --- a/source/gameengine/Ketsji/BL_ActionManager.h +++ b/source/gameengine/Ketsji/BL_ActionManager.h @@ -85,6 +85,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 */ void SetActionFrame(short layer, float frame); diff --git a/source/gameengine/Ketsji/KX_GameObject.cpp b/source/gameengine/Ketsji/KX_GameObject.cpp index 536670dde1a..651df9aebbe 100644 --- a/source/gameengine/Ketsji/KX_GameObject.cpp +++ b/source/gameengine/Ketsji/KX_GameObject.cpp @@ -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; diff --git a/source/gameengine/Ketsji/KX_GameObject.h b/source/gameengine/Ketsji/KX_GameObject.h index 99c8a6d718b..c10802a83b2 100644 --- a/source/gameengine/Ketsji/KX_GameObject.h +++ b/source/gameengine/Ketsji/KX_GameObject.h @@ -277,6 +277,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 */ void SetActionFrame(short layer, float frame); @@ -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); |