🎉 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!

Weirdness with function pointers

Started by
0 comments, last by WitchLord 10 years, 5 months ago

I'm trying to make an engine similar to how unity does it where I can compile the engine on the other OS's and be able to use scripting to make it work. So my classes I want to look like this.


class TestLevel : AffinityScript {
        void Init() {

        }

	void Start() {
	
	}

	void Update() {
	
	}
}

However when calling the Start method I get a null reference pointer.

Here's some of the code.


    int r;

    asIScriptModule* mod = this->m_pEngine->GetModule(filename.c_str(), asGM_ONLY_IF_EXISTS);

    r = this->m_bBuilder.StartNewModule(this->m_pEngine, filename.c_str()); assert(r >= 0);

    r = this->m_bBuilder.AddSectionFromFile((filename + ".as").c_str()); assert(r >= 0);

    r = this->m_bBuilder.BuildModule(); assert(r >= 0);

    Script* script = new Script();

    script->Module = filename;

    mod = this->m_pEngine->GetModule(filename.c_str(), asGM_ONLY_IF_EXISTS);
    asIObjectType *type = 0;
    int tc = mod->GetObjectTypeCount();
    for(int n = 0; n < tc; n++) {
        bool found = false;
        type = mod->GetObjectTypeByIndex(n);
        int ic = type->GetInterfaceCount();
        for(int i = 0; i < ic; ++i) {
            if(strcmp(type->GetInterface(i)->GetName(), "AffinityScript") == 0) {
                found = true;
                break;
            }
        }
        if(found == true) {
            script->m_pType = type;
            break;
        }
    }

    if(script->m_pType == 0) {
        delete script;
        return;
    }

    script->InitMethod = type->GetMethodByDecl("void Init()");
    script->StartMethod = type->GetMethodByDecl("void Start()");
    script->DrawMethod = type->GetMethodByDecl("void Draw()");
    script->UpdateMethod = type->GetMethodByDecl("void Update()");
    script->ExitMethod = type->GetMethodByDecl("void OnExit()");

    this->m_mScripts[this->m_mScripts.size()] = script;

if(itr->second->StartMethod != 0) {
	asIScriptContext *ctx = PrepareContextFromPool(itr->second->StartMethod);
	ExecuteCall(ctx);
	ReturnContextToPool(ctx);
}

	asIScriptContext *ctx = 0;
	if(this->m_vContexts.size()) {
		ctx = *this->m_vContexts.rbegin();
		this->m_vContexts.pop_back();
	}else{
		ctx = this->m_pEngine->CreateContext();
	}

	int r = ctx->Prepare(func); assert(r >= 0 );

	return ctx;

Of course I'm borrowing some code from the Game sample to get this working until I come up with my own solution but the assert function happens at int r= ctx->Prepare(func);

Edit: I forgot to mention the null reference happens when taking away the assert./

Edit2: Actually it's happening WITH the assert now.

Edit3: I have my program dumping compile errors to a file and here's the contents

Edit4: I did a bit of research on it and the engine wants me to use ctx->SetObject(); but I don't want to attach a script to an object in particular is there a way to call the script without it?


Exception: Null pointer access
Function: 04898AC0
Line: 0

Any ideas?

Advertisement

You're trying to call a class method on a script object, but you're not telling the context which script object it is. That's why you get the 'null pointer acess' exception when calling Execute().

If you don't want to have script objects, then implement the logic as global functions in the script.

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

This topic is closed to new replies.

Advertisement