Game Development Community

Found a perfect way to debug projects - and make your life easier!

by Milan Rancic · in Torque Game Builder · 11/09/2009 (7:07 pm) · 9 replies

I was bored with constant compiling of my project just to test and see how one variable will affect my game - trying to find a perfect value.

So i created an input text fields with GUI and i was assigning those input values from text fields to different variables through out my project. So i could change my variables dynamically while i was IN the game.

But then... often you write the function - test the project - just to find out that you haven't set some minor thingie that render entire function unusable... :( So, back to the labs - compile again, and again and again... its not always just about some variable value.

So i found a perfect way to write the ENTIRE functions DYNAMICALLY while i am in the game! B)
Here is how:

Make one external file where you will write your temporary code. I called it "debugScript.cs". I write most of my new functions there, until i "perfect them" (and then move to the file where they belong).

Then i use these functions to exec that code dynamically:
// universal function for loading data from files.
// Returns space-delimited data (may be used as vector)
// Ignores comments at the beginning of lines, and empty rows
function readFile(%fileName) {
	if ( isFile(%fileName) ) {
		%data = "";
		%file = new FileObject();    
		
		if (%file.openForRead( %fileName )) {    
			while ( !%file.isEOF() ) {
				%lineOfCode = %file.readLine();
				if (getWord(%lineOfCode, 0) !$= "//" && %lineOfCode !$= "" )
					%data = %data @ %lineOfCode SPC "";
			}
			%file.close();
		}
		else
			%data = "Error opening file for read:" SPC %fileName;
	}
	else {
		%data = "Error loading file:" SPC %fileName;
	}
	return %data;
}

// and finally i execute the entire script from the file
function debugScript() {
	eval( readFile("./debugScript.cs") );
	echo("Debug code executed!");
}

You just bind to execute debugScript() function on a press of some key (i use "D", but you may experience problems if you are using WSAD in your game)

The effect is the same as i wrote the entire code directly into the console during the game.

#1
11/09/2009 (7:50 pm)
How is this different from the "exec" function with the "nocalls" parameter set?
#2
11/09/2009 (8:54 pm)
mmm, yes, and for changing variables ingame plastic tweaker dont do that?
#3
11/09/2009 (10:13 pm)
Really guys? This is a good idea, I don't see why you are saying it's useless. He doesn't even own TGB and he got this ready. We should be lucky that programmers are much more generous than artists at giving away their work for free.

I bet this would work for TGE too.
#4
11/09/2009 (11:40 pm)
Really, Tyler? Where did I say that his code is useless? I really wish people would quit reading extra words in my posts.

Looking over the code, I can't see a difference between his code and doing
exec( "./myScript.cs", true );

Because I don't see an immediate use for this right now, I was wondering if there was an actual difference that I couldn't see.

Also, there may be a chance that Milan was unaware of this ability in the function "exec". In that case, I've helped save him (and others) time in adding custom code in the future.
#5
11/10/2009 (7:36 am)
Oh, yea, exec can be used also :)

I just didn't thought about that at the moment, since i figured out this thing few days ago. Thnx William ;)

Maybe others misread your posts because you "go right straight to the point". Maybe you should first write "This is fine, but..." ;)

My logic was... I was putting entire lines of code directly into the console. Then i thought, "why wouldn't i try to do it automatically somehow" so i used my readFile() function in combination with eval() to do it. But my guess is that exec will work too.
The point is in general idea: to make script without frequently compiling my project.
#6
11/10/2009 (1:00 pm)
Binding it to a key is a brilliant idea, by the way!
#7
11/11/2009 (5:55 am)
thnx ;)

I just hope that many people will read this and find this useful.

I know, this made MY life easier ;)
#8
11/13/2009 (5:52 am)
(back to development)

Yea, exec() works even better!
Its best if you have dual monitors for this ;)
#9
11/13/2009 (6:11 am)
Cool!

I know you don't have the source, but if you ever do, you should check out this resource by Phillip O'Shea. Something in your last post triggered my memory that this existed. I keep meaning to implement it and I keep forgetting! (Although, 99.99% of the time I just need to tweak one script and binding an "exec" to that sounds tons easier.)