🎉 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!

Embed GameMonkey in vs c++

Started by
1 comment, last by evolutional 19 years, 9 months ago
Hello everyone, I have a question. if i want to run the following script from a file. temp = true; if (temp) { print ("True"); } else { print ("False"); } how can i run this script in c++, because i have an error in running the script. #include <iostream> #include <fstream> #include <string> #include "gmThread.h" using namespace std; FILE *stream; void main( void ) { string spt = ""; string tmpChar; fstream fs; fs.open( "script.gm" ); while ( !fs.eof() ) { fs >> tmpChar; spt += tmpChar; } char *charArray = new char[ spt.length() ]; spt.copy( charArray, spt.length(), 0 ); gmMachine machine; int error = machine.ExecuteString( charArray, NULL, false, NULL ); if (error) { bool first = true; const char * message; while((message = machine.GetLog().GetEntry(first))) { fprintf(stderr, "%s"GM_NL, message); } } fs.close(); } Or, is there any method to read and run this script more effectively?? Thanks.
Advertisement
I think it would be better to allocate memory on the heap in this case and free it as soon as you are done with it. In this case allocating memory on the stack would last through the execution of the program. I am not sure that is too good if the game turns out to be large and the scripts too.

- Sid
In my gmScriptEnvironment my loadscript routine has this code for loading the scripts...

        std::ifstream fin;	fin.open( filename, std::ios_base::in);		std::streamoff nLen = 0;	if ( !fin.is_open() )	{		return false;	}		std::string sScriptLine, sScript;	while ( !fin.eof() )	{					std::getline( fin, sScriptLine );		sScript.append( sScriptLine );	}


Afterwards, sScript is stored away for compilation and later use. Hope this was helpful. You may be interested in the new site I've created for GameMonkey. I'm running a series of articles that curently focus on the basics, but I'm going through and explaining how to create a robust script environment for GM and how to use gmBind, my new easy-binding template that allows easy porting of C++ classes to GM.

This topic is closed to new replies.

Advertisement