C++ã¤ã³ã¹ã¿ã³ã¹é¢æ°ãå¼ã³åºãSquirrelã¤ã³ã¹ã¿ã³ã¹é¢æ°ï¼ãã¤ãã§ã¤ããªãããªï¼
SQPlusã¯C++ã®ã¤ã³ã¹ã¿ã³ã¹ãinstanceupã¸ãé¢æ°ãã¤ã³ã¿ãfunctionã®free variableã¸ããã¦ããfree variableåã¯ãããªããªããï¼ã¨æã£ãã®ã§ãèªåã¯ãããªãµãã«æ¸ããããã³ãã¬ã¼ãã£ã¦ãã³ãèéãããªãã
inline static void mysq_register_function(HSQUIRRELVM v, const char* name, SQFUNCTION func, int numparams, const char* paramtype, SQBool bstatic = SQFalse) { sq_pushstring(v,name,-1); sq_newclosure(v,func,0); if(paramtype) sq_setparamscheck(v,numparams,paramtype); sq_setnativeclosurename(v,-1,name); sq_newslot(v,-3,bstatic); } template <typename T, int(T::*FUNC)(HSQUIRRELVM)> static int _mysq_instance_nativeclosure(HSQUIRRELVM v) { T* g = (T*)_instance(stack_get(v,1))->_userpointer; return (g->*FUNC)(v); } #define MYSQ_REG_INSTANCE_LINKED_FUNCTION(VM,CLASS,FUNCNAME,PNUM,FORMAT) \ mysq_register_function(VM,#FUNCNAME,_mysq_instance_nativeclosure<CLASS,&CLASS::FUNCNAME>,PNUM,FORMAT)
ã§ããããªé¢¨ã«ä½¿ããã
SQObjectPtr sqclass; _table(v->_roottable)->Get(SQObjectPtr(SQString::Create(_ss(v),"Agent")),sqclass); sq_pushobject(v,sqclass); MYSQ_REG_INSTANCE_LINKED_FUNCTION(v,MobBotWithAI,WalkTo, 2, ".x"); MYSQ_REG_INSTANCE_LINKED_FUNCTION(v,MobBotWithAI,Attack, 0, ".ix"); MYSQ_REG_INSTANCE_LINKED_FUNCTION(v,MobBotWithAI,Track, 2, ".x"); sq_poptop(v);
Squirrelã®ã¤ã³ã¹ã¿ã³ã¹ãã¤ããã¨ãã¯ç´ä»ããC++ã®ã¤ã³ã¹ã¿ã³ã¹ãsq_setinstanceupãããã¨ãå¿ããã«
mysq_call(v,true,-1,"Agent"); m_sqagent = stack_get(v,-1); // C++ -> Squirrel ã®åç § _instance(m_sqagent)->_userpointer = this; // C++ <- Squirrel ã®åç §
æè¿ã¯ã§ããã ãã¹ã¿ãã¯ã使ããªãã®ããã¤ãã¼ã ã