View Single Post
Old 08-07-2008, 10:57 AM   #17 (permalink)
vermillion2083
Forum Expert
 
vermillion2083's Avatar
 
Join Date: Jun 2005
Location: Lansing, MI
Age: 25
Posts: 1,042
Send a message via ICQ to vermillion2083 Send a message via MSN to vermillion2083
Default

This create the dictionary itself. This dictionary stores a Mobile (the guild member) as the key, and an item (their robe) as the value.
Code:
		public static Dictionary<Mobile, Item> m_UmbarianRooster = new Dictionary<Mobile, Item>();
This method is simular to the one we made before, only now it is made to work with a dictionary, not a list. This will check to see if a person is already in the guild. If they are it will deny them membership, but if not it will add them, create a robe for them, and store the robe with their membership.
Code:
		public void AddGuildMember( Mobile from )
		{
			if( m_UmbarianRooster.ContainsKey( from ) ) 
			{
				from.SendMessage("you are alreaady a member");
				return;
			}

			else 
			{ 
				SpecialRobe robe = new SpecialRobe();
				from.AddToPack( robe );
     				m_UmbarianRooster.Add( from, robe ); 
			}
		}
This method again is simular to the one we made before, only now it works with dictionaries. When called it will check to see if the person is a guild member. If not it will inform them they arn't currently a member, but if they are a member it will search the world for their robe (no matter where it is) and delete it, then remove them from the guild rooster.
Code:
		public void RemoveGuildMember( Mobile from )
		{
			if( m_UmbarianRooster.ContainsKey( from ) ) 
			{
				SpecialRobe robe = m_UmbarianRooster[from];
				if( robe != null )
					robe.Delete();

     				m_UmbarianRooster.Remove( from );
			}

			else 
			{ 
				from.SendMessage("you are not currently a member");
				return;
			}
		}
This is how you serialize a dictionary. The first step is to determine if the list is polulated or not, if it isn't then it won't serialize anything, but if it is it will then save how many entries are in the list as an int (to be used by the deserialize). Once it does that it will go through each entry (for both the keys and values) and serialize them one by one.
Code:
		public override void Serialize( GenericWriter writer )
		{
			base.Serialize( writer );
			writer.Write( (int) 0 );

   			if( m_UmbarianRooster != null ) 
   			{ 
      				writer.Write( true ); 
      				writer.Write( m_UmbarianRooster.Count ); 

				foreach( KeyValuePair<Mobile, Item> kvp in m_UmbarianRooster ) 
				{ 
					writer.Write( kvp.Key ); 
 
					writer.Write( kvp.Value );
				}
			} 

			else 
			{ 
				writer.Write( false ); 
			} 
		}
This is the deserialize method for dictionaries. The first thing it does is decide weither or not the serialize saved the dictionary or not (depending on if it was empty during the serialize). If the list is populated it will then read the int that we saved as step two above. It then uses that into to determine how many times to cycle through the saved info and pull out each key and value we saved above. It will then cycle through the save files, deserialize each key and value, and then re add them back to the dictionary.
Code:
		public override void Deserialize( GenericReader reader ) 
		{ 
			base.Deserialize( reader ); 
 
			int version = reader.ReadInt(); 
 
			switch( version ) 
			{ 
				case 0: 
				{ 
					bool notNull = reader.ReadBool(); 
 
					if( notNull ) 
					{
						int tableSize = reader.ReadInt();
						for( int i = 0; i < tableSize; i++ ) 
						{ 
							Mobile m = reader.ReadMobile();
							Item item = reader.ReadItem(); // This might be wrong, can't remember how to deserialize an item *grins* mental block!
							m_UmbarianRooster.Add( m, item );
						} 
					}
				}
			}
		}
And there you have it! This should be functional, or atleast very close to it. Look this over and see if this is along the lines of what you want. if so we will move on from there. Now Im off to read your stone code and see what you are up to!
__________________
Father Time
Life's journey is not to arrive at the grave safely in a well preserved body, but rather to skid in sideways, totally worn out, shouting "Holy Hell......What a ride!!!"

Server: UO: Extinction
ICQ: 146563794
FatherTime@UOExtinction.com
UO: Extinction homepage
UO: Extinction forum
vermillion2083 is offline   Reply With Quote