Questions on implementation

Started by
1 comment, last by WitchLord 19 years, 4 months ago
I'm testing out a rudimentary AngelScript console, and am running into a problem with it. It has to do with how asCScriptEngine::ExecuteString is implemented. Right now I want to be able to do something like this (I have registered the string type and print() function):

AngelScript Console 0.1 (AS Ver: 1.10.1b)

>> string test = "Hello, World!\n";
>> print(test);
This does nothing at the moment, because my console loop looks like this (pseudo-code):

string line;
while(1)
{
  getline(cin,line);
  if(!cin)
    break; // Use CTRL+Z to exit
  
  pEngine->ExecuteString("as_console",line.c_str());
}

Now, I'm pretty sure the reason this doesn't work is because of how asCScriptEngine::ExecuteString works: It wraps the string in a "void ExecuteString() {\n" + str + "}" script chunk. This turns the above line string hello = "Hello, World!\n"; into a local variable, which isn't found in the subsequent call to print(hello); Is there anything way to create a variable in one call to ExecuteString() and then reference it in another call? Or am I completely missing how AngelScript works?
daerid@gmail.com
Advertisement
You can't reference a global variable throught ExecuteString, and you have discovered why : the script you pass to ExecuteString is simply parsed and executed in a local defined function.

What you can do :

Scan the input collecting it into a string. When you scan that the input is "run" then, call execute string with the collected string.

string line, alllines;while(1){  getline(cin,line);  if(!cin)    break; // Use CTRL+Z to exit  if (line != "run")    alllines += line;  else    pEngine->ExecuteString("as_console",alllines.c_str());}


hope it helps.

AbrKen.

Variables declared in ExecuteString() are as you noticed local to the function, and cannot be used in subsequent calls to ExecuteString(). It can however access global variables declared by a previously built script, or registered by the application.

What abrken said would allow a user to write scripts with multiple lines and execute them with ExecuteString(), which is a good thing in my opinion.

If you want the user to be able to create new variables on the fly, you will have to find some way of storing the declaration for later use. I suggest you register some object, that would work kind of like a dictionary, or a map. It could for example look like this in the console:

>> mem("test") = "Hello, World!\n";
>> print(mem("test"));

You could also do something like this:

>> declare string test
>> test = "Hello, World!\n";
>> print(test);

Where the declare instruction is a special instruction that registers a new global property with the script engine used for the console. AngelScript currently doesn't have any way of unregistering functions or properties again, so the user wouldn't have any way of undeclaring a variable like this.

Yet another solution would be to allow the user to alter the script compiled with the engine used by the console. This way it would be possible for a user to add new global variables and even functions. Each time the user alters the script, it is recompiled. If you do this it might be a good idea to store the value of each of the global variables before rebuilding the script, then after the build reload the values, otherwise the values will be reset with each build.



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

This topic is closed to new replies.

Advertisement