Simple greeter bot: Difference between revisions

From Virtual Paradise Wiki
Jump to navigation Jump to search
mNo edit summary
(Fix code to work with newer SDK)
 
(11 intermediate revisions by 3 users not shown)
Line 4: Line 4:
*Have Visual Studio, Visual C++ Express or another compatible C/C++ IDE+Compiler installed and [[developer environments#C|configured]]
*Have Visual Studio, Visual C++ Express or another compatible C/C++ IDE+Compiler installed and [[developer environments#C|configured]]
*Download the x86 SDK from the [http://dev.virtualparadise.org/downloads.php Developer Downloads] page
*Download the x86 SDK from the [http://dev.virtualparadise.org/downloads.php Developer Downloads] page
<C>
<syntaxhighlight lang="cpp">
#include <vpsdk/VP.h>
#include <VPSDK/VP.h>
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
Line 12: Line 12:
#define Password "password"
#define Password "password"
#define Botname  "greeterbot"
#define Botname  "greeterbot"
#define Worldname "VP-Gate"
#define Worldname "testworld"


void event_avatar_add(VPInstance sdk);
void event_avatar_add(VPInstance sdk);
Line 19: Line 19:
{
{
     int err;
     int err;
    VPInstance sdk;
     if(err = vp_init(VPSDK_VERSION))
     if(err = vp_init(VPSDK_VERSION))
     {
     {
Line 25: Line 27:
     }
     }


    VPInstance sdk;
     sdk = vp_create(NULL);
     sdk = vp_create();


     if(err = vp_connect_universe(sdk, "universe.virtualparadise.org", 57000))
     if(err = vp_connect_universe(sdk, "universe.virtualparadise.org", 57000))
Line 56: Line 57:
void event_avatar_add(VPInstance sdk)
void event_avatar_add(VPInstance sdk)
{
{
     char message[100];
     char message[300];
     sprintf((char*)&message, "Hello, %s!", vp_string(sdk, VP_AVATAR_NAME));
     sprintf((char*)&message, "Hello, %s!", vp_string(sdk, VP_AVATAR_NAME));
     vp_say(sdk, (char*)&message);
     vp_say(sdk, (char*)&message);
}
}
</C>
</syntaxhighlight>
==C#==
==C#==
In order to use the Virtual Paradise .Net SDK you must:
Requirements for building and running the code below:
*Have Visual Studio or Visual C# Express installed and [[developer environments#CSharp|configured]]
* C# console application project (at least .NET Framework 4.6.1 or .NET Core 2.0)
*Download the x86 SDK from the [http://dev.virtualparadise.org/downloads.php Developer Downloads] page
* Reference to the [https://www.nuget.org/packages/vpnet VpNet NuGet package] (version 1.0.9.2960-alpha or later)
*Download the .Net SDK from the [http://vpnet.codeplex.com/ VPNet SDK Download] page
* <code>vpsdk.dll</code> present in the output folder, this can be downloaded from the [https://dev.virtualparadise.org/downloads.php developer downloads page]
<CSharp>
* Language version for your project set to at least C# 7.1 (In Visual Studio: Project Properties ▶ Build ▶ Advanced... ▶ Language Version)
using System;
 
using VpNet.Core;
<syntaxhighlight lang="csharp">
using VpNet.Core.EventData;
using System.Threading.Tasks;
using VpNet.Core.Structs;
using VpNet;


class ExampleBot
namespace GreeterBot
{
{
     //Virtual Paradise SDK Instance
     class Program
    public static Instance VPInstance;
 
    static void Main(string[] args)
     {
     {
         try
         static async Task Main(string[] args)
         {
         {
             //Initalize the Virtual Paradise .Net SDK
             var instance = new Instance();
            VPInstance = new Instance();


             //Register the events you wish to capture (Such as avatar entry)
             // Register the events you wish to capture (such as avatar entry)
             //This must be done AFTER the initalization, but before connection
             instance.OnAvatarEnter += Instance_OnAvatarEnter;
            VPInstance.EventAvatarAdd += EventAvatarAdd;
             instance.OnAvatarLeave += Instance_OnAvatarLeave;
             VPInstance.EventAvatarDelete += EventAvatarDelete;


             //Connect to the universe using the default server settings
             await instance.ConnectAsync();
             VPInstance.Connect();
            await instance.LoginAsync("Username", "Password", "MyBotName");
             await instance.EnterAsync("MyWorld");


             //Login to the universe using your username, password,
             // Announce the position in the world, this allows the bot to receive avatar and chat events
            //and the desired bot name
             instance.UpdateAvatar(0.0f, 0.0f, 0.0f);
             VPInstance.Login("Username", "Password", "MyBotName");


             //Enter the world
             // Wait forever
            VPInstance.Enter("MyWorld");
 
            //Set the bot's position in the world
            VPInstance.UpdateAvatar(0.0f, 0.0f, 0.0f);
 
            //in order to proccess any pending network events and data
            //we must repeatedly call the Wait() function
            //Here we use a wait of 50 Miliseconds
             while (true)
             while (true)
             {
             {
                 VPInstance.Wait(50);
                 await Task.Delay(System.TimeSpan.FromMilliseconds(int.MaxValue));
             }
             }
         }
         }


         //This allows us to cleanly catch and handle
         private static void Instance_OnAvatarEnter(Instance sender, AvatarEnterEventArgsT<Avatar<Vector3>, Vector3> args)
        //any errors the SDK throws. In normal usage,
        //you should handle specific errors, and not
        //use a catch-all like the one below.
        catch (VpException ex)
         {
         {
             Console.WriteLine("{0}", ex.Message);
             sender.Say($"Hello {args.Avatar.Name}");
         }
         }


         //This pauses the console on crash, so that
         private static void Instance_OnAvatarLeave(Instance sender, AvatarLeaveEventArgsT<Avatar<Vector3>, Vector3> args)
        //you can review the error thrown by the SDK
        {
        Console.Read();
            sender.Say($"Goodbye {args.Avatar.Name}");
    }
        }
    static void EventAvatarAdd(Instance Sender, Avatar AvatarData)
    {
        //A user has entered the world (or the bot's range)
        //We print the user's name followed by a breif message
        VPInstance.Say(AvatarData.Name + " Entered");
     }
     }
}
</syntaxhighlight>


    static void EventAvatarDelete(Instance Sender, Avatar AvatarData)
    {
        //A user has left the world (or the bot's range)
        //We print the user's name followed by a breif message
        VPInstance.Say(AvatarData.Name + " Left");
    }
}
</CSharp>
==VB .Net==
==VB .Net==
<VBNet>
Requirements for building and running the code below:
Imports VpNet.Core
* VB console application project (at least .NET Framework 4.6.1 or .NET Core 2.0)
Imports VpNet.Core.EventData
* Reference to the [https://www.nuget.org/packages/vpnet VpNet NuGet package] (version 1.0.9.2960-alpha or later)
Imports VpNet.Core.Structs
* <code>vpsdk.dll</code> present in the output folder, this can be downloaded from the [https://dev.virtualparadise.org/downloads.php developer downloads page]
<syntaxhighlight lang="vbnet">
Imports VpNet


Module ExampleBot
Module Program
     'Virtual Paradise SDK Instance
     Sub Main(args As String())
     Public VPInstance As Instance
        MainAsync().GetAwaiter().GetResult()
     End Sub


     Sub Main(ByVal args As String())
     Async Function MainAsync() As Task
         Try
         Dim instance = New Instance
            'Initalize the Virtual Paradise .Net SDK
            VPInstance = New Instance()


            'Register the events you wish to capture (Such as avatar entry)
        ' Register the events you wish to capture (such as avatar entry)
            'This must be done AFTER the initalization, but before connection
        AddHandler instance.OnAvatarEnter, AddressOf Instance_OnAvatarEnter
            AddHandler VPInstance.EventAvatarAdd, AddressOf EventAvatarAdd
        AddHandler instance.OnAvatarLeave, AddressOf Instance_OnAvatarLeave
            AddHandler VPInstance.EventAvatarDelete, AddressOf EventAvatarDelete


            'Connect to the universe using the default server settings
        Await instance.ConnectAsync()
            VPInstance.Connect()
        Await instance.LoginAsync("Username", "Password", "MyBotName")
        Await instance.EnterAsync("MyWorld")


            'Login to the universe using your username, password,
        ' Announce the position in the world, this allows the bot to receive avatar and chat events
            'and the desired bot name
        instance.UpdateAvatar(0.0F, 0.0F, 0.0F)
            VPInstance.Login("Username", "Password", "MyBotName")


            'Enter the world
        ' Wait forever
             VPInstance.Enter("MyWorld")
        While True
             Await Task.Delay(System.TimeSpan.FromMilliseconds(Integer.MaxValue))
        End While
    End Function


            'Set the bot's position in the world
     Private Sub Instance_OnAvatarEnter(sender As Instance, args As AvatarEnterEventArgsT(Of Avatar(Of Vector3), Vector3))
            VPInstance.UpdateAvatar(0.0F, 0.0F, 0.0F)
         sender.Say($"Hello {args.Avatar.Name}")
 
            'Write success message to the console
            Console.WriteLine("Login success!")
 
            'In order to proccess 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 EventAvatarAdd(ByVal Sender As Instance, ByVal AvatarData As Avatar)
        'A user has entered the world (or the bot's range)
         'We print the user's name followed by a breif message
        VPInstance.Say(AvatarData.Name & " Entered")
     End Sub
     End Sub


     Private Sub EventAvatarDelete(ByVal Sender As Instance, ByVal AvatarData As Avatar)
     Private Sub Instance_OnAvatarLeave(sender As Instance, args As AvatarLeaveEventArgsT(Of Avatar(Of Vector3), Vector3))
        'A user has left the world (or the bot's range)
         sender.Say($"Goodbye {args.Avatar.Name}")
         'We print the user's name followed by a breif message
        VPInstance.Say(AvatarData.Name & " Left")
     End Sub
     End Sub
End Module
End Module
</VBNet>
</syntaxhighlight>
[[Category: Bots]]
[[Category: Bots]]
[[Category: SDK]]
[[Category: SDK]]

Latest revision as of 22:12, 30 July 2021

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(NULL);

    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

Requirements for building and running the code below:

  • VB 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
Imports VpNet

Module Program
    Sub Main(args As String())
        MainAsync().GetAwaiter().GetResult()
    End Sub

    Async Function MainAsync() As Task
        Dim instance = New Instance

        ' Register the events you wish to capture (such as avatar entry)
        AddHandler instance.OnAvatarEnter, AddressOf Instance_OnAvatarEnter
        AddHandler instance.OnAvatarLeave, AddressOf 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(Integer.MaxValue))
        End While
    End Function

    Private Sub Instance_OnAvatarEnter(sender As Instance, args As AvatarEnterEventArgsT(Of Avatar(Of Vector3), Vector3))
        sender.Say($"Hello {args.Avatar.Name}")
    End Sub

    Private Sub Instance_OnAvatarLeave(sender As Instance, args As AvatarLeaveEventArgsT(Of Avatar(Of Vector3), Vector3))
        sender.Say($"Goodbye {args.Avatar.Name}")
    End Sub
End Module