Greeterbot example in C#

Ask questions about bots programming, discuss bot programming or just show off your bot. Topics are not required to be about bots for VP.
Post Reply
Edwin
Site Admin
Posts: 163
Joined: Mon Apr 09, 2007 10:04 am
Location: Almere, The Netherlands
Contact:

Greeterbot example in C#

Post by Edwin »

This simple greeterbot says hello to everyone who comes in :)
It is written in C#, you can download it at: http://www.virtualparadise.org/download ... net_r1.zip.

Code: Select all

using System;
 
namespace testbot
{
	class MainClass
	{
		const string botname = "C# bot";
		static string username;
		static string password;
		public static void Main(string[] args)
		{
			//Create a new VP SDK instance
			VP.Instance bot = new VP.Instance();
 
			//Set events
			bot.EventAvatarAdd += event_avatar_add;
			bot.EventChat += event_chat;
 
			Console.Write("Username: ");
			username = Console.ReadLine();
			Console.Write("Password: ");
			password = Console.ReadLine();
 
			int rc = 0;
 
			//Connect to the universe server
			if((rc = bot.Connect("virtualparadise.gotdns.com", 57000)) > 0)
			{
				Console.WriteLine("Error: {0}({1})",(VP.ReasonCode)rc, rc);
				Console.ReadLine();
				return;
			}
 
			//Log in using your username and password
			if((rc = bot.Login(username, password, botname)) > 0)
			{
				Console.WriteLine("Error: {0}({1})",(VP.ReasonCode)rc, rc);
				Console.ReadLine();
				return;
			}
 
			//Enter the world
			if((rc = bot.Enter("VP-Build")) > 0)
			{
				Console.WriteLine("Error: {0}({1})",(VP.ReasonCode)rc, rc);
				Console.ReadLine();
				return;
			}
 
			//Position the avatar
			bot.X = 1;
			bot.Z = 2;
			bot.State_Change();
 
			while(true)
			{
				if((rc = bot.Wait(100)) > 0)
				{
					Console.WriteLine("Error: {0}({1})",(VP.ReasonCode)rc, rc);
					Console.ReadLine();
					return;
				}
			}
 
		}
 
		public static void event_avatar_add(VP.Instance sender, object data)
		{
			VP.AvatarData avatar = (VP.AvatarData)data;
			sender.Say("Hello "+avatar.Name);
		}
 
		public static void event_chat(VP.Instance sender, object data)
		{
			VP.ChatMessage message = (VP.ChatMessage)data;
			Console.WriteLine("{0}:\t{1}", message.User, message.Text);
		}
	}
}
Xenion
Moderator
Posts: 49
Joined: Sun Apr 15, 2007 7:38 pm
Location: Belgium
Contact:

Re: Greeterbot example in C#

Post by Xenion »

Looks nice, but it would be even more cool if the vp sdk could work exactly the same as the aw_version so we can easily convert existing bots.
Edwin
Site Admin
Posts: 163
Joined: Mon Apr 09, 2007 10:04 am
Location: Almere, The Netherlands
Contact:

Re: Greeterbot example in C#

Post by Edwin »

Xenion wrote:Looks nice, but it would be even more cool if the vp sdk could work exactly the same as the aw_version so we can easily convert existing bots.
The C API is like that, the wrapper isn't. But do you really like that better? I think this fits C# and .Net much better.
Xenion
Moderator
Posts: 49
Joined: Sun Apr 15, 2007 7:38 pm
Location: Belgium
Contact:

Re: Greeterbot example in C#

Post by Xenion »

Edwin wrote:
Xenion wrote:Looks nice, but it would be even more cool if the vp sdk could work exactly the same as the aw_version so we can easily convert existing bots.
The C API is like that, the wrapper isn't. But do you really like that better? I think this fits C# and .Net much better.
For .NET it's oké, there are not many bots for .NET now, so we'ell just get used to the new wrapper. But for C (or a VB Wrapper if possible), it would be handy to just be able to replace AW_ by VP_
Edwin
Site Admin
Posts: 163
Joined: Mon Apr 09, 2007 10:04 am
Location: Almere, The Netherlands
Contact:

Re: Greeterbot example in C#

Post by Edwin »

Xenion wrote:
Edwin wrote:
Xenion wrote:Looks nice, but it would be even more cool if the vp sdk could work exactly the same as the aw_version so we can easily convert existing bots.
The C API is like that, the wrapper isn't. But do you really like that better? I think this fits C# and .Net much better.
For .NET it's oké, there are not many bots for .NET now, so we'ell just get used to the new wrapper. But for C (or a VB Wrapper if possible), it would be handy to just be able to replace AW_ by VP_
Yes, it would be handy. But with that you also copy what I think is a major weakness of the AW sdk. That weakness is that AW uses a global instance. This means you can only use one instance at a time, making threading impossible. With VP you specify the instance with every call. Because of this you might have to write a little more code but it's a lot more flexible. There's a greeterbot example here: http://www.virtualparadise.org/wiki/ind ... reeter_bot. If you want, I can send you the files to develop using the C SDK.
Xenion
Moderator
Posts: 49
Joined: Sun Apr 15, 2007 7:38 pm
Location: Belgium
Contact:

Re: Greeterbot example in C#

Post by Xenion »

You can switch instances in AW to. I never really used that but you have aw_instance_set
Edwin
Site Admin
Posts: 163
Joined: Mon Apr 09, 2007 10:04 am
Location: Almere, The Netherlands
Contact:

Re: Greeterbot example in C#

Post by Edwin »

Xenion wrote:You can switch instances in AW to. I never really used that but you have aw_instance_set
I know it's possible, I didn't say it's not. What I said is that AW uses a global instance. that means if you call aw_instance_set, it affects the whole program. This means you can only use the AW SDK on one thread. With VP there is no global instance, it's passed to every function you call. Multiple threads can use the VP SDK at the same time with different instances without having to lock all other threads from changing the instance.

Say I have two AW bots running on different threads and one is programmed to say something, one bot sets the instance and tries to say something. Just before calling aw_say, the other bot changes the instance and aw_say is called on the wrong instance. This can be solved by locking other threads but this will probably affect performance because the other threads will just be waiting for the SDK to be available again.

With the VP SDK the instance is passed to every function except vp_init and vp_create like this:
vp_say(VPInstance instance, const char * message)

I hope this clears things up :)
Xenion
Moderator
Posts: 49
Joined: Sun Apr 15, 2007 7:38 pm
Location: Belgium
Contact:

Re: Greeterbot example in C#

Post by Xenion »

Ah yes, I see it now.

But could you perhaps make the Instance an optional parameter so if you don't specify it, it just takes a default instance?
Edwin
Site Admin
Posts: 163
Joined: Mon Apr 09, 2007 10:04 am
Location: Almere, The Netherlands
Contact:

Re: Greeterbot example in C#

Post by Edwin »

Xenion wrote:Ah yes, I see it now.

But could you perhaps make the Instance an optional parameter so if you don't specify it, it just takes a default instance?
Sorry, no. C doesn't do optional arguments and the instance is always the first parameter so that wouldn't work anyway.
Xenion
Moderator
Posts: 49
Joined: Sun Apr 15, 2007 7:38 pm
Location: Belgium
Contact:

Re: Greeterbot example in C#

Post by Xenion »

Edwin wrote:
Xenion wrote:Ah yes, I see it now.

But could you perhaps make the Instance an optional parameter so if you don't specify it, it just takes a default instance?
Sorry, no. C doesn't do optional arguments and the instance is always the first parameter so that wouldn't work anyway.
We'll just get used to it, I guess.
Edwin
Site Admin
Posts: 163
Joined: Mon Apr 09, 2007 10:04 am
Location: Almere, The Netherlands
Contact:

Re: Greeterbot example in C#

Post by Edwin »

Xenion wrote:
Edwin wrote:
Xenion wrote:Ah yes, I see it now.

But could you perhaps make the Instance an optional parameter so if you don't specify it, it just takes a default instance?
Sorry, no. C doesn't do optional arguments and the instance is always the first parameter so that wouldn't work anyway.
We'll just get used to it, I guess.
Or you could write a wrapper that uses a global instance... :mrgreen: :wink:
Post Reply