String factories

Started by
4 comments, last by GameDev.net 19 years ago
I haven't programmed in a while and a lot of progress has been made on AngelScript. I kicked up a new game and decided to use AS for scripting. I have everything working, but when I try to register the old BSTR string factory I get errors like "RegisterTypeBehaviour" doesn't exist and asCALLRETURNBYVAL or something like that doesn't exist. I am using the newest WIP and have all the files copied in the right dir and my directory setup is right. So I have two questions: What is the best way to EASILY manage string between the host program and AS and How can I get the old BSTR code to work with the newest WIP?
Advertisement
I don't recommend the use of BSTR anymore, because it's too easy to forget allocating or deallocating memory in the application functions.

Instead I recommend you use the std::string. Code for registering the std::string with the library is available in the SDK download in the folder add_on/std_string.

If you still want to use bstr (for backwards compatibility for example), then you can find the code for registering that in the folder test_feature/source (the files bstr.h and bstr.cpp).

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

thanks, but do you have any examples of using the std::string code? One for registering a function, a global string variable, and one inside the actual script? I looked at the string example code, and it looked like a lot more work than using bstr.
also, the RegisterBSTr() function made the script not compile with an error code -17
To the script writer there is no difference between bstr and std::string, unless you've registered them with different names.

Using std::string, your application functions would use the string type normally, just as if you were receiving it from another application function. This is makes it much easier to avoid potential bugs and memory leaks.

Using bstr the application function needs to use the functions bstrAlloc(), and bstrFree() to manually allocate and free the memory used by the strings passed to and from the scripts. If you are used to COM components this is exactly the way the COM strings work, but with SysAlloc() and SysFree() instead.

The teststdstring.cpp code shows that std::string really works as intended. testbstr.cpp also shows that bstr works as intended.

If you receive an error code of -17 with the call to Build() it means that one of the Register...() functions failed. Verify each of the return codes from the Register...() calls to pinpoint the exact error. I recommend doing this with an assert( r >= 0 ); after each call, where r is the resulting code. (of course the assert() call will only be compiled in debug mode).

I'm guessing you are upgrading to AS 2.x.x from an early AS 1.x.x version. If that is the case, then you may want to take a look at the function declarations that you use, e.g. the * is no longer used to declare pointers in the script (since pointers are no longer directly supported), and & for parameters must be followed by one of in, out, or inout.

There is also a new type modifier @ that declares the type as object handle, which is basically a reference counted pointer. To use @ you must also register the ADDREF and RELEASE behaviours for the 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

That is outstanding! I was upgrading from a 1.5 WIP or something. You are doing an awesome job on angescript.

This topic is closed to new replies.

Advertisement