Movement (basic)

A basic code example that shows you how to make the bot move between two locations.

MovementTask.cs

Task class that will be registered in PluginBase.cs and will be responsible for Movement in this plugin. While the player is not then this task will move between two Locations.

public class MovementTask : ITask, ITickListener {
    
    public static ILocation Location1 = new Location(100, 10, 100);
    public static ILocation Location2 = new Location(  1, 10, 100);
    
    public override bool Exec() {
        // Do not execute 'OnTick' if the bot is dead.
        return !Context.Player.IsDead();
    }
    
    public async Task OnTick() {
        // The 'await' keyword waits until MoveTo completes/fails and
        // only then continues the execution.
        var moveResult = await Context.Player.MoveTo(Location1).Task;
        if(moveResult.Result != MoveResultType.Completed) {
            // Failed to move to the location, lets output an error 
            // to the console and return.
            Console.WriteLine($"Failed to move to {Location1}!");
            return;
        }
        
        // We have successfully reached Location1, 
        // now we can start moving to Location2.
        var moveTask = Context.Player.MoveTo(Location2);
        
        /*
            As the MoveTo(Location2) code does not have the 'await' keyword 
            we can execute other actions parallel and optionally await later
            for the task to complete.
        */
        
        // Wait until MoveTo completes/fails to Location2. We do not care
        // if it succeeded therefore we do no assign the result to any variable.
        await moveTask.Task;
    }
}

It is usually desirable to use the 'await' keyword as it will pause all execution until the task completes and then the result can be used. Not awaiting for tasks to complete can cause another function (e.g.: OnTick) to start while a task is running (e.g.: while still moving).

Don't know what async programming is? Check out Microsoft's documentation here.

Last updated