- Home /
yield troubles. function needs to finish before continuing
Ok so I'm not to sure how to explain this.
I am using the MoveObject script from the wiki. Its awesome, It does waht I need it to do just fine. The problem I have is the function that I am calling MoveObject->Translation needs to finish before moving on.
Here is Where I call my moveFunction
HexManager.GetInstance().MovePlayer( newPos );
activeUnit.canMove = false;
StateMachine.GetInstance().PopState();
Now this is that function
function MovePlayer( destPos : Vector2 )
{ // turn off any grid HexManager.GetInstance().TurnOffCurrentGrid();
var endHex; // Get the active unit var activeUnit = WorldManager.GetInstance().GetActiveUnit();
// Get the script component Unit from the active unit var activePlayerScript = activeUnit.GetComponent( "Unit" );
Debug.Log( " BEFORE activeUnit pos = " + activePlayerScript.hexPos );
// Search for shortest path AStar.GetInstance().Search( activePlayerScript.hexPos, destPos );
// If path found move player if( AStar.GetInstance().IsPathFound() == true ) { // reset current hex variables GetHex( activePlayerScript.hexPos ).GetComponent( HexData ).hexOccupant = null; GetHex( activePlayerScript.hexPos ).GetComponent( HexData ).isOccupied = false;
// Get the path
var highlightPath = AStar.GetInstance().GetPath();
if( highlightPath.length - 1 <= activePlayerScript.moveRange )
{
var moving : boolean = false;
var mainCam = GameObject.Find( "Main Camera" ).GetComponent( FreeFollowCamera );
mainCam.SetUserControl( false );
mainCam.SetFollowTarget( activeUnit );
// translate between each hex in path
for( index = 0; index < highlightPath.length; ++index )
{
if( highlightPath[ index ].GetComponent( HexData ).IsTrapped() == true )
{
yield MoveObject.GetInstance().Translation( activeUnit.transform , activeUnit.transform.position, highlightPath[ index ].transform.position, 10, 1 );
endHex = highlightPath[ index ].GetComponent( HexData );
endHex.trap.GetComponent( Trap ).Trigger();
// Change units position
activePlayerScript.SetHexPos( endHex.hexPos );
break;
}
else
{
yield MoveObject.GetInstance().Translation( activeUnit.transform , activeUnit.transform.position, highlightPath[ index ].transform.position, 10, 1 );
endHex = highlightPath[ index ].GetComponent( HexData );
// Change units position
activePlayerScript.SetHexPos( endHex.hexPos );
}
}
}
else
{
// A Pop up box will or sound or text will notify the player they can't move there for what ever reason?
// Temp
print( "Dest pos = " + destPos );
print( " Cannot move there " );
return;
}
} else { print( " Path not found" ); return; }
// Update hex GetHex( endHex.hexPos ).GetComponent( HexData ).hexOccupant = activeUnit; GetHex( endHex.hexPos ).GetComponent( HexData ).isOccupied = true;
Debug.Log( " AFTER activeUnit pos = " + activePlayerScript.hexPos );
mainCam.SetUserControl( true );
WorldManager.GetInstance().GetComponent( WorldManager ).remainingActions -= 1;
WorldManager.GetInstance().SetState( eCurrentState.eIdle );
}
The problem is that the few lines of code after HexManager.GetInstance().MovePlayer( newPos ) are being called before MovePlayer is finished. I can't find a way to translate smoothly without a coroutine. Any help would be greatly apreciated.
Answer by Eric5h5 · Jul 24, 2010 at 02:19 AM
HexManager.GetInstance().MovePlayer( newPos );
needs to be
yield HexManager.GetInstance().MovePlayer( newPos );
I have tried that many of times. It just sits in a function somewhere. It doesn't freeze but it never moves the player.
@$$anonymous$$ike: I don't know what World$$anonymous$$anager.GetInstance() is doing. $$anonymous$$aybe try "yield StartCoroutine(Hex$$anonymous$$anager.GetInstance().$$anonymous$$ovePlayer( newPos ));"
or else use a different way of accessing $$anonymous$$ovePlayer so it can run as a coroutine.