Sign in to follow this  
bigfatsumo

String factories

Recommended Posts

bigfatsumo    133
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?

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
also, the RegisterBSTr() function made the script not compile with an error code -17

Share this post


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

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
That is outstanding! I was upgrading from a 1.5 WIP or something. You are doing an awesome job on angescript.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this