Simple greeter bot: Difference between revisions

From Virtual Paradise Wiki
Jump to navigation Jump to search
Chris (talk | contribs)
No edit summary
Aerizeon (talk | contribs)
mNo edit summary
Line 72: Line 72:
using VpNet.Core.Structs;
using VpNet.Core.Structs;


namespace VPSDKExample
class ExampleBot
{
{
     class Program
     //Virtual Paradise SDK Instance
    public static Instance VPInstance;
 
    static void Main(string[] args)
     {
     {
         //Virtual Paradise SDK Instance
         try
        public static Instance VPInstance;
 
        static void Main(string[] args)
         {
         {
             try
             //Initalize the Virtual Paradise .Net SDK
            {
            VPInstance = 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
            //This must be done AFTER the initalization, but before connection
                VPInstance.EventAvatarAdd += EventAvatarAdd;
            VPInstance.EventAvatarAdd += EventAvatarAdd;
                VPInstance.EventAvatarDelete += EventAvatarDelete;
            VPInstance.EventAvatarDelete += EventAvatarDelete;


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


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


                //Enter the world
            //Enter the world
                VPInstance.Enter("MyWorld");
            VPInstance.Enter("MyWorld");


                //Set the bot's position in the world  
            //Set the bot's position in the world  
                VPInstance.UpdateAvatar(0.0f, 0.0f, 0.0f);
            VPInstance.UpdateAvatar(0.0f, 0.0f, 0.0f);


                //in order to proccess any pending network events and data
            //in order to proccess any pending network events and data
                //we must repeatedly call the Wait() function
            //we must repeatedly call the Wait() function
                //Here we use a wait of 50 Miliseconds
            //Here we use a wait of 50 Miliseconds
                while (true)
            while (true)
                {
                    VPInstance.Wait(50);
                }
            }
 
                //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 (VpException ex)
             {
             {
                 Console.WriteLine("{0}", ex.Message);
                 VPInstance.Wait(50);
             }
             }
        }


            //This pauses the console on crash, so that
        //This allows us to cleanly catch and handle
            //you can review the error thrown by the SDK
        //any errors the SDK throws. In normal usage,
            Console.Read();
        //you should handle specific errors, and not
         }
        //use a catch-all like the one below.
        static void EventAvatarAdd(Instance Sender, Avatar AvatarData)
         catch (VpException ex)
         {
         {
             //A user has entered the world (or the bot's range)
             Console.WriteLine("{0}", ex.Message);
            //We print the user's name followed by a breif message
            VPInstance.Say(AvatarData.Name + " Entered");
         }
         }


         static void EventAvatarDelete(Instance Sender, Avatar AvatarData)
         //This pauses the console on crash, so that
        {
        //you can review the error thrown by the SDK
            //A user has left the world (or the bot's range)
        Console.Read();
            //We print the user's name followed by a breif message
    }
            VPInstance.Say(AvatarData.Name + " Left");
    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");
    }
 
    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");
     }
     }
}
}
Line 144: Line 141:
==VB .Net==
==VB .Net==
<VBNet>
<VBNet>
Imports VpNet
Imports VpNet.Core
Imports VpNet.Core
Imports VpNet.NativeApi
Imports VpNet.Core.EventData
Imports VpNet.Core.Structs


Module Module1
Module ExampleBot
     'Virtual Paradise SDK Instance
     'Virtual Paradise SDK Instance
     Public VPInstance As VpNet.Core.Instance
     Public VPInstance As Instance


     Sub Main(ByVal args As String())
     Sub Main(ByVal args As String())
Line 192: Line 189:
             Console.WriteLine("{0}", ex.Message)
             Console.WriteLine("{0}", ex.Message)
         End Try
         End Try


         'This pauses the console on crash, so that
         'This pauses the console on crash, so that
Line 199: Line 194:
         Console.Read()
         Console.Read()
     End Sub
     End Sub
     Private Sub EventAvatarAdd(ByVal Sender As Instance, ByVal AvatarData As VpNet.Core.Structs.Avatar)
     Private Sub EventAvatarAdd(ByVal Sender As Instance, ByVal AvatarData As Avatar)
         'A user has entered the world (or the bot's range)
         'A user has entered the world (or the bot's range)
         'We print the user's name followed by a breif message
         'We print the user's name followed by a breif message
         VPInstance.Say(Convert.ToString(AvatarData.Name) & " Entered")
         VPInstance.Say(AvatarData.Name & " Entered")
     End Sub
     End Sub


     Private Sub EventAvatarDelete(ByVal Sender As Instance, ByVal AvatarData As VpNet.Core.Structs.Avatar)
     Private Sub EventAvatarDelete(ByVal Sender As Instance, ByVal AvatarData As Avatar)
         'A user has left the world (or the bot's range)
         'A user has left the world (or the bot's range)
         'We print the user's name followed by a breif message
         'We print the user's name followed by a breif message
         VPInstance.Say(Convert.ToString(AvatarData.Name) & " Left")
         VPInstance.Say(AvatarData.Name & " Left")
     End Sub
     End Sub
End Module
End Module
</VBNet>
</VBNet>
[[Category: Bots]]
[[Category: Bots]]
[[Category: SDK]]
[[Category: SDK]]

Revision as of 23:36, 23 November 2012

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

<C>

  1. include <vpsdk/VP.h>
  2. include <stdio.h>
  3. include <stdlib.h>
  1. define Username "username"
  2. define Password "password"
  3. define Botname "greeterbot"
  4. define Worldname "VP-Gate"

void event_avatar_add(VPInstance sdk);

int main(int argc, char ** argv) {

   int err;
   if(err = vp_init(VPSDK_VERSION))
   {
       printf("Couldn't initialize VP API(reason %d)", err);
       return 1;
   }
   VPInstance sdk;
   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[100];
   sprintf((char*)&message, "Hello, %s!", vp_string(sdk, VP_AVATAR_NAME));
   vp_say(sdk, (char*)&message);

} </C>

C#

In order to use the Virtual Paradise .Net SDK you must:

<CSharp> using System; using VpNet.Core; using VpNet.Core.EventData; using VpNet.Core.Structs;

class ExampleBot {

   //Virtual Paradise SDK Instance
   public static Instance VPInstance;
   static void Main(string[] args)
   {
       try
       {
           //Initalize 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 initalization, but before connection
           VPInstance.EventAvatarAdd += EventAvatarAdd;
           VPInstance.EventAvatarDelete += EventAvatarDelete;
           //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);
           //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);
           }
       }
       //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 (VpException ex)
       {
           Console.WriteLine("{0}", ex.Message);
       }
       //This pauses the console on crash, so that
       //you can review the error thrown by the SDK
       Console.Read();
   }
   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");
   }
   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

<VBNet> Imports VpNet.Core Imports VpNet.Core.EventData Imports VpNet.Core.Structs

Module ExampleBot

   'Virtual Paradise SDK Instance
   Public VPInstance As Instance
   Sub Main(ByVal args As String())
       Try
           'Initalize 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 initalization, but before connection
           AddHandler VPInstance.EventAvatarAdd, AddressOf EventAvatarAdd
           AddHandler VPInstance.EventAvatarDelete, AddressOf EventAvatarDelete
           '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 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
   Private Sub EventAvatarDelete(ByVal Sender As Instance, ByVal AvatarData As Avatar)
       '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")
   End Sub

End Module </VBNet>