Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


- - - - -

Questions on implementation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 daerid   Members   -  Reputation: 354

Like
0Likes
Like

Posted 02 December 2004 - 11:42 AM

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

Sponsor:

#2 abrken   Members   -  Reputation: 130

Like
0Likes
Like

Posted 02 December 2004 - 05:37 PM

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.



#3 Andreas Jonsson   Moderators   -  Reputation: 3453

Like
0Likes
Like

Posted 03 December 2004 - 12:59 AM

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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS