Go Back   RunUO - Ultima Online Emulation > RunUO > Script Support

Script Support Get support for modifying RunUO Scripts, or writing your own!

Reply
 
Thread Tools Display Modes
Old 12-29-2004, 02:01 PM   #1 (permalink)
Newbie
 
Join Date: Dec 2004
Age: 26
Posts: 18
Exclamation Serializing a Custom Class

Code:
private ColorTeam m_ColorTeam;
...//later in the seralize function
writer.Write( m_ColorTeam );
...//later in the deserialize function
m_ColorTeam = (ColorTeam)reader.ReadGuild();
ColorTeam is a class inherited from baseguild. I'm trying to find a way to serialize and deserialize it but this isn't working. This code compiles but there are errors when loading:

System.Exception: Load failed (items=False, mobiles=True, guilds=False, regions=False, type=Server.Mobiles.PlayerMobile, serial=0x00000001) ---> System.InvalidCastException: Specified Cast is not valid.
at Server.Mobiles.PlayerMobile.Deserialize(GenericRea der reader)

I would love to just make a custom ReadColorTeam function but I have nothing to base it on. I don't know what happens in a 'Read_____' function.
I Miss Hockey is offline   Reply With Quote
Old 12-29-2004, 02:12 PM   #2 (permalink)
P3'c Orion Aviator
 
Join Date: Sep 2004
Age: 30
Posts: 1,272
Default

Read is the load, and write is the save :"P

Look at this serialize example...
Code:
public class CharFlagStone : Item
	{
		private bool SomeVariable;

                                 //Constructor and stuff here
                                 //do some stuff 

		public CharFlagStone( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 ); // version
	
			writer.Write( (bool)SomeVariable );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			SomeVariable = reader.ReadBool();
		}
	}
Declare a variable
Pass variable through the writer/saver
Pass it to the Reader/loader

This way uses the serialization of its parent class.
sirens song is offline   Reply With Quote
Old 12-29-2004, 02:28 PM   #3 (permalink)
Newbie
 
Join Date: Dec 2004
Age: 26
Posts: 18
Default

My problem is when I am using the class as a property of other items/mobiles. I added it to PlayerMobile and thats where I'm getting the error. I'm not following if you are suggesting a solution to those errors.
I Miss Hockey is offline   Reply With Quote
Old 12-29-2004, 03:45 PM   #4 (permalink)
P3'c Orion Aviator
 
Join Date: Sep 2004
Age: 30
Posts: 1,272
Default

Maybe Im not following your question
sirens song is offline   Reply With Quote
Old 12-29-2004, 04:02 PM   #5 (permalink)
Forum Novice
 
Victor's Avatar
 
Join Date: Sep 2002
Posts: 238
Default

Take a look at this code from keyring I made. This has an expamle of what you are trying to do.

Code:
using System.Collections;
using Server.Network;
using Server.Targeting;
using Server.Prompts;

namespace Server.Items
{
	public class KeyInfo
	{
		private string m_Description;
		private uint m_KeyVal;
		private Item m_Link;
		private int m_MaxRange;
		private KeyType m_Type;

		public string Description{ get{ return m_Description; } }
		public int MaxRange{ get{ return m_MaxRange; } }
		public uint KeyValue{ get{ return m_KeyVal; } }
		public Item Link{ get{	return m_Link; } }
		public KeyType type{ get{ return m_Type; } }

		public KeyInfo( Key key )
		{
			m_KeyVal = key.KeyValue;
			m_Description = key.Description;
			m_MaxRange = key.MaxRange;
			m_Link = key.Link;
			m_Type = (KeyType)key.ItemID;
		}

		public KeyInfo( GenericReader reader )
		{
			int version = reader.ReadInt();

			m_KeyVal = reader.ReadUInt();
			m_Description = reader.ReadString();
			m_MaxRange = reader.ReadInt();
			m_Link = reader.ReadItem();
			m_Type = (KeyType)reader.ReadInt();
		}

		public void Serialize( GenericWriter writer )
		{
			writer.Write( (int) 0 ); // version

			writer.Write( (uint) m_KeyVal );
			writer.Write( (string) m_Description );
			writer.Write( (int) m_MaxRange );
			writer.Write( (Item) m_Link );
			writer.Write( (int) m_Type );
		}
	}

	public class KeyRing : Item
	{
		private ArrayList m_Keys;

		public ArrayList Keys{ get{ return m_Keys; } }

		[Constructable]
		public KeyRing() : base( 0x1011 )
		{
			m_Keys = new ArrayList();
		}

		public KeyRing( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 0 );

			writer.Write( (int) m_Keys.Count );

			foreach( KeyInfo key in m_Keys )
				key.Serialize( writer );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			m_Keys = new ArrayList();
			int KeyCount = reader.ReadInt();
			for ( int i = 0; i < KeyCount; i++ )
				m_Keys.Add( new KeyInfo( reader ) );
		}

		public override bool OnDragDrop( Mobile from, Item item )
		{
			base.OnDragDrop( from, item );

			if ( item is Key && ((Key)item).KeyValue != 0 )
			{
				if ( m_Keys.Count < 20 )
				{
					from.SendLocalizedMessage( 501691 ); // You put the key on the keyring.

					m_Keys.Add( new KeyInfo( (Key)item ) );
					item.Delete();

					if( m_Keys.Count > 0 && m_Keys.Count < 3 )
						ItemID = 0x1769;
					else if ( m_Keys.Count > 2 && m_Keys.Count < 5 )
						ItemID = 0x176A;
					else if ( m_Keys.Count > 4 )
						ItemID = 0x176B;
				}
				else
				{
					from.SendLocalizedMessage( 1008138 ); // This keyring is full.
				}
			}
			else
			{
				from.SendLocalizedMessage( 501689 ); // Only non-blank keys can be put on a keyring.
			}

			return false;
		}

		public override void OnDoubleClick( Mobile from )
		{
			from.SendLocalizedMessage( 501680 ); // What do you want to unlock?
			from.Target = new UnlockTarget( this );
		}

		private class UnlockTarget : Target
		{
			private KeyRing m_KeyRing;

			public UnlockTarget( KeyRing keyring ) : base( 10, false, TargetFlags.None )
			{
				m_KeyRing = keyring;
				CheckLOS = false;
			}

			protected override void OnTarget( Mobile from, object targeted )
			{
				if ( targeted == m_KeyRing )
				{
					from.SendLocalizedMessage( 501685 ); // You open the keyring.
					m_KeyRing.ItemID = 0x1011;

					foreach( KeyInfo k in m_KeyRing.Keys ) 
					{
						Key key = new Key( k.type, k.KeyValue, k.Link );
						key.Description = k.Description;
						from.AddToBackpack( key );
					}

					m_KeyRing.Keys.Clear();
				}
				else if ( targeted is ILockable )
				{
					ILockable o = (ILockable)targeted;
					bool KeyMatched = false;

					foreach( KeyInfo key in m_KeyRing.Keys ) 
					{
						if ( key.KeyValue == o.KeyValue )
						{
							KeyMatched = true;
							break;
						}
					}

					if ( KeyMatched )
					{
						if ( o is BaseDoor && !((BaseDoor)o).UseLocks() )
						{
							from.SendLocalizedMessage( 1008140 ); // You do not have a key for that.
						}
						else
						{
							o.Locked = !o.Locked;

							if ( o is LockableContainer )
							{
								LockableContainer cont = (LockableContainer)o;

								if ( cont.LockLevel == -255 )
									cont.LockLevel = cont.RequiredSkill - 10;
							}

							if ( targeted is Item )
							{
								Item item = (Item)targeted;

								if ( o.Locked )
									item.SendLocalizedMessageTo( from, 1048000 ); // You lock it
								else
									item.SendLocalizedMessageTo( from, 1048001 ); // You unlock it.
							}
						}
					}
					else
					{
						from.SendLocalizedMessage( 1008140 ); // You do not have a key for that.
					}
				}
				else
				{
					from.SendLocalizedMessage( 501666 ); // You can't unlock that!
				}
			}
		}
	}
Victor is offline   Reply With Quote
Old 12-29-2004, 04:07 PM   #6 (permalink)
Newbie
 
Join Date: Dec 2004
Age: 26
Posts: 18
Default

here is the Item BlueTeamBook that uses it s a property:
Code:
public class BlueTeamBook : Item
	{
		private ColorTeam m_ColorTeam;

		public ColorTeam ColorTeam
		{
			get
			{
				return m_ColorTeam;
			}
		}

		[Constructable]
		public BlueTeamBook() : base( 0xFBE )
		{
			m_ColorTeam = new ColorTeam( "Blue", "BLU" );
			Hue = 94;
			Name = "Blue Team Sign Up Book";
			Movable = false;
		}

		
		public BlueTeamBook( Serial serial ) : base( serial )
		{
		}

		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );

			writer.Write( (int) 1 ); // version

			writer.Write( m_ColorTeam );
		}

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			switch ( version )
			{
				case 1:
				{
					m_ColorTeam = (ColorTeam)reader.ReadGuild();
					break;
				}
			}
		}

		public override void OnDoubleClick( Mobile temp )
		{
			PlayerMobile from = (PlayerMobile)temp;
			if ( !from.InRange( GetWorldLocation(), 2 ) )
			{
				from.SendLocalizedMessage( 500446 ); // That is too far away.
			}
			else if ( from.ColorTeam == null )
			{
				ColorTeamsGump.EnsureClosed( from );
				from.SendGump( new ColorTeamsGump( from, m_ColorTeam ) );
			}
			else if ( from.ColorTeam == m_ColorTeam )
			{
				from.SendMessage("You are already on the Red Team");
			}
			else 
			{
				from.SendMessage("You are on another color team. You cannot join this one.");
			}
		}
	}
}
Notice i have to serialize a colorteam and then deserialize it when I'm using it as a property. This is where things screw up. I'm writing the colorteam and then trying to read it but there is no function in the reader that can read of class colorteam. I've tried casting it as a guild and as an int and as a string and it never works right.
I Miss Hockey is offline   Reply With Quote
Old 12-29-2004, 04:47 PM   #7 (permalink)
Newbie
 
Join Date: Dec 2004
Age: 26
Posts: 18
Default

Thanks Victor... thats what I needed to see. It worked after I adjusted a few things.
I Miss Hockey is offline   Reply With Quote
Old 12-29-2004, 05:46 PM   #8 (permalink)
Newbie
 
Join Date: Dec 2004
Age: 26
Posts: 18
Default

I think its going to be alittle more complicated. Does anyone have the ReadGuild code? I need to do something like that.
I Miss Hockey is offline   Reply With Quote
Old 12-29-2004, 10:29 PM   #9 (permalink)
Swashbuckling Ninja Pimp
 
Join Date: Jun 2004
Age: 31
Posts: 44
Default

Code:
m_ColorTeam=m_ColorTeam.Deserialize( reader );
That should work. Unless your custom class doesn't have a Deserialize method, or that just doesn't work. Then just add this constructor to your custom class:
Code:
public ColorTeam( /*add any other paramaters here that come with the regular constructor call,*/ Generic reader )
{
}
And then deserialize like this:
Code:
m_ColorTeam = new ColorTeam( */any other parameters here,*/ reader );
Caleb Darkmoon is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5