Jump to content
• Advertisement

[Solved] Strange issue getting a script to build...

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

I am having some trouble getting a small script to build using 2.4.1. I am beginning the process of integrating AngelScript with my application, and I am just trying to get a basic script up and running. The script is:
void OnLoad(int x)
{
int i;
i = x;
}


And I am using the following code to load the script:
ifstream file;
file.open("test.as");
if(!file.good())
m_Log->logMessage("Could not open script file");

file.seekg(0, ios::end);
int len = file.tellg();
file.seekg(0, ios::beg);

string code = "";
code.resize(len);
file.read(&code[0], len);
file.close();

m_ScriptEngine->SetCommonMessageStream(new LogOutput(m_Log));
m_ScriptEngine->AddScriptSection(NULL, "script", &code[0], len, 0, false);
int r = m_ScriptEngine->Build(NULL);
if(r < 0)
m_Log->logMessage("Could not build script");


I get the following message in my log file: 13:40:58: script (6, 1) : Error : Unexpected token '<unrecognized token>' Line 6 is an empty line, it's just the line after the CR/LF at the end of line 5. It's a very odd error. Does AS not properly handle CR/LF (Windows style) line endings? Or empty lines? Or am I missing something entirely? [Edited by - Calefaction on October 11, 2005 4:34:07 PM]

Share this post

Share on other sites
Advertisement
My guess is that the last character is a null character or maybe some scrap character from uninitialized memory. Try passing len-1 instead as the length of the script. It shouldn't be necessary if the filesize was computed correctly by tellg() though.

Unrelated to your problem, but a memory leak: You call SetCommonMessageStream(new LogOutput(m_Log)); AngelScript will not delete this object so it will cause a memory leak in your application. Probably not much to worry about, unless you are a perfectionist like me.

Share this post

Share on other sites
Quote:
 Original post by WitchLordMy guess is that the last character is a null character or maybe some scrap character from uninitialized memory. Try passing len-1 instead as the length of the script. It shouldn't be necessary if the filesize was computed correctly by tellg() though.Unrelated to your problem, but a memory leak: You call SetCommonMessageStream(new LogOutput(m_Log)); AngelScript will not delete this object so it will cause a memory leak in your application. Probably not much to worry about, unless you are a perfectionist like me.

I already know about the leak, this is just quick & dirty to get it up and running, but thanks for pointing it out :) I will try your suggestions and let you know.

Share this post

Share on other sites
Nope, that's not it. I have stepped through the code several times, and tellg is reporting the correct file length. Also, no null characters are padded at the end of the line (as per a memory dump).

I am pretty stumped. Everything is compiled with the same CRT settings, so there shouldn't be any wierd Debug/Release/DLL/NonDLL CRT memory issues (everything is compiled /MDd).

Share this post

Share on other sites
I solved this :)

I wasn't opening the ifstream in ios::binary mode, and thus it was translated a bunch of characters out of the stream (namely the carriage returns), thus tellg was reporting the right size, but read was only reading 39 characters (as the 5 carriage returns were getting stripped). The fix was to either open it in binary mode, or resize the string back to the gcount of the read stream.

Works now :)

Share this post

Share on other sites
text mode is evil ;)

I rarely use text mode for files, for this very reason. Even for text files I prefer to use binary mode.

Share this post

Share on other sites
I hate problems like that.

Every time I encounter problems like that, I curse myself for not knowing things.

Share this post

Share on other sites
Quote:
 Original post by Rain DogI hate problems like that.Every time I encounter problems like that, I curse myself for not knowing things.

The sad thing is, I know better ;) It's just one of those "Ah crap, I can't believe I didn't notice that" moments :P

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
2. 2
3. 3
frob
15
4. 4
5. 5
• Advertisement

• 11
• 20
• 12
• 13
• 14
• Forum Statistics

• Total Topics
632146
• Total Posts
3004433

×

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!