Sign in to follow this  

Strings, passing to native functions and converting from math types

This topic is 4729 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

Hi, I'm having some problems with strings. I'm using this code:
void print_str(std::string msg)
{
//	std::cout << msg << std::endl;
	printf("%s\n", msg.c_str());
}

void print_int(int i)
{
//	std::cout << i << std::endl;
	printf("%d\n", i);
}

const char * script = 
"								"
"	void do_test()					"
"	{							"
"		int i = 42;					"
"		print_int(i);					"
"								"
"		string msg = \"Hello, World!\";		"
"		print_str(msg);				"
"	}							"
"								";

int main()
{
 	asIScriptEngine * engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);

	RegisterStdString(engine);

	engine->RegisterGlobalFunction("void print_str(string msg)", asFUNCTION(print_str), asCALL_CDECL);
	engine->RegisterGlobalFunction("void print_int(int i)", asFUNCTION(print_int), asCALL_CDECL);

	OutStream out;
	engine->AddScriptSection(0, "name", script, (int)strlen(script), 0);
	engine->Build(0, &out);

	asIScriptContext *ctx;
	engine->CreateContext(&ctx);
	ctx->Prepare(engine->GetFunctionIDByDecl(0, "void do_test()"));

	int r = ctx->Execute();
	
	if (r != 0)
	{
		if (r == asEXECUTION_EXCEPTION)
		{
			printf("Script exception\n");
			printf("Func: %s\n", engine->GetFunctionName(ctx->GetExceptionFunction()));
			printf("Line: %d\n", ctx->GetExceptionLineNumber());
			printf("Desc: %s\n", ctx->GetExceptionString());
		}
	}

	return 0;
}



Which prints the int as expected, but doesn't print the string. I put a breakpoint in print_str and msg is empty. Any ideas? Secondly, I want to be able to convert between the math types and strings in script as if it were a typeless language. I think to do this I just need to register asBEHAVE_ASSIGNMENT functions for "string &f(int &in)" (and other types as needed), and then implement the conversion function as something like:
string IntToString(int i)
{
	stringstream stream;
	stream << i;

	string str;
	stream >> str;

	return str;
}



Am I on the right track? Have I missed some existing functionality that does this automatically or provides a simpler way? Alan

Share this post


Link to post
Share on other sites
You said the string is empty in print_str - have you checked how you are passing the string from AS to C++?

Your conversion functions is very near the cannocal lexical_cast:

template <typename R, typename T> R lexical_cast(const T& in)
{
stringstream str;
str << in;
R ret;
str >> ret;
return ret;
}

Share this post


Link to post
Share on other sites
I;m not sure about the problem with the strings in AS, everything looks normal, but your conversion routine could be simplified a bit:


std::string IntToString(int i)
{
std::istringstream stream;
stream << i;

return stream.str();
}

Share this post


Link to post
Share on other sites
Quote:

You said the string is empty in print_str - have you checked how you are passing the string from AS to C++?


I'm not exactly sure what you mean? I'm calling print_str from my as script. I put a break point into the stdstring StringFactory function, and at some point the correct string is being created. I tried changing print_str to take a reference to a string, but now I get an "Invalid configuration" error message.

As for the conversion routines, I'm pretty sure I can manage to turn an int into a string :-) My question was more todo with how to write the AS interface code, ie can I just provide an asBEHAVE_ASSIGNMENT function for strings that takes a paramter of an int and have AS to do the right thing? I would just try it, but I cant print strings to see if its working...

Alan

Share this post


Link to post
Share on other sites
That should work. It's not like AS could handle the template anyhow.

Have you registered all of the strings operators properly?
Are you using the stable 1.10 version or the new 2.0? AFAIK; changes were made to how parameters were passed to C++ for 2.0.

Share this post


Link to post
Share on other sites
Let me answer your second question first. Yes, you can register the asBEHAVE_ASSIGNMENT with different types to have the script allow automatic conversion of types to strings. You can also register asBEHAVE_ADD to allow conversion when doing concatenations as well.

Now for the first question, that is more worrying to me. There is no apparent error in your code, that leads me to believe it might be a problem with AngelScript itself. What version of AngelScript are you using, and what C++ compiler are you using? As a possible work around you might want to change the print_str() function to take a reference to the string instead of a copy of the string.


void print_str(std::string &msg)
{
// std::cout << msg << std::endl;
printf("%s\n", msg.c_str());
}

engine->RegisterGlobalFunction("void print_str(string &in)", asFUNCTION(print_str), asCALL_CDECL);


Share this post


Link to post
Share on other sites
Thanks for the reply. I'm using Visual Studio .NET 2003, and AngelScript 2.0.0 WIP1.

To try and figure out where things were going wrong I put print statements into StringFactory, ConstructString, DestructString and print_str, and found something rather strange. (c++ style comments are my annotations).


ConstructString() // the string gets created
StringFactory("Hello, World!") // and filled with its contents
DestructString() // the string gets destroyed...
ConstructString() // a new string gets created
print_str() // called with the new, empty string
(null) // this is the printf trying to output the contents of the string
DestructString() // at least it doesn't leak the memory :-)



I tried the changes to calling print_str with a reference, but it now won't even Build. It just says "Invalid Configuration".

Alan

Share this post


Link to post
Share on other sites
Seems like a bug in AS 2.0 WIP 1. I'll try to verify that as soon as possible.

Did you register the print_str() function exactly as I wrote it? with the 'string &in' parameter? the 'in' keyword is necessary to tell the library how the reference is used.

It will be a while yet before AS 2.0 will be stable, so if you wish to work with something stable and bug free I suggest you try AS 1.10.1c instead. If you don't mind the bugs in AS 2.0 you're welcome to use it of course, it will help me find the bugs that are likely to be there.

Share this post


Link to post
Share on other sites
Quote:

Did you register the print_str() function exactly as I wrote it? with the 'string &in' parameter? the 'in' keyword is necessary to tell the library how the reference is used.


No I hadn't. Changing that and it works! Thanks.

In an attempt to track down the problem I grabbed a copy of 1.10.1c, which worked with the original (non-reference) version. I enabled AS_DEBUG and compared the byte code generated by both versions, and they were identical. That means the bug has to be in the interpreter, but debugging into that is a little beyond my familiarity with AS at the moment.

As a sidenote, it would make checking bytecode easier if the reference actually contained all the instructions :-)

Alan

Share this post


Link to post
Share on other sites
It's good to hear that at least the reference version works. I'm sure I'll be able to find out pretty soon why the non-reference version fails. I'll be back tomorrow with more info on that (hopefully).

The bytecode reference actually do contain all the instructions, though you have to read the right version. The one on the site is for version 1.10.1c and the one for 2.0.0 WIP 1 can be found in the zip.

If the bytecode was identical for both versions then something is terribly wrong. AngelScript has changed substantially in the way it handles objects (strings included) that it should definitely be different from 1.10.1c.

But don't worry, with your sample code I'm sure it will be easy (for me) to find the bug.

Share this post


Link to post
Share on other sites
Just wanted to let you know that I've found and fixed the bug.

I've completed WIP 2, which will contain the bug fix as well as support for object handles. I will hopefully be able to upload it to the site tonight.

Share this post


Link to post
Share on other sites
Quote:

If the bytecode was identical for both versions then something is terribly wrong. AngelScript has changed substantially in the way it handles objects (strings included) that it should definitely be different from 1.10.1c.


Of course this was just me being an idiot and comparing the same file to itself...

Quote:

Just wanted to let you know that I've found and fixed the bug.

I've completed WIP 2, which will contain the bug fix as well as support for object handles. I will hopefully be able to upload it to the site tonight.


Excellent. I'll update as soon as its posted. I'm still right at the begining of developing my app so I might as well keep up to the bleeding edge version.

Alan

Share this post


Link to post
Share on other sites
That's ok with me [smile]

I plan on making AS 2.0.0 a stable version once it is complete, because it is in my opinion much better than AS 1.10.1. I'll still keep 1.10.1 around though for those who are currently using that version.

Share this post


Link to post
Share on other sites

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

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