Few more problems

Started by
14 comments, last by WitchLord 15 years, 3 months ago
Hello. It's me again :). Now that I've cleared up the earlier problems thanks to WitchLord's wonderful help, I have a few more problems. Two of them may be linked, but I'm not sure. One of the class methods I registered causes things to crash right away. I'm not sure why. It's a function that works when used by the class itself external to scripting. It is passed a std::string. Another function will execute, but the string that is supposed to be passed to it from the script doesn't come through. It gets displayed as a square block. After this function is called, I get an immediate crash. If I take this call out and the script is basically empty, no crash occurs. It just calls MessageBoxA This may be something I'm doing. I'm not sure.
Advertisement
Usually these kind of crashes happen when you're not registering the types or functions correctly. When you're registering the functions you're telling AngelScript how the application works with these functions. If you get something wrong in the registration then AngelScript will call the function incorrectly, which can lead to memory violations and stack corruptions, eventually crashing the application.

Can you show us how you're registering this class method and it's implementation? Also how are you registering the string type? Are you using the CScriptString add-on or are you registering your own string type?

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 WitchLord
Usually these kind of crashes happen when you're not registering the types or functions correctly. When you're registering the functions you're telling AngelScript how the application works with these functions. If you get something wrong in the registration then AngelScript will call the function incorrectly, which can lead to memory violations and stack corruptions, eventually crashing the application.

Can you show us how you're registering this class method and it's implementation? Also how are you registering the string type? Are you using the CScriptString add-on or are you registering your own string type?


I am using the addon.

How I am registering them is the same way you saw in my registration problem post.

I might have left one out, but the one crashing right off the bat is "FindText"

r = engine->RegisterObjectMethod("CEngine", "void FindText(string &in)", asMETHOD(CEngine, FindText), asCALL_THISCALL); assert( r >= 0 );


Both are the same signature.

ShowMessage(std::string)
FindText(std::string)
The problem is that you're registering the function as taking a string by reference, but in truth it takes a string by value.

Unfortunately since you use the add-on you won't be able to register this function as-is, since the add-on is a reference type which cannot be passed by value to application functions. You have three options:

1. Change your function to take a std::string by reference.
2. Write a wrapper function that takes a std::string by reference and calls the real function.
3. Stop using the add-on and instead register a string type as a value type.

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

Ok. That makes sense. Thanks. I'll try that :).
Is there a chart that states what corresponds to what C++ types are in Angelscript? I'm trying to register a function with ints this time and it's crashing again. I see the chart in the manual, but I tried what was there and I still crash.
Here are all the types in AngelScript and their corresponding types in C++: Manual: Datatypes in AngelScript and C++

Primitive types usually don't cause this kind of crashes. You might get an ESP error if you have the wrong number of arguments, or if the return type is registered incorrectly.

Show me the function you're trying to register, and how you're registering it and I'll tell you what's wrong.

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

r = engine->RegisterObjectMethod("CEngine", "void BuildButton(string &in, int, int, int, int, string &in)", asMETHOD(CEngine, BuildButton), asCALL_THISCALL); assert( r >= 0 );


void CEngine::BuildButton(string& caption, int x, int y, int width, int height, string& function){	CButton *button = new CButton();	button->id = rand();	button->function = function;	button->window = CreateWindowExA(0, "BUTTON", caption.c_str(), BS_PUSHBUTTON | WS_VISIBLE | WS_CHILD, x, y, width, height, window, (HMENU)button->id, NULL, NULL);	bList.push_back(button);}


I also put a MessageBox call in there to easily trace whether or not it even got called. The box never showed, so I assume it doesn't actually get into the function.
I don't see anything wrong with this. I need more information to discover the cause for the crash.

How is this function called from the script? And how is the CEngine type registered with the engine?

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 WitchLord
I don't see anything wrong with this. I need more information to discover the cause for the crash.

How is this function called from the script?


blah.BuildButton("Test", 0, 0, 100, 100, "test");


Quote:And how is the CEngine type registered with the engine?


HWND g_Window;static void Constructor(void *memory){	new(memory) CEngine(g_Window);}static void Destructor(CEngine *p){	p->~CEngine();}CLoad::CLoad(HWND win){	ostringstream errorMessage;	engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);	int r = engine->SetMessageCallback(asMETHOD(CLoad, GetScriptError), this, asCALL_THISCALL); assert( r >= 0 );	RegisterScriptString(engine);	r = engine->RegisterObjectType("CEngine", sizeof(CEngine), asOBJ_VALUE | asOBJ_APP_CLASS_CDA); assert( r >= 0 );	r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(Constructor), asCALL_CDECL_OBJLAST); assert(r >= 0);		r = engine->RegisterObjectBehaviour("CEngine", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(Destructor), asCALL_CDECL_OBJLAST); assert(r >= 0);	r = engine->RegisterObjectMethod("CEngine", "void ShowMessage(string &in)", asMETHOD(CEngine, ShowMessage), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod("CEngine", "void FindText(string &in)", asMETHOD(CEngine, FindText), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod("CEngine", "void BuildButton(string &in, int, int, int, int, string &in)", asMETHOD(CEngine, BuildButton), asCALL_THISCALL); assert( r >= 0 );	r = engine->RegisterObjectMethod("CEngine", "void LoadExtension(string &in)", asMETHOD(CEngine, Extension), asCALL_THISCALL); assert( r >= 0 );g_Window = win;

This topic is closed to new replies.

Advertisement