x


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.

more ▼

asked Jul 24 '10 at 02:03 AM

Mike 4 gravatar image

Mike 4
37 7 7 10

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
HexManager.GetInstance().MovePlayer( newPos );

needs to be

yield HexManager.GetInstance().MovePlayer( newPos );
more ▼

answered Jul 24 '10 at 02:19 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

I have tried that many of times. It just sits in a function somewhere. It doesn't freeze but it never moves the player.

Jul 24 '10 at 02:32 AM Mike 4

@Mike: I don't know what WorldManager.GetInstance() is doing. Maybe try "yield StartCoroutine(HexManager.GetInstance().MovePlayer( newPos ));" or else use a different way of accessing MovePlayer so it can run as a coroutine.

Jul 24 '10 at 06:08 AM Eric5h5
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x336
x324
x42

asked: Jul 24 '10 at 02:03 AM

Seen: 1053 times

Last Updated: Jul 24 '10 at 02:03 AM