Jump to content
  • Advertisement
Sign in to follow this  
Cosmy

Help :(

This topic is 3212 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

I have this code:
void as_();

void ConfigureEngine(asIScriptEngine *pEngine)
{
	int r;

	if(!strstr(asGetLibraryOptions(), "AS_MAX_PORTABILITY"))
	{
		r = pEngine->RegisterGlobalFunction("void print(string &in)", asFUNCTION(as_print), asCALL_STDCALL); assert(r >= 0);
		r = pEngine->RegisterGlobalFunction("void a()", asFUNCTION(as_), asCALL_CDECL); assert(r >= 0);
	}
	else
	{
	}
}

void MessageCallback(const asSMessageInfo *msg, void *param)
{
	const char *type = "ERROR";
	if(msg->type == asMSGTYPE_WARNING) 
		type = "WARNING";
	else if(msg->type == asMSGTYPE_INFORMATION) 
		type = "INFO";

	printf("%s (%d, %d): %s", type, msg->row, msg->col, msg->message);
}

int CompileScript(char *pcFileName, char *pcModule, asIScriptEngine *pEngine)
{
	int r;
	FILE *pFile = NULL;

	fopen_s(&pFile, pcFileName, "rb");
	if (pFile == NULL)
	{
		printf("ANGELSCRIPT: Failed to open '%s'!", pcFileName);
		return ANGELSCRIPT_ERROR;
	}

	fseek(pFile, 0, SEEK_END);
	int len = ftell(pFile);
	fseek(pFile, 0, SEEK_SET);

	string script;
	script.resize(len);
	int c =	fread(&script[0], len, 1, pFile);
	fclose(pFile);

	if(c == 0) 
	{
		printf("ANGELSCRIPT: Failed to load '%s'!", pcFileName);
		return ANGELSCRIPT_ERROR;
	}

	asIScriptModule *mod = pEngine->GetModule(0, asGM_ALWAYS_CREATE);
	r = mod->AddScriptSection(pcModule, &script[0], len);
	if(r < 0) 
	{
		printf("ANGELSCRIPT: Failed to add script section in file '%s'!", pcFileName);
		return ANGELSCRIPT_ERROR;
	}

	r = mod->Build();
	if(r < 0)
	{
		printf("ANGELSCRIPT: Failed to build '%s'!", pcFileName);
		return ANGELSCRIPT_ERROR;
	}
	return ANGELSCRIPT_OK;
}

int RunApplication(char *pcFileName)
{
	int r;

	asIScriptEngine *pEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
	if(pEngine == 0)
	{
		printf("ANGELSCRIPT: Failed to create engine!");
		return ANGELSCRIPT_ERROR;
	}

	RegisterScriptString(pEngine);

	ConfigureEngine(pEngine);

	pEngine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);

	r = CompileScript(pcFileName, NULL, pEngine);
	if(r == ANGELSCRIPT_ERROR)
	{
		pEngine->Release();
		return ANGELSCRIPT_ERROR;
	}



	asIScriptContext *ctx = pEngine->CreateContext();
	if(ctx == 0) 
	{
		printf("ANGELSCRIPT: Failed to create context!");
		pEngine->Release();
		return ANGELSCRIPT_ERROR;
	}

	ctx->Release();
	pEngine->Release();
	return ANGELSCRIPT_OK;
}

void as_()
{
	printf("lol");
}
And this is my script.as
void main()
{
	a();
}
But don't print anything :( I need to declare "void main()"?

Share this post


Link to post
Share on other sites
Advertisement
You're implementation of "a()" is what?

did you mean to call as_()?

Please post only relevant code which gets called by your test app.

Share this post


Link to post
Share on other sites
Quote:
Original post by Martin
You're implementation of "a()" is what?

did you mean to call as_()?

Please post only relevant code which gets called by your test app.

Quote:
Engine->RegisterGlobalFunction("void a()", asFUNCTION(as_), asCALL_CDECL);
?

Share this post


Link to post
Share on other sites
You create a context but never do anything with it. You need to call Prepare() with the id of the script function you want to run and then call Execute() to actually run your script.

Share this post


Link to post
Share on other sites
Solved!



r = ctx->Prepare(pEngine->GetModule(0)->GetFunctionIdByDecl("void main()"));
if(r < 0)
{
logprintf("ANGELSCRIPT: main function not found in script '%s'!", pcFileName);
return ANGELSCRIPT_ERROR;
}
r = ctx->Execute();


I declared main function and now work :)

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!