Sign in to follow this  
swan1981

Embed GameMonkey in vs c++

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this