Page 1 of 1

GameMonkey problem

Posted: Wed Sep 29, 2010 9:29 am
by Virion
I have switched to GameMonkey recently because it's easier to compile and use it on my app. The implementation works, but somehow I'm stumbled with a problem which is reading the script from a file.

I have a test.gm file that contains the following script

Code: Select all

print(\"yo\");
and I use the following code to read the file and run the script

Code: Select all

std::string s;
std::ifstream file;
file.open("test.gm");
while(!file.eof())
{ getline(file, s); }
file.close();

gm->ExecuteString(s.c_str());
Unfortunately nothing happen. Supposingly it should print the word "yo".

However if I don't read the script from file and include it in the source code, it works like charm

Code: Select all

std::string str = "print(\"yo\");";
gm->ExecuteString(s.c_str());
can anyone spot any error from my code? thanks in advance.

I have asked the same question on GameMonkey forum and waited for days but the forum seems dead.

Posted: Wed Sep 29, 2010 10:44 pm
by xDan
I know nothing about gamemonkey. But anyway:-

- why do you escape the quotes (\") in the test.gm file? Does gamemonkey require it? (I'm guessing not)
- print out your getlines for debugging. and print out the final "s" before calling ExecuteString. See what it contains.
- doesn't getline overwrite the string each time? So you will only end up with the last line in the file stored in s (which might be a blank line).

So try:

Code: Select all

std::string s;
std::string temp;
std::ifstream file;

file.open("test.gm");

while(!file.eof())
{
	getline(file, temp);
	s += temp;
}

file.close();

cout << "File contents: " << s;

gm->ExecuteString(s.c_str());

Posted: Thu Sep 30, 2010 4:41 am
by DarkDepths
I don't know very much about GameMonkey either, but I do know that it does require the escape characters in this case. (I only did a very short tutorial on it) Sorry, I have no other information to share.

Posted: Thu Sep 30, 2010 7:35 am
by Virion
thanks! it works now. the escape characters is not required if your script is stored in a text file. ahh didn't know getline overwrite the string each time.

again, big thank! :D

Posted: Thu Sep 30, 2010 9:35 pm
by stefbuet
Hey you made me discover GameMonkey! Thanks.
I'll surely use it instead of doing my own code parser to save time. I find it better than Lua because of its C style & little size. I had never used Lua but when looking at it, it looks like pretty (too) huge.

Posted: Thu Sep 30, 2010 11:58 pm
by Virion
i tried Lua and it's very hard to bind (for me) even if use the luabind which requires boost. very huge.