🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Expected identifier error.

Started by
4 comments, last by Hulag 18 years, 4 months ago
I'm having an error when I build a very simple script:
script (1, 7) : Error   : Expected identifier
The text of the script is:
void main()
{
	printOutput("TEST TEST TEST");
}
The printOutput is registered with the following line
scriptEngine->RegisterGlobalFunction("void printOutput(string ∈)", asFUNCTION(printOutput), asCALL_CDECL)
the registration is successful. All the functions until the build function is called are successful, and the script text was uploaded successfuly (also, I follow the code of the tutorial provided). I'm using AngelScript 2.4.1e. Thanks
Advertisement
Ok, I have been working a bit more and I got it to work but I don't understand why it only works in one way.
I use AddScriptSection this way:
scriptEngine->AddScriptSection(0, scriptName, scriptString.c_str(), (int)scriptString.length(), 0, false);

Now, when the scriptString variable is defined as static std::string everything works fine, the script builds fine and there are no problems, my script is executed just fine. But if it isn't static then the script fails to build with the error I posted on my first post.

Why does it work with static std::string and doesn't work with a normal std::string?
Quote: Why does it work with static std::string and doesn't work with a normal std::string?


Because probably the function that loads the script into the string has some bug in it...
Quote: Original post by mandrav
Quote: Why does it work with static std::string and doesn't work with a normal std::string?


Because probably the function that loads the script into the string has some bug in it...

I'm using basically the same function that's provided in the AngelScript tutorial. Also if that was the case then the static std::string version shouldn't work either cause all I'm doing is defining the std::string variable where I load my script as static, the rest of the code remains the same.
It's a bit difficult to say, because you're not showing your code, but it sounds like you're calling AddScriptSection() in one function and Build() in another.

If the std::string is locally declared in the function that calls AddScriptSection() it will go out of scope before the Build() method is called. The memory block that AngelScript is trying to compile as a script may thus contain non-understandable noise.

By changing the declaration of the std::string to static, you're making the string stay alive beyond the scope of the function.

You can also fix this problem by changing the call to AddScriptSection() to tell AngelScript to make a copy of the script buffer, in which case it will not matter if the original string is deallocated or not. The last parameter to AddScriptSection() does this.

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

Quote: Original post by WitchLord
It's a bit difficult to say, because you're not showing your code, but it sounds like you're calling AddScriptSection() in one function and Build() in another.

If the std::string is locally declared in the function that calls AddScriptSection() it will go out of scope before the Build() method is called. The memory block that AngelScript is trying to compile as a script may thus contain non-understandable noise.

By changing the declaration of the std::string to static, you're making the string stay alive beyond the scope of the function.

You can also fix this problem by changing the call to AddScriptSection() to tell AngelScript to make a copy of the script buffer, in which case it will not matter if the original string is deallocated or not. The last parameter to AddScriptSection() does this.

Regards,
Andreas

Yep that fixed the problem, I didn't know I had to tell to keep a copy. Thanks.

This topic is closed to new replies.

Advertisement