Game Development Community

Need help creating a "NPC Movement" behavior for a RPG

by Diego Santos Leao - GameBlox Studio · in Torque Game Builder · 03/27/2008 (7:48 am) · 3 replies

I'm creating a top down rpg and I want to add a random movement to some of my npcs (in the future I will add some AI, but for now random would be nice).

The problem is that I don't know the "right way" to do this. I can only think of a behavior that, when added, would start a scheduled event to call "setConstantForce(%X,%Y)" to make the character walk and a "setConstantForce(0,0)" to stop him.

Is there another (better) way?

#1
03/27/2008 (11:35 am)
Another way would be to use the moveTo function.

Quote:
moveTo(t2dVector positionTarget, float linearSpeed,[bool autoStop=true],[bool callback=false],[bool snap=true],[float targetMargin=0.1])

Description: Sets a position target for the object and moves toward it. The object is not actually forced to the target position, it is merely sent there. So, if a collision or some other outside force interferes with the object, it may not make it to the target position. However, the target is still set, so the object may still reach the target in the future.

Params:
positionTarget: The x and y position of the position target.
linearSpeed: The speed at which the object is setn to the target.
autoStop: Whether or not to autoStop upon reaching the taret.
callback: Whether or not to perform a script callback (onPositionTarget()) upon reaching the designated target.
snap: Whether or not to snap the object to the target when it is within margin.
targetMargin: The proximity to the target necessary to qualify as reaching the target.
#2
06/15/2008 (12:08 am)
Maybe this snippet would be of help? It's the code I used to animate my pets in Grimm's Hatchery.

//---------------------------------------------------------------------------------------------
// Moves the pet or monster in a random direction.
//---------------------------------------------------------------------------------------------
function Sprite::moveSprite(%this)
{
    %this.direction = getRandom(1, 4);


    switch (%this.direction)
    { 
        case 1: //move right
            %this.targetX = %this.getPositionX()+10; 
            %this.targetY = %this.getPositionY();
            %this.frame = 0;            

        case 2: //move left
            %this.targetX = %this.getPositionX()-10; 
            %this.targetY = %this.getPositionY();
            %this.frame = 4;
            
        case 3: //move up
            %this.targetX = %this.getPositionX(); 
            %this.targetY = %this.getPositionY()-10;
            %this.frame = 8;
            
        case 4: //move down
            %this.targetX = %this.getPositionX(); 
            %this.targetY = %this.getPositionY()+10;
            %this.frame = 12;

        case 5: //don't move
            %this.targetX = %this.getPositionX(); 
            %this.targetY = %this.getPositionY();
            %this.frame = 12;
    }

    //move sprite
    %this.firstFrame = %this.frame;
    %this.setTimerOn(%this.timePeriod); 
    %this.moveTo(%this.targetX, %this.targetY, %this.speed);
}



//---------------------------------------------------------------------------------------------
// TIMER
// Use this timer manager to track events in the sprite class.
//---------------------------------------------------------------------------------------------
function Sprite::onTimer(%this)
{
    if ($paused == 0)
    {

        %this.setTimerOff();

        // Move pets
        if (%this.time < 3)
        {     
            %this.setFrame(%this.frame);
            %this.setTimerOn(%this.timePeriod);  

            %this.frame++;   
            %this.time++;      
        }         

        else pause
        {
            %this.time = 0;
            %this.setFrame(%this.firstFrame);
            %this.moveSprite(); 
        }

    }
}
#3
08/09/2008 (6:09 pm)
I just saw your answer amaranthia :)
I solved this issue with the moveTo (as suggested by Mike - Thanks mike!) function triggered by a scheduled event. I didn't knew about the onTimer callback, and will use it the next time I need something similar :) Thanks for your answer and would also notice that I am a fan of your work, though I only played the demo of your game :) Unfortunatelly I don't have the time to play all of it (I have a 'day job') :(