Automatically add () and ; to anything typed in the console
by Tom Bampton · 07/23/2005 (10:11 pm) · 10 comments
Tom Spilman and I were just discussing scripting languages on IRC, and we both expressed how annoying it was having to type the (); after quit when wanting to quit Torque after typing stuff in the console.
So, we whipped up this little snippet that automatically adds () and ; to anything you type in the console that needs it. This means you can omit the ; or the () or both for anything you type into the console.
Open up common/ui/ConsoleDlg.gui in your favorite editor, scroll down to the ConsoleEntry::eval() method, and modify as follows:
Note that this is based on Torque 1.4, so may be slightly different for other versions of Torque. The difference will not be all that great, so it should be easy to apply to older versions of Torque.
So, we whipped up this little snippet that automatically adds () and ; to anything you type in the console that needs it. This means you can omit the ; or the () or both for anything you type into the console.
Open up common/ui/ConsoleDlg.gui in your favorite editor, scroll down to the ConsoleEntry::eval() method, and modify as follows:
function ConsoleEntry::eval()
{
%text = trim(ConsoleEntry.getValue());
if(strpos(%text, "(") == -1)
{
if(strpos(%text, "=") == -1 && strpos(%text, " ") == -1)
{
%text = %text @ "()";
}
}
%pos = strlen(%text) - 1;
if(strpos(%text, ";", %pos) == -1)
{
%text = %text @ ";";
}
echo("==>" @ %text);
eval(%text);
ConsoleEntry.setValue("");
// Check for any pending errors to display
updateConsoleErrorWindow();
}Note that this is based on Torque 1.4, so may be slightly different for other versions of Torque. The difference will not be all that great, so it should be easy to apply to older versions of Torque.
About the author
Recent Blogs
• A Game in 2750 Days• GID23 and NPC Editor
• Fun with Lua
• How NOT to make a game
• Thinking Outside the Box
#2
At the end of the day, this is a 60 second hack to make life a little bit easier, and it works exactly as it was intended to work. The better solution would be to write an isFunction() ConsoleFunction and check if what is typed is a function, and perhaps do a bit of parsing to better support multiple statements. However, that would require C++ changes and we wanted to keep it script only.
T.
07/24/2005 (6:25 am)
That's not a bug, its a design feature. It is better to not modify the string when it should then to modify it when it shouldnt, as then it could potentially break valid code. This way it will never break code that should be otherwise valid. If you want the second example to work, take out the "&& strpos(%text, " ") == -1". I checked for the space to attempt to catch situations that we hadnt considered, just in case. That said, how often do you write multiple statements on one line in the console ?At the end of the day, this is a 60 second hack to make life a little bit easier, and it works exactly as it was intended to work. The better solution would be to write an isFunction() ConsoleFunction and check if what is typed is a function, and perhaps do a bit of parsing to better support multiple statements. However, that would require C++ changes and we wanted to keep it script only.
T.
#3
07/24/2005 (11:51 am)
In that case, I haven't said a word. I can definately see it's usefullness (sp?). Cheers!
#4
Can't this also be done by hitting TAB when something is partially typed in the console? So if you just Type "Qu", hit tab, it scrolls through all functions starting with "Qu" (Quit();)
07/24/2005 (4:32 pm)
What about adding/Changing variables? Would $myVar = "Hello"; be made into $myVar = "Hello"();?Can't this also be done by hitting TAB when something is partially typed in the console? So if you just Type "Qu", hit tab, it scrolls through all functions starting with "Qu" (Quit();)
#5
The TAB feature works differently, but in theory it could be changed to do that.
07/24/2005 (4:42 pm)
@Alex - If you look closely you'll see that it checks for variable assignment and avoids adding () if it's detected.The TAB feature works differently, but in theory it could be changed to do that.
#6
Re-reading the resource, I should probably have mentioned some of what I said in my last post in there. Never mind :)
Re: Tab,
That would require C++ code. It is certainly possible, but it's more involved.
07/24/2005 (6:09 pm)
Thijs,Re-reading the resource, I should probably have mentioned some of what I said in my last post in there. Never mind :)
Re: Tab,
That would require C++ code. It is certainly possible, but it's more involved.
#7
08/04/2005 (9:55 pm)
I've put a version of this into 1.4 that properly deals with assignations, function declarations, if statements, etc... As someone who frequently does complex statements into the console, I took some extra time to make sure it would work properly. :)
#8
08/04/2005 (10:05 pm)
@Ben - Freaking sweet!
#9
metrics fps
... into the console and it automatically makes it a function. This works with any function with any amount of parameters, except for the case of spaces embedded in strings (that does the wrong thing... someone feel like fixing that case for me).
02/26/2006 (12:52 am)
I just added an enhancement to this. With this change you can type...metrics fps
... into the console and it automatically makes it a function. This works with any function with any amount of parameters, except for the case of spaces embedded in strings (that does the wrong thing... someone feel like fixing that case for me).
function ConsoleEntry::eval()
{
%text = trim(ConsoleEntry.getValue());
// Skip anything that looks like a complete statement
// as we don't want to junk it up.
if( strpos(%text, "(") == -1 &&
strpos(%text, ")") == -1 &&
strpos(%text, "=") == -1)
{
if(strpos(%text, " ") == -1)
{
%text = %text @ "()";
} else {
// We assume this is a function with space separated
// parameter list. The first space becomes a ( and each
// subsequent space becomes a comma.
%i = strpos(%text, " ");
%cmd = getSubStr( %text, 0, %i ) @ "(";
%text = getSubStr( %text, %i, 99999 );
%text = ltrim( %text );
%i = strpos(%text, " ");
while ( %i != -1 )
{
%cmd = %cmd @ getSubStr( %text, 0, %i ) @ ",";
%text = getSubStr( %text, %i, 99999 );
%text = ltrim( %text );
%i = strpos(%text, " ");
}
%text = %cmd @ %text @ ")";
}
}
// Do we need to close the statement?
%pos = strlen(%text) - 1;
if(strpos(%text, ";", %pos) == -1)
{
%text = %text @ ";";
}
echo("==>" @ %text);
eval(%text);
ConsoleEntry.setValue("");
// Check for any pending errors to display
updateConsoleErrorWindow();
}
#10
02/14/2007 (3:32 am)
Very nice resource. 
Torque Owner Thijs Sloesen
Scattered Studio
Examples that won't work with this code:
echo("hello"); quit
myVar = 2; quit