Game Development Community

getRandom() always retuning the same values

by DiegoGdVU · in Torque 2D Beginner · 08/05/2013 (8:30 pm) · 2 replies

Hello i'm having a problem the RNG always returns the same numbers, this is happening in the tutorial and in a prototype i'm making. Im just opening the Torque2d.exe and closing it with alt+F4 the sprites spawn in the same position every time.

function createEnemy(%Number_of_Enemies)
{
for(%i=0;%i<%Number_of_Enemies;%i++)
   {
		%enemy = new Sprite(fireEnemy)
		{
			size= "4 4"; 
			SceneLayer   = "1";
			Angle= "0";	
			fixedAngle=true;
			CollisionCallback = true;
		};
		
		%enemy.Position=  getRandom(-20,20) SPC getRandom(-20,20);
		echo(%enemy.Position);
		
		%enemy.createCircleCollisionShape(1);
		
		%enemy.Image = "mainModule:fireEnemyImage";
		
		%takesDamageBehavior = TakesDamageBehavior.createInstance();
		%takesDamageBehavior.initialize(100, "", "", true);
		fireEnemy.addBehavior(%takesDamageBehavior);
		
		myScene.add( %enemy );   
				
	}
}

This is my main.

function mainModule::create( %this )
{
	exec("./gui/guiProfiles.cs");
	exec("./scripts/controls.cs");
	exec("./scripts/sceneWindow.cs");
	exec("./scripts/scene.cs");
	exec("./scripts/player.cs");
	exec("./scripts/enemies.cs");
	exec(".scripts/dealsDamageBehavior.cs");
	exec(".scripts/takesDamageBehavior.cs");
	exec("./scripts/generateCave.cs");
	
	createSceneWindow();
	createScene();
	mySceneWindow.setScene(myScene);
	
	createPlayer();
	createEnemy(4);
	//createLevel();
	
	//add suport for controls to the project
	new ScriptObject(InputManager);
	mySceneWindow.addInputListener(InputManager);
	InputManager.Init_controls();
}

function mainModule::destroy( %this )
{
	InputManager.delete();
	
	destroySceneWindow();
	//delete the action map
	playerControls.pop();
}

#1
08/05/2013 (8:42 pm)
One way to do it is to set a new Randomized Seed before calling getRandom

setRandomseed();

#2
08/05/2013 (9:49 pm)
Well that did the trick, thanks Simon.