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 09-25-2003, 01:04 PM   #26 (permalink)
Account Terminated
 
Join Date: Sep 2002
Age: 26
Posts: 3,846
Send a message via ICQ to Phantom Send a message via AIM to Phantom Send a message via MSN to Phantom
Default

Quote:
Originally Posted by XxSP1DERxX
Can someone make a serialization and deserialization method for commands, or regions for example, so we do not require putting an item or mobile in the internal map?
Of course, I am doing that for the Factions I am writting.

Its not easy, so if you don't know C# you might not be able to do it.
Phantom is offline   Reply With Quote
Old 01-04-2004, 08:05 PM   #27 (permalink)
Seven
Guest
 
Posts: n/a
Default

Ok...I finally choose to read through this because im always having troubles having to restart my playerbase whe i add a new seralize desearlize. So..i must not be doing it correctly. How exactly are you suppose to do it for the playermobile.cs what i read here looks like for items...
  Reply With Quote
Old 01-17-2004, 11:02 PM   #28 (permalink)
Forum Expert
 
Join Date: Oct 2003
Location: Calhoun, Ga
Age: 43
Posts: 985
Send a message via AIM to roadmaster
Default Serialize / Deserialize

I have a Item that once equiped by a Mobile, starts a timer to do a check of the lightlevels.

so, how do i serialize this?

do i have to serialize the mobile, the item, and the timer? or just one or two of them?

and other than string and number fields how would you serialize and deserialize your data?

ie... if i have to serialize the timer how do i do it?

would i just simply add the timer start to serialize and deserialize, like so

[code:1]
m_DrowLightCheckTimer = new DrowLightCheckTimer( m, this );
m_DrowLightCheckTimer.Start();
[/code:1]

or is there something more complicated to it?

I tried to serialize my item but it threw a compile error that i "used a class where a variable was expected".

[code:1]
writer.Write(DrowShadowTunic);
[/code:1]

so i instead tried a reference to that item:

[code:1]
writer.Write(m_DrowShadowTunic);
[/code:1]

it compiled fine but crashed and threw an exception Due to Bad Serialize.
Maybe the bad serialize is due to the fact that the variable m_DrowShadowTunic is not in the Deserialize method, my question there would be how would you deserialize a reference to an item? or can you serialize and deserialize a reference to an item.

I tried using:

[code:1]
m_DrowShadowTunic = reader.ReadItem();
[/code:1]

but it shot me the error

Quote:
Cannot implicitly convert type 'Server.Item' to 'Server.Items.DrowShadowTunic'
I am trying to learn these two methods but i just dont understand them completely yet.

I appreciate any help that anyone may give.

roadmaster
roadmaster is offline   Reply With Quote
Old 02-03-2004, 04:06 AM   #29 (permalink)
 
Join Date: Jan 2003
Posts: 314
Send a message via ICQ to juani Send a message via AIM to juani Send a message via MSN to juani
Default

maybe it would help i you understood what serialize does in low level...
as far as i understand serializing converts something into a series of 0s and 1s, so for example if you serialize the int 3 you would get something like 00000000000000000000000000000011 in memory/hard disk (wich is 3 in binary in 32 bit form).

when u serialize a 3 and a 1 u get:
00000000000000000000000000000011 (3)
00000000000000000000000000000001 (1) etc.

Now, we give the writer the int parameter so he knows in how many bytes it should write the info (integers have 32 bits i believe, thats why theres so many 0s), if instead of writer.write((int) 3). we put writer.write((byte) 3) (bytes have 8 bits instead of 32) you would get something like:
00000011 (3 in binary format in 8 bits)

thats for the writer.

the problem for the reader is that it doesnt really knows if with:
00000000000000000000000000000011
you meant 1 32 bit integer value of 3,
or 4 8 bit "byte" values (0,0,0,3).

Thats why we specify the type when we read.


trying to write or read directly a deathshroud for example is a problem because the writer doent knows how to convert it to binary, and the reader doenst knows how to read it.
you need to serialize each property 1 at a time making sure u read the same type of info in the same order, after all for the server your worldfile looks like: 10010100100010101101001001011101001001001000100001 001100100100
:D

theres another way that involves letting c# convert the WHOLE object to memory in binary format and retrieve it and put it in memory "as it is" when deserialized... thats how its done mostly on normal programming, but for some reason its not used a lot for scripts.


Hope it helps!
if any of that is wrong i apologize, thats how i understand it.
juani is offline   Reply With Quote
Old 12-11-2007, 11:17 PM   #30 (permalink)
Newbie
 
Join Date: Nov 2007
Posts: 52
Default

Quote:
Originally Posted by krrios View Post
To save and load the world, RunUO uses serialization. Every item or mobile you script is required to have three different methods associated with that.
...
Perhaps it should be mentioned as well that when you load an old version of an object you want to bring it up to date with the new version. If the version did not have certain variables, you must now set them. This is usually done by either letting them acquire default values (null in the case of objects, 0 in the case of numbers), by setting them in the Serial-constructor, or through explicit sets in the deserialize method. For instance, in the topic post, m_StringField2 is not set for version 0 and would therefor be null. Many times code is written where one assumes the objects are always non-null. If this was such as string, one could add m_StringField2 = "" to the Serial constructor which version 1 loads simply overwrites.

Some more things that should perhaps be noted: object refences are done by serial, serialization methods called for ancestors, type-handling in saves, and saving for custom [static/non-static] non-mobile non-item non-guild objects.
Dalliance is offline   Reply With Quote
Old 04-16-2008, 11:40 AM   #31 (permalink)
Newbie
 
i_am_neo's Avatar
 
Join Date: Jan 2008
Location: Warsaw, Poland
Age: 21
Posts: 97
Send a message via MSN to i_am_neo
Default Good to know...

Since I haven't found it explained thoroughly in here and it might be really useful for new scripters...

If you just add some variables in the serialize and deserialize methods, the server still might not see the variables' values on existing mobiles / items / whatever and might want to delete them.

There's a simple solution to that.

If you have added new variables in the Serialize and Deserialize methods
eg.
Code:
(...)
public override void Deserialize( GenericReader reader )
{
base.Deserialize( reader );
int version = reader.ReadInt();
 
 
switch ( version )
{
 
#region MyRegion
case 26:
{
var1 = reader.ReadLong();
var2 = reader.ReadInt();
var3 = reader.ReadString();
gotocase 25;
}
#endregion
 
(...)
 
public override void Serialize( GenericWriter writer )
{
 
(...)
 
writer.Write( (int) 26 ); // VERSION!
 
 
 
#region MyRegion
writer.Write( (long)var1 );
writer.Write( (int)var2 );
writer.Write( (string)var3 );
#endregion
 
(...)
You should make sure the script doesn't lead to an error when an object without those properties (old object) is loaded.
An easy way to do that supposing you increased the version number when adding the new properties would be to those lines:
Code:
#region MyRegion
if (version < 26)
{
var1 = [value 1];
var2 = [value 2];
var3 = "[value3]";
}
#endregion
in the Deserialize method right after the end of the switch function:
Code:
case 0:
{
break;
}
}
That should prevent the server from throwing any errors and deleting existing objects.

[Post inspired by Ki Frost]

Last edited by i_am_neo; 04-16-2008 at 05:23 PM.
i_am_neo is offline   Reply With Quote
Old 07-26-2008, 07:42 PM   #32 (permalink)
Newbie
 
Join Date: Mar 2008
Age: 23
Posts: 36
Default Patching scripts without versions to add new variables.

I've seen this issue time and time again. The solution that most people respond with is, "You have to wipe your server or old items". Well here is a very simple method to update items, that never included a "version" variable.

This will work anytime you need to update a script that does not already have a "Version Variable".
In this example, we will also demonstrate adding a "Version Variable" for easier future updates.


So, lets take a look at the code.
Serialize = "Writes the data"
Deserialize = "Reads the data"

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

		public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );
		}
The reason we get an error and have to delete our objests is because, its trying to "Load" data that doesn't exist.

So How do we fix this?
Simple, we update the data it's trying to "load" before it loads the new format.

How is this done?
- Create the Serialize method that you want to "upgrade to".

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

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

			writer.Write( ContainerName );
		}
- Leave the Deserialize method the same, so it can load our exsiting data.
- Launch/Restart your server.
- It should load fine, once it's up - Shutdown w/save
- Make the changes to the Deserialize method, to support your new data structure.
Code:
public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			switch (version)
			{
				case 1: { ContainerName = reader.ReadString(); } goto case 0;
				case 0: {  } break;
			}
		}
- Launch your server again
- Done You now have a working upgrade to your script.

Last edited by Moroth; 07-27-2008 at 01:40 PM.
Moroth is offline   Reply With Quote
Old 07-27-2008, 10:56 AM   #33 (permalink)
Forum Expert
 
Vorspire's Avatar
 
Join Date: Jan 2005
Location: Newcastle, United Kingdom
Age: 21
Posts: 2,294
Send a message via ICQ to Vorspire Send a message via MSN to Vorspire Send a message via Skype™ to Vorspire
Default

TIP: Where-ever possible, you should use the 'switch' statement to control your versions.
Code:
public override void Deserialize( GenericReader reader )
		{
			base.Deserialize( reader );

			int version = reader.ReadInt();

			switch (version)
			{
				case 1: { ContainerName = reader.ReadString(); } goto case 0;
				case 0: {  } break;
			}
		}
__________________

RPK.VORSPIRE.COM - The WoW-UO Cross-Over Shard
Vorspire is offline   Reply With Quote
Old 08-02-2008, 04:00 PM   #34 (permalink)
Lurker
 
Corvicus's Avatar
 
Join Date: Dec 2005
Location: Québec city
Age: 26
Posts: 3
Send a message via MSN to Corvicus
Default

Hahaha... I go tru this thread and feel like in a movie. I knew what gonna appen... Hahaha... I had the response for this and keep chalenging me to not write the response before i'd read all the thread... Hahaha I like it.

Moroth you got it ... hahaha . That is simple. I've been using this tecnic for a while. And after try some other way... This is the right one ... the only problem whit this is you have to restart the server two time instead of once but... is that realy matter? No
Corvicus 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