Simple greeter bot

From Virtual Paradise Wiki
Revision as of 11:35, 9 February 2019 by Edwin (talk | contribs) (→‎C#)
Jump to navigation Jump to search

C/C++

In order to use the Virtual Paradise C SDK you must:

  • Have Visual Studio, Visual C++ Express or another compatible C/C++ IDE+Compiler installed and configured
  • Download the x86 SDK from the Developer Downloads page
#include <VPSDK/VP.h>
#include <stdio.h>
#include <stdlib.h>

#define Username "username"
#define Password "password"
#define Botname  "greeterbot"
#define Worldname "testworld"

void event_avatar_add(VPInstance sdk);

int main(int argc, char ** argv)
{
    int err;
    VPInstance sdk;

    if(err = vp_init(VPSDK_VERSION))
    {
        printf("Couldn't initialize VP API(reason %d)", err);
        return 1;
    }

    sdk = vp_create();

    if(err = vp_connect_universe(sdk, "universe.virtualparadise.org", 57000))
    {
        printf("Couldn't connect to universe(reason %d)", err);
        return 1;
    }
    
    if(err = vp_login(sdk, Username, Password, Botname))
    {
        printf("Couldn't login(reason %d)", err);
        return 1;
    }
    
    if(err = vp_enter(sdk, Worldname))
    {
        printf("Couldn't enter world(reason %d)", err);
        return 1;
    }
    
    vp_event_set(sdk, VP_EVENT_AVATAR_ADD, event_avatar_add);
    
    vp_state_change(sdk);
    
    while(vp_wait(sdk, 1000) == 0){}
    return 0;
}

void event_avatar_add(VPInstance sdk)
{
    char message[300];
    sprintf((char*)&message, "Hello, %s!", vp_string(sdk, VP_AVATAR_NAME));
    vp_say(sdk, (char*)&message);
}

C#

Requirements for building and running the code below:

  • C# console application project (at least .NET Framework 4.6.1 or .NET Core 2.0)
  • Reference to the VpNet NuGet package (version 1.0.9.2960-alpha or later)
  • vpsdk.dll present in the output folder, this can be downloaded from the developer downloads page
  • Language version for your project set to at least C# 7.1 (In Visual Studio: Project Properties ▶ Build ▶ Advanced... ▶ Language Version)
using System.Threading.Tasks;
using VpNet;

namespace GreeterBot
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var instance = new Instance();

            // Register the events you wish to capture (such as avatar entry)
            instance.OnAvatarEnter += Instance_OnAvatarEnter;
            instance.OnAvatarLeave += Instance_OnAvatarLeave;

            await instance.ConnectAsync();
            await instance.LoginAsync("Username", "Password", "MyBotName");
            await instance.EnterAsync("MyWorld");

            // Announce the position in the world, this allows the bot to receive avatar and chat events
            instance.UpdateAvatar(0.0f, 0.0f, 0.0f);

            // Wait forever
            while (true)
            {
                await Task.Delay(System.TimeSpan.FromMilliseconds(int.MaxValue));
            }
        }

        private static void Instance_OnAvatarEnter(Instance sender, AvatarEnterEventArgsT<Avatar<Vector3>, Vector3> args)
        {
            sender.Say($"Hello {args.Avatar.Name}");
        }

        private static void Instance_OnAvatarLeave(Instance sender, AvatarLeaveEventArgsT<Avatar<Vector3>, Vector3> args)
        {
            sender.Say($"Goodbye {args.Avatar.Name}");
        }
    }
}

VB .Net

Imports VpNet

Module ExampleBot
    Public VPInstance As Instance

    Sub Main()
        Try
            'Initialize the Virtual Paradise .Net SDK
            VPInstance = New Instance()

            'Register the events you wish to capture (Such as avatar entry)
            'This must be done AFTER the initialization, but before connection
            AddHandler VPInstance.OnAvatarEnter, AddressOf EventAvatarEnter
            AddHandler VPInstance.OnAvatarLeave, AddressOf EventAvatarLeave

            'Connect to the universe using the default server settings
            VPInstance.Connect()

            'Login to the universe using your username, password,
            'and the desired bot name
            VPInstance.Login("Username", "Password", "MyBotName")

            'Enter the world
            VPInstance.Enter("MyWorld")

            'Set the bot's position in the world 
            VPInstance.UpdateAvatar(0.0F, 0.0F, 0.0F)

            'Write success message to the console
            Console.WriteLine("Login success!")

            'In order to process any pending network events and data
            'we must repeatedly call the Wait() function
            'Here we use a wait of 50 Miliseconds
            While True
                VPInstance.Wait(50)
            End While

            'This allows us to cleanly catch and handle 
            'any errors the SDK throws. In normal usage,
            'you should handle specific errors, and not
            'use a catch-all like the one below.
        Catch ex As VpException
            Console.WriteLine("{0}", ex.Message)
        End Try

        'This pauses the console on crash, so that
        'you can review the error thrown by the SDK
        Console.Read()
    End Sub

    Private Sub EventAvatarEnter(sender As Instance, args As AvatarEnterEventArgsT(Of Avatar(Of Vector3), Vector3))
        'A user has entered the world (or the bot's range)
        'We print the user's name followed by a brief message
        VPInstance.Say(args.Avatar.Name & " Entered")
    End Sub

    Private Sub EventAvatarLeave(sender As Instance, ByVal args As AvatarLeaveEventArgsT(Of Avatar(Of Vector3), Vector3))
        'A user has left the world (or the bot's range)
        'We print the user's name followed by a brief message
        VPInstance.Say(args.Avatar.Name & " Left")
    End Sub
End Module