🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Just getting started, having a bit a trouble.

Started by
4 comments, last by ontheheap 19 years, 4 months ago
I'm trying to work through the semi-tutorial on the angelscript page, but I'm having difficulty. Code speaks louder than words, so I'll post the short code listing now: c++

#include <iostream>
#include <fstream>
#include "angelscript.h"
using namespace std;

const char* LoadScript(std::string scriptfile);

void Test();

int main()
{
    int counter = 0;
    
    asIScriptEngine* engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
	const char* script = LoadScript("test01.as");
	
    engine->AddScriptSection("module", "section", script, strlen(script));
    engine->Build("module");
    	
	asIScriptContext *context = 0;
    engine->CreateContext(&context);
    int functionID = engine->GetFunctionIDByDecl("module", "void DoSomething()");

    engine->RegisterGlobalFunction("void Test()", asFUNCTION(Test), asCALL_CDECL);
    engine->RegisterGlobalProperty("int counter", &counter);

    // Execute the script function
    context->Prepare(functionID);
    context->Execute();

    // Release the context when finished with it
    context->Release();
	
    // test
    cout << counter;
    cin.get();
    return EXIT_SUCCESS;
}

const char* LoadScript(std::string scriptfile)
{
    ifstream fin(scriptfile.c_str());
    char ch;
    std::string script_contents;
    
    while(fin.get(ch))
    {
       script_contents += ch;
    }
    
    fin.close();
    return script_contents.c_str();
}

void Test()
{
    cout << "Function called by script!" << endl;
}


AngelScript

void DoSomething()
{
    counter = 5;
    Test();
}

The only thing printed to the screen is a "0," which means I'm obviously not doing something right. Can anyone help?
Advertisement
I think you're supposed to register the functions/properties before you compile the module.
Yea, that's what the problem was! Thanks Gybro. Now that I have a working script I think following the rest of the tutorial / sample code should be much easier.
Gyrbo:

Thanks Gyrbo for helping out.

ontheheap:

I guess you're reading the overview article. That is only meant to show a bit of what can be done with AngelScript, not to serve as a tutorial. The sample code in that article lacks error checking (to make it easier to understand), which if you had used probably would have told you what was wrong.

Especially when calling Build() it is a good idea to pass a pointer to an output stream, example:

class COutStream : public asIOutputStream{public:	void Write(const char *text) { printf(text); }};


Build the scripts, passing a pointer to the output stream:

COutStream out;int r = engine->Build("module name", &out);if( r < 0 ){  // The build failed, verify the messages passed to the output stream}


Also when registering application functions, types, or properties, always verify that the return code is not negative, which would mean the registration failded. I usually put an assert, after each register function call. That way it is possible to pinpoint in debug mode which register function that failed. In release mode it is not necessary to verify them (the assert call will be removed by the compiler), the Build() method would fail anyway.

r = engine->RegisterGlobalFunction(...); assert( r >= 0 );r = engine->RegisterGlobalProperty(...); assert( r >= 0 );


Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Quote: Original post by ontheheap
Yea, that's what the problem was! Thanks Gybro. Now that I have a working script I think following the rest of the tutorial / sample code should be much easier.


i have the same problems, but i don't understand the solution. could you post your working script here again? thanks
Quote: Original post by Anonymous Poster
i have the same problems, but i don't understand the solution. could you post your working script here again? thanks


Here's my original working script. I haven't modified it for error checking, but it does work. Anyway, hope it helps. And thanks for the reply, WitchLord.

AS:
void DoSomething(){    counter = 5;    Test();}


C++:
#include <iostream>#include <fstream>#include "angelscript.h"using namespace std;const char* LoadScript(std::string scriptfile);void Test();int main(){    int counter = 0;        asIScriptEngine* engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);    const char* script = LoadScript("test01.as");	    engine->AddScriptSection("module", "section", script, strlen(script));    engine->RegisterGlobalProperty("int counter", &counter);    engine->RegisterGlobalFunction("void Test()", asFUNCTION(Test), asCALL_CDECL);        engine->Build("module");    	    asIScriptContext *context = 0;    engine->CreateContext(&context);        int functionID = engine->GetFunctionIDByDecl("module", "void DoSomething()");    // Execute the script function    context->Prepare(functionID);    context->Execute();    // Release the context when finished with it    context->Release();	    // test    cout << counter;    cin.get();    return EXIT_SUCCESS;}const char* LoadScript(std::string scriptfile){    ifstream fin(scriptfile.c_str());    char ch;    std::string script_contents;        while(fin.get(ch))    {       script_contents += ch;    }        fin.close();    return script_contents.c_str();}void Test(){    cout << "Function called by script!" << endl;}

This topic is closed to new replies.

Advertisement