Game Development Community

Releasing Memory

by Derk Adams · in Torque Game Engine · 02/20/2005 (8:27 am) · 16 replies

Greetings,

I am using the script engine to pull in large arrays to provide selections for the user (gui selection). Since I use the arrays multiple times, I store them in global variables. Once I am done with them, I would like to remove them from memory. How do I do this?

Thanks.

#1
02/20/2005 (8:36 am)
Delete(%varaible); is used for objects, it might work with arrays as well.
#2
02/22/2005 (1:42 pm)
Anthony,

Good try, but nope, delete() nor deleteVariable() work for just simple variables.

To give a better description, here is what I want:

$myVariable = "myValue";
echo("myVariable is" SPC $myVariable);
do something to delete variable (not just clear it)
echo("myVariable is" SPC $myVariable);

The first echo should show "myVariable is myValue" and the second echo should show "myVariable is ".

Any ideas?

Thanks.

P.S. as another solution, I have a table of information in a text file. I am pulling it in and providing user selectable options in multiple places. Then when done, I want to delete it all. Is there a structure that will let me do that?
#3
02/22/2005 (1:56 pm)
Did you put quotes around it?

deleteVariable("$myVariable");

That works for me, it removed mine in my code.
#4
02/22/2005 (2:03 pm)
Steve,

No, I didn't try it with the quotes (everything else though :) It works on simple variable names beautifully.

Any suggestions for variables like this?

$myVariable[%n] or $myVariable[1]

Thanks.
#5
02/22/2005 (2:26 pm)
@Derk: I haven't tried this but I believe that variables such as $myVariable[1] are the same as $myVariable1 so you could try deleteVariable("$myVariable1") and maybe deleteVariable("$myVariable"@%n) may work too although I'm completely guessing here. Worth a try.

- Melv.
#6
02/22/2005 (2:33 pm)
Melv,

Ah, that was the piece I was missing. Both ways work.

Thanks.
#7
02/22/2005 (2:49 pm)
Cool. Didn't actually know if that would work so we've both learnt something today. :)

- Melv.
#8
02/26/2005 (10:33 am)
Set them up as such. $My::Varible[#] then do deleteVaribles($My::); This will whipe them all in one call.
#9
02/26/2005 (1:32 pm)
Ralph,

I assume you meant to type deleteVariables($My::); and it works beautifully.

Thanks.
#10
02/26/2005 (2:41 pm)
Update.

deleteVariables($My::); doesn't work, it gives a script error and deleteVariables("$My::"); doesn't error, but doesn't work either.

Any ideas?
#11
02/26/2005 (3:14 pm)
Based on a real quick scan of the underlying C++ code, try it without the $ in front. deleteVariables appears to only work on global variables, therefore the $ would be redundant to save as part of the variable name in the hashtable. Just a guess, but it does look like this may solve your issue.

In other words, try: deleteVariables("My::");
#12
02/26/2005 (4:03 pm)
I used deleteVariables("$My::*") and it worked.
#13
02/26/2005 (4:24 pm)
Stephen,

It doesn't work without the dollar sign and without the star, although it doesn't throw an error.

Jacob, I see you caught the second quote :)

deleteVariables("$My::*") WORKS.

Thanks everyone, this will make a lot of my programming much easier.
#14
09/17/2006 (4:13 pm)
I found the following works fine for me.

$arrayNLPlayerList[0]=0;
$arrayNLPlayerList[1]=1;
$arrayNLPlayerList[2]=2;
deleteVariables("$arrayNLPlayerList*");

Deletes the array and all elements.
#15
09/18/2006 (11:49 am)
Or put them into an object which you delete:

function newplayer(%name) {
  // Create the player list if it doesn't exist
  if(!isObject(NLPlayerList)) {
    new ScriptObject(NLPlayerList);
    NLPlayerList.numplayers = 0;
  }

  // Add player to list
  NLPlayerList.Player[NLPlayerList.numplayers] = %name;
  NLPlayerList.numplayers++;
}

function clearplayers() {
  // Bye Bye
  if(isObject(NLPlayerList)) {
    NLPlayerList.delete();
  }
}

Then just do stuff like:
// Populate some players
newplayer("John");
newplayer("Paul");
newplayer("George");
newplayer("Ringo");
...


// Add list of names to GUI object
for(%i=0; %i<NLPlayerList.numplayers; i++) {
  MyGuiControl.AddField(NLPlayerList.Player[%i]);
}

// Delete players
clearplayers();

Gary (-;
#16
09/18/2006 (10:40 pm)
Yes you can use a script object as a sort of array. Many ways to delete vars but I think as I posted is the best method although apparently I forgot to put the astrik at the end.

$Var::MyVar[0] = "foo";
$Var::MyVar[1] = "foobar";
deleteVaribles("$Var::*");

or this method

for (%i = 0; $Var::MyVar[%i] !$= ""; %i++)
$Var::MyVar[%i] = "";

$ScriptObject = new ScriptObject(MyScriptObject) {
Var[0] = "foo";
Var[1] = "foobar";
};

$ScriptObject.delete();