Advertisement

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

Started by October 11, 2005 01:42 PM
6 comments, last by Calefaction 19 years, 1 month ago
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]
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com
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.




AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
Quote: Original post by WitchLord
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.



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.
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com
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).
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com
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 :)
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com
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.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Advertisement
I hate problems like that.

Every time I encounter problems like that, I curse myself for not knowing things.
Quote: Original post by Rain Dog
I 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
Matt Holmes[ aka Calefaction ]Wildfire Games - General Programmerhttp://www.wildfiregames.com

This topic is closed to new replies.

Advertisement