Jump to content
  • Advertisement
Sign in to follow this  
luizribeiro

Singleton Script Engine Class

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello... I'm working in a singleton class that will handle the angelscript engine (asIScriptEngine), so I can call the script engine (CScript or something like that) and create my own script loading functions (and libraries, like math with functions like sin, cos, tan, rand, etc) simply using:
CScript *se = &CScript::Instance();
se->LoadSectionFile("module", "file.as");
se->RegisterGlobalFunction(.......)
se->BuildModule("module");

I already have the following code for the class:
class CScript {		
	public:
		CScript &Instance();
	
		// Own functions
		int LoadSectionFile(const char *, const char *);
		int RegisterMathLib(void);
		
	private:
		CScript();
		~CScript();

		asIScriptEngine *m_pAsEngine;
};

CScript &CScript::Instance() {
	static CScript instance;
	return instance;
}

CScript::CScript() {
	m_pAsEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
}

CScript::~CScript() {
	if(m_pAsEngine) {
		m_pAsEngine->Release();
		m_pAsEngine = NULL;
	}
}

Obviously, this thing doesn't work, but it is what I'm trying to do. But, how do I "export" all the asIScriptEngine methods to CScript so I can access them via the CScript object? Thanks, Luiz Ribeiro

Share this post


Link to post
Share on other sites
Advertisement
Why not just drop an asIScriptEngine pointer into a singleton controller?


asIScriptEngine* AngelScript()
{
static asIScriptEngine* engine = asCreateEngine(...);
return engine;
}

AngelScript()->ScriptEngineMemberFunction(...);

Share this post


Link to post
Share on other sites
I want to use a class so I can have my own functions like: LoadFileSection(const char *module, const char *fn), that loads a file (fn) and AddSection it on the module specified...

And I want to have some simple functions on the class that register global functions to the engine like cos() sin() tan(), etc.

Share this post


Link to post
Share on other sites
I got it working with the overload thing described in this topic:
[link]http://www.gamedev.net/community/forums/topic.asp?topic_id=328633[/link]

If anyone's interested, the class definition is now:
class CScript {
public:
static CScript &Instance();

// Own functions
int LoadSectionFile(const char *, const char *);
int RegisterMathLib(void);

// Overloaded functions
/*void SetEngine(asIScriptEngine *in_pAsIScriptEngine) {m_pAsEngine=in_pAsIScriptEngine;};
asIScriptEngine *GetEngine() {return m_pAsEngine;};*/


virtual int AddRef() {return m_pAsEngine->AddRef();};
virtual int Release() {return m_pAsEngine->Release();};

virtual int RegisterObjectType(const char *name, int byteSize, asDWORD flags) {return m_pAsEngine->RegisterObjectType(name, byteSize, flags);};
virtual int RegisterObjectProperty(const char *obj, const char *declaration, int byteOffset) {return m_pAsEngine->RegisterObjectProperty(obj, declaration, byteOffset);};
virtual int RegisterObjectMethod(const char *obj, const char *declaration, asUPtr funcPointer, asDWORD callConv) {return m_pAsEngine->RegisterObjectMethod(obj, declaration, funcPointer, callConv);};
virtual int RegisterObjectBehaviour(const char *datatype, asDWORD behaviour, const char *declaration, asUPtr funcPointer, asDWORD callConv) {return m_pAsEngine->RegisterObjectBehaviour(datatype, behaviour, declaration, funcPointer, callConv);};

virtual int RegisterGlobalProperty(const char *declaration, void *pointer) {return m_pAsEngine->RegisterGlobalProperty(declaration, pointer);};
virtual int RegisterGlobalFunction(const char *declaration, asUPtr funcPointer, asDWORD callConv) {return m_pAsEngine->RegisterGlobalFunction(declaration, funcPointer, callConv);};

virtual int RegisterStringFactory(const char *datatype, asUPtr factoryFunc, asDWORD callConv) {return m_pAsEngine->RegisterStringFactory(datatype, factoryFunc, callConv);};

virtual int AddScriptSection(const char *module, const char *name, const char *code, int codeLength, int lineOffset) {return m_pAsEngine->AddScriptSection(module, name, code, codeLength, lineOffset);};
virtual int Build(const char *module, asIOutputStream *out) {return m_pAsEngine->Build(module, out);};
virtual int Discard(const char *module) {return m_pAsEngine->Discard(module);};
//virtual int GetModuleID(const char *module) {return m_pAsEngine->GetModuleID(module);};
virtual int GetModuleIndex(const char *module) {return m_pAsEngine->GetModuleIndex(module);};
virtual const char *GetModuleNameFromIndex(int index, int *length = 0) {return m_pAsEngine->GetModuleNameFromIndex(index, length);};

virtual int GetFunctionCount(const char *module) {return m_pAsEngine->GetFunctionCount(module);};
virtual int GetFunctionIDByIndex(const char *module, int index) {return m_pAsEngine->GetFunctionIDByIndex(module, index);};
virtual int GetFunctionIDByName(const char *module, const char *name) {return m_pAsEngine->GetFunctionIDByName(module, name);};
virtual int GetFunctionIDByDecl(const char *module, const char *decl) {return m_pAsEngine->GetFunctionIDByDecl(module, decl);};
virtual const char *GetFunctionDeclaration(int funcID, int *length = 0) {return m_pAsEngine->GetFunctionDeclaration(funcID, length);};
virtual const char *GetFunctionName(int funcID, int *length = 0) {return m_pAsEngine->GetFunctionName(funcID, length);};
#ifdef AS_DEPRECATED
virtual int GetFunctionDeclaration(int funcID, char *buffer, int bufferSize) {return m_pAsEngine->GetFunctionDeclaration(funcID, buffer, bufferSize);};
virtual int GetFunctionName(int funcID, char *buffer, int bufferSize) {return m_pAsEngine->GetFunctionName(funcID, buffer, bufferSize);};
#endif

virtual int GetGlobalVarCount(const char *module) {return m_pAsEngine->GetFunctionCount(module);};
virtual int GetGlobalVarIDByIndex(const char *module, int index) {return m_pAsEngine->GetGlobalVarIDByIndex(module, index);};
virtual int GetGlobalVarIDByName(const char *module, const char *name) {return m_pAsEngine->GetGlobalVarIDByName(module, name);};
virtual int GetGlobalVarIDByDecl(const char *module, const char *decl) {return m_pAsEngine->GetGlobalVarIDByDecl(module, decl);};
virtual const char *GetGlobalVarDeclaration(int gvarID, int *length = 0) {return m_pAsEngine->GetGlobalVarDeclaration(gvarID, length);};
virtual const char *GetGlobalVarName(int gvarID, int *length = 0) {return m_pAsEngine->GetGlobalVarName(gvarID, length);};
#ifdef AS_DEPRECATED
virtual int GetGlobalVarDeclaration(int gvarID, char *buffer, int bufferSize) {return m_pAsEngine->GetGlobalVarDeclaration(gvarID, buffer, bufferSize);};
virtual int GetGlobalVarName(int gvarID, char *buffer, int bufferSize) {return m_pAsEngine->GetGlobalVarName(gvarID, buffer, bufferSize);};
#endif
virtual void *GetGlobalVarPointer(int gvarID) {return m_pAsEngine->GetGlobalVarPointer(gvarID);};

virtual int SetDefaultContextStackSize(asUINT initial, asUINT maximum) {return m_pAsEngine->SetDefaultContextStackSize(initial, maximum);};
virtual asIScriptContext *CreateContext() {return m_pAsEngine->CreateContext();};

virtual int ExecuteString(const char *module, const char *script, asIOutputStream *out = 0, asIScriptContext **ctx = 0, asDWORD flags = 0) {return m_pAsEngine->ExecuteString(module, script, out, ctx, flags);};
#ifdef AS_DEPRECATED
virtual int ExecuteString(const char *module, const char *script, asIOutputStream *out, asDWORD flags) {return m_pAsEngine->ExecuteString(module, script, out, flags);};
virtual asIScriptContext *GetContextForExecuteString() {return m_pAsEngine->GetContextForExecuteString();};
#endif

virtual int SaveByteCode(const char *module, asIBinaryStream *out) {return m_pAsEngine->SaveByteCode(module, out);};
virtual int LoadByteCode(const char *module, asIBinaryStream *in) {return m_pAsEngine->LoadByteCode(module, in);};

// 1.9.0 WIP4
// Dynamic binding between modules
virtual int GetImportedFunctionCount(const char *module) {return m_pAsEngine->GetImportedFunctionCount(module);};
virtual int GetImportedFunctionIndexByDecl(const char *module, const char *decl) {return m_pAsEngine->GetImportedFunctionIndexByDecl(module, decl);};
virtual const char *GetImportedFunctionDeclaration(const char *module, int importIndex, int *length = 0) {return m_pAsEngine->GetImportedFunctionDeclaration(module, importIndex, length);};
#ifdef AS_DEPRECATED
virtual int GetImportedFunctionDeclaration(const char *module, int importIndex, char *buffer, int bufferSize) {return m_pAsEngine->GetImportedFunctionDeclaration(module, importIndex, buffer, bufferSize);};
#endif
virtual const char *GetImportedFunctionSourceModule(const char *module, int importIndex, int *length = 0) {return m_pAsEngine->GetImportedFunctionSourceModule(module, importIndex, length);};
virtual int BindImportedFunction(const char *module, int importIndex, int funcID) {return m_pAsEngine->BindImportedFunction(module, importIndex, funcID);};
virtual int UnbindImportedFunction(const char *module, int importIndex) {return m_pAsEngine->UnbindImportedFunction(module, importIndex);};

virtual int BindAllImportedFunctions(const char *module) {return m_pAsEngine->BindAllImportedFunctions(module);};
virtual int UnbindAllImportedFunctions(const char *module) {return m_pAsEngine->UnbindAllImportedFunctions(module);};
//

// 1.10.1
virtual int RegisterGlobalBehaviour(asDWORD behaviour, const char *declaration, asUPtr funcPointer, asDWORD callConv) {return m_pAsEngine->RegisterGlobalBehaviour(behaviour, declaration, funcPointer, callConv);};


private:
CScript();
virtual ~CScript();

asIScriptEngine *m_pAsEngine;
};


And if someone knows a less polluted way of doing this, I'll be happy to hear your solution ;)

Luiz Ribeiro

Share this post


Link to post
Share on other sites
In this case, that seems the best way...

You can also think about composition..

class MyScript
{

public:
asScriptEngine *MyEngine;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
You could probably just inherit from the implementation of asIScriptEngine.

I'd like to know how you can do this.
Can you drop a sample, please.

AbrKen.



Share this post


Link to post
Share on other sites
The asIScriptEngine is already a polymorphic class. What you see in angelscript.h is just a declaration of the abstract base class. You will have to dig into the library implementation, find the actuall definition that asCreateScriptEngine returns, and derive a new type from it. Then change asCreateScriptEngine to return an instance of it instead.

Rain Dog : That's not any different than what he's already got. He's trying to add new functions without having to write wrappers for all the others.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!