Game Development Community

Mouse clicking objects and scheduled calls, beginner tips

by Erlend Salbu · in Torque 3D Beginner · 10/06/2009 (7:25 pm) · 2 replies

Simple script showing how to mouseclick specific types of objects in torque 3d, determine distance and do scheduled function calls and a progressbar type rpg for like mining rocks, including determining number of clicks and adding stuff to guis. im very new to the scripting language and strugled finding out several of these things, so posting in hope itl help other beginners:)

its not a script you can just import as it have hardcoded stuff, more a educational thing for other noobs:), i have annotated it heavily, everything in it can be executed from playGui.cs:)

hope someone find it useful:)

//on mouse down in the game, here we go:)
function PlayGui::onMouseDown(%this, %pos, %start, %ray)
{

   // find end of search vector
   %ray = VectorScale(%ray, 2000);
   %end = VectorAdd(%start, %ray);
   
   // we only care about Item Objects
   // you can search for several object types like this
   // (%start,%end,$TypeMasks::TerrainObjectType | $TypeMasks::TerrainObjectType); 
   %scanTarg = ContainerRayCast(%start,%end,$TypeMasks::ItemObjectType); 
     
   // If An Item object was found in the scan
   if( %scanTarg )
   {
   //Find Distance between player and object
   //note $client1.player beeing the player object, replace with whatever player object you 
   //have
   %distance = VectorDist($client1.player.getPosition(),%scanTarg.getPosition());
        
       //Check distance, ignore if its too far away 
 	if (%distance > 5) 
	{ 
	//realtime debugging using the chathud to show if the distance is ok, use echo as/if you please:)
		chatHud.addLine (%distance SPC "FALSE");
	}
	else 
	{ 
		// ignore click if a delay is already pending
		if( isEventPending($game::58581) ) 
	     	{
			//echo("got a pending event");
		}
		else
		{
		// Process Progressbar within the same time as the getloot function is finished, 
                // 1 second   in this case
		Myprogressbar.updateThread = Myprogressbar.schedule(100, "update");
	
		
		// call getloot with a 1 second delay, note 58581 is just a unique event id, 
                // could be anything, a literal name would work too
		$game::58581 = schedule($game::EndGamePause * 100, 0, "Getloot",%scanTarg);
		}	
	//realtime debugging using the chathud	
	chatHud.addLine (%distance SPC "TRUE");
   	}

    }

}


//this function is quite hacky, just for learning purposes, just adding it to show the flow
function getloot(%obj)
{

//Get dynamic fieldvalue from object, note in this case the dynamic value is already set to 1, 
//for the dynamic field counter1
%fldvalue = %obj.getFieldValue("counter1");

//p1 is the hardcoded name of a 'inventory' gui bitmap object
p1.setFieldValue(bitmap, "art/gui/redrock.jpg");	

        // check if object is pressed more than 2 times and add rest of points and delete it
	if (%fldvalue > 2 )
	{
	
	%obj.delete();
	$globpoints += 10;
	
        //add info to the guis, note te1 is a hardcoded name of a text object in a 'inventory' gui.
        Mylist.additem("Mining Skills:" SPC "10",1);
	Mylist.additem("Total:" SPC $globpoints,1);
	MyScroll.scrollToBottom();
	te1.setFieldValue(text, $globpoints / 10);
	}
	// else just add some points, update guis
	else
	{
	$globpoints += 5;
	Mylist.additem("Mining Skills:" SPC "5",1);
	MyScroll.scrollToBottom();
	//set dynamic field value + 1
	%obj.setFieldValue(counter1, %fldvalue + 1);
	te1.setFieldValue(text, $globpoints / 10);
		
	}
}

function PlayGui::onRightMouseDown(%this, %pos, %start, %ray)
{	
         hideCursor();	 	
}




//run a progressbar that go to max in 1 second then reset to 0
function Myprogressbar::update(%this)  
{  
   // progress bar is a value between 0 and 1,  
   // so increment by 1 and divide by 10 to  
   // to coincide with the 1 seconds schedule  
   %this.current +=1;  
   %newValue1 = %this.current / 10;  
   	if (%newValue1 > 1)
		{
		cancel(Myprogressbar.updateThread); 
		 %this.current = 0;
		 %this.setValue(0); 
		}
		else
		{// change the progress bar  
			%this.setValue(%newValue1);  
  
   // call ourself 0.1 seconds later to update again  
   	%this.updateThread = %this.schedule(100, "update");  
	}
}

#1
10/06/2009 (7:47 pm)
Im also happy to receive any sugestions,corrections and improvements, theres likely flaws in the coding as im new to it, but it work at least:)
#2
09/08/2010 (3:47 am)
I am highly interested in this functionality, but am a total noob with this(still working on building my first level).

What do I do with this. Put it in a file of it's own? If so what do I name it?

Or do I paste this code into playGui.cs?

I am sorry, I know how noob questions can be annoying.


TIA!