Jump to content
  • Advertisement
Sign in to follow this  
Maega

Few more problems

This topic is 3533 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. 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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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;


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!