blender/source/gameengine/GameLogic/SCA_ILogicBrick.cpp
Porteries Tristan 2050ecc307 BGE: Fix T48071: Global logic manager
Previously the logic manager was used as a global variable for SCA_ILogicBrick::m_sCurrentLogicManager,
this request to always update it before run any python script and allow call function like
ConvertPythonTo[GameObject/Mesh]. The bug showed in T48071 is that as exepted the global
m_sCurrentLogicManager is not updated with the proper scene logic manager.
Instead of trying to fix it by updating the logic manager everywhere and wait next bug report to add
a similar line. The following patch propose a different way:
- Every logic brick now contain its logic manager to SCA_ILogicBrick::m_logicManager, this value is
  set and get by SCA_ILogicBrick::[Set/Get]LogicManager, It's initialized from blender conversion and
  scene merging.
- Function ConvertPythonTo[GameObject/mesh] now take as first argument the logic manager to find name
  coresponding object or mesh. Only ConvertPythonToCamera doesn't do that because it uses the
  KX_Scene::FindCamera function.

Reviewers: moguri

Differential Revision: https://developer.blender.org/D1913
2016-04-10 23:57:43 +02:00

272 lines
5.3 KiB
C++

/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*
* The Original Code is: all of this file.
*
* Contributor(s): none yet.
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file gameengine/GameLogic/SCA_ILogicBrick.cpp
* \ingroup gamelogic
*/
#include <stddef.h>
#include "SCA_ILogicBrick.h"
#include "EXP_PyObjectPlus.h"
SCA_ILogicBrick::SCA_ILogicBrick(SCA_IObject* gameobj)
:
CValue(),
m_gameobj(gameobj),
m_logicManager(NULL),
m_Execute_Priority(0),
m_Execute_Ueber_Priority(0),
m_bActive(false),
m_eventval(0)
{
m_text = "KX_LogicBrick";
}
SCA_ILogicBrick::~SCA_ILogicBrick()
{
RemoveEvent();
}
void SCA_ILogicBrick::SetExecutePriority(int execute_Priority)
{
m_Execute_Priority = execute_Priority;
}
void SCA_ILogicBrick::SetUeberExecutePriority(int execute_Priority)
{
m_Execute_Ueber_Priority = execute_Priority;
}
void SCA_ILogicBrick::ReParent(SCA_IObject* parent)
{
m_gameobj = parent;
}
void SCA_ILogicBrick::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
{
// nothing to do
}
CValue* SCA_ILogicBrick::Calc(VALUE_OPERATOR op, CValue *val)
{
CValue* temp = new CBoolValue(false,"");
CValue* result = temp->Calc(op,val);
temp->Release();
return result;
}
CValue* SCA_ILogicBrick::CalcFinal(VALUE_DATA_TYPE dtype,
VALUE_OPERATOR op,
CValue *val)
{
// same as bool implementation, so...
CValue* temp = new CBoolValue(false,"");
CValue* result = temp->CalcFinal(dtype,op,val);
temp->Release();
return result;
}
const STR_String& SCA_ILogicBrick::GetText()
{
if (m_name.Length())
return m_name;
return m_text;
}
double SCA_ILogicBrick::GetNumber()
{
return -1;
}
STR_String& SCA_ILogicBrick::GetName()
{
return m_name;
}
void SCA_ILogicBrick::SetName(const char *name)
{
m_name = name;
}
bool SCA_ILogicBrick::LessComparedTo(SCA_ILogicBrick* other)
{
return (this->m_Execute_Ueber_Priority < other->m_Execute_Ueber_Priority)
|| ((this->m_Execute_Ueber_Priority == other->m_Execute_Ueber_Priority) &&
(this->m_Execute_Priority < other->m_Execute_Priority));
}
void SCA_ILogicBrick::SetLogicManager(SCA_LogicManager *logicmgr)
{
m_logicManager = logicmgr;
}
SCA_LogicManager *SCA_ILogicBrick::GetLogicManager()
{
return m_logicManager;
}
void SCA_ILogicBrick::RegisterEvent(CValue* eventval)
{
if (m_eventval)
m_eventval->Release();
m_eventval = eventval->AddRef();
}
void SCA_ILogicBrick::RemoveEvent()
{
if (m_eventval)
{
m_eventval->Release();
m_eventval = NULL;
}
}
CValue* SCA_ILogicBrick::GetEvent()
{
if (m_eventval)
{
return m_eventval->AddRef();
}
return NULL;
}
#ifdef WITH_PYTHON
/* python stuff */
PyTypeObject SCA_ILogicBrick::Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"SCA_ILogicBrick",
sizeof(PyObjectPlus_Proxy),
0,
py_base_dealloc,
0,
0,
0,
0,
py_base_repr,
0,0,0,0,0,0,0,0,0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
0,0,0,0,0,0,0,
Methods,
0,
0,
&CValue::Type,
0,0,0,0,0,0,
py_base_new
};
PyMethodDef SCA_ILogicBrick::Methods[] = {
{NULL,NULL} //Sentinel
};
PyAttributeDef SCA_ILogicBrick::Attributes[] = {
KX_PYATTRIBUTE_RO_FUNCTION("owner", SCA_ILogicBrick, pyattr_get_owner),
KX_PYATTRIBUTE_INT_RW("executePriority",0,100000,false,SCA_ILogicBrick,m_Execute_Priority),
KX_PYATTRIBUTE_STRING_RO("name", SCA_ILogicBrick, m_name),
{NULL} //Sentinel
};
int SCA_ILogicBrick::CheckProperty(void *self, const PyAttributeDef *attrdef)
{
if (attrdef->m_type != KX_PYATTRIBUTE_TYPE_STRING || attrdef->m_length != 1) {
PyErr_SetString(PyExc_AttributeError, "inconsistent check function for attribute type, report to blender.org");
return 1;
}
SCA_ILogicBrick* brick = reinterpret_cast<SCA_ILogicBrick*>(self);
STR_String* var = reinterpret_cast<STR_String*>((char*)self+attrdef->m_offset);
CValue* prop = brick->GetParent()->FindIdentifier(*var);
bool error = prop->IsError();
prop->Release();
if (error) {
PyErr_SetString(PyExc_ValueError, "string does not correspond to a property");
return 1;
}
return 0;
}
/*Attribute functions */
PyObject *SCA_ILogicBrick::pyattr_get_owner(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
SCA_ILogicBrick* self = static_cast<SCA_ILogicBrick*>(self_v);
CValue* parent = self->GetParent();
if (parent)
return parent->GetProxy();
Py_RETURN_NONE;
}
/* Conversions for making life better. */
bool SCA_ILogicBrick::PyArgToBool(int boolArg)
{
if (boolArg) {
return true;
} else {
return false;
}
}
PyObject *SCA_ILogicBrick::BoolToPyArg(bool boolarg)
{
return PyLong_FromLong(boolarg ? KX_TRUE: KX_FALSE);
}
#endif // WITH_PYTHON