Game Development Community

Avoiding Global Variables

by Jeff Trier · in Torque Game Engine · 04/25/2003 (4:07 am) · 6 replies

Hi guys,

In an attempt to avoid global variables I need to ask for some help. Mainly with passing a local variables value to another function.

What I want to do is pass "LineCount" from this function:
function ReadListFile(%filename){ // Read a whole file and Store it to a set of arrays, and kick out total line count of file to a variable.
   %file = new FileObject();
   if(%file.openForRead(%filename)) {
	%index = 0;
	$LineCount = 0;
      while(!%file.isEOF()){
	//---test
		$DataSheet[%index] = %file.readLine();
		%index++;
		$LineCount++;
		echo("Lines: " @ $LineCount);
	//---test end
      }
   }
   else {
	echo("File not found!");
   }

   %file.close();
   %file.delete();
}

And send it to this function to be used in "FOR":

function UnitsList::fillList( %this ){
	%this.clear();
	%filename = "fps/server/scripts/armies/armyData.cs";
	ReadListFile(%filename);
	for ( %i = 0; %i < $LineCount; %i++ ){
		%RowInfo = buildUnitDataString($DataSheet[%i]);
		%this.addRow( %i, %RowInfo, %i );
		echo(%rowInfo);
	}
}

It currently works as a global, but I would like to make it local so I don't have to create different "$LineCount" variations for other functions which may be running simultaniously using a line counting method.

I am a little shakey on passing variables. I can grasp the concept of passing arguments to functions for processing, but I can't seem to get a function to return local variables.

For instance in this function's snippit:

//---CHECK FILE FOR WEAPON
			%word = %SetWeapon;
			%filename = "fps/server/scripts/armies/objectData.cs";
			SearchObjectFile(%filename, %word);
			%ObjectType = getWord($UnitEquipment, 0);
			//---

I would really love to be able to grab %ObjectType directly out of "SearchObjectFile(%filename, %word)". Currently, I have to store the info I want into the global variable $UnitEquipment, which contains information that was stored from the "SearchObjectFile(%filename, %word)" function.


Any help on this would be great. Thanks guys!

-Jeff

EDIT: Grammer

About the author

Originally a Classical/Metal musician, I've always been attracted to anything involving computers, including: Networking, PC Building and Repair, software design and coding. I've been involved with game design and development for over 10 years.


#1
04/25/2003 (5:56 am)
use:

return %objectType;

At the end of SearchObjectFile, and then get it by:

%objecttype = SearchObjectFile(%filename, %word);
#2
04/25/2003 (6:16 am)
Yay! It works! :)

I tried return before, but it didn't work. It was embeded in an if statement, but it seems to work if I do it at the end of the function.

Thanks,
-Jeff
#3
04/25/2003 (6:42 am)
You can put return anywhere, including an if statement.
#4
04/25/2003 (6:50 am)
Hmm, wonder why it didn't work... I will have to experiment more.

FMI:
I was wondering why it is better to use local variables instead of global variables. I have heard this, but I can't see a disadvantage to globals other than the accidental passing of information to other functions using a variable of the same name. Does it take more memory to use globals? Do local variables give up thier same memory locations to other locals after they have been used(I guess I tend to follow this logic)?

Thanks,
-Jeff
#5
04/25/2003 (7:17 am)
Many, many, many reasons. It's a bit difficult to explain, but once you've done more procedural programming it'll be blindingly obvious.

One good reason is it makes recursion infinitly easier:
function factorial(n)
{
    if (n == 1)
        return 1;
    else
        return (n * factorial(n - 1));
}

It's not impossible without locals, but it's one hell of a lot harder.

Edit: code tags
#6
04/25/2003 (12:48 pm)
Thanks Ian. :)

Recursion is another thing I need to get into one day. :)


-Jeff