Game Development Community

Question about WhackaMole in documentation

by Raymond Clark · in Torque Game Builder · 08/23/2010 (7:07 pm) · 1 replies

Hi all. I have a question about the whackamole Doc. Here is an excerpt.

function moleLevel::spawnMole(%this)
{
// find a spawn point
%respawnPointIndex = getRandom( %this.respawnPointSet.getCount()-1 );
%respawnPoint = %this.respawnPointSet.getObject( %respawnPointIndex );

// select a color for the mole
%color = getRandom(2);
if( %color == 0 )
%color = "red";
if( %color == 1 )
%color = "green";
if( %color == 2 )
%color = "lilac";


// create a new Mole
%mole = new t2dAnimatedSprite()
{
sceneGraph = %this;
class = "mole";
layer = 4;
size = "9 16";
};

My question is, how is it possible for %color to be a number then into a string. It first becomes a random number, if it equivalant to 0, then it changes into a string red. Am I on the right track with this?

#1
08/24/2010 (7:18 pm)
Its possible because you can do anything you want with variables in Torquescript. To avoid confusion, I would write the code this way:

%rnd = getRandom(2);
if( %rnd == 0 ) %color = "red";
else if( %rnd == 1 ) %color = "green";
else if( %rnd == 2 ) %color = "lilac";

It doesn't matter a hill of beans to the compiler but its easier to follow for us humans.