Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-13-2007, 06:23 AM
killerogue killerogue is offline
Registered Omega
killerogue's Avatar
Join Date: Apr 2006
Location: United States
Posts: 1,920
killerogue is on a distinguished road
Send a message via AIM to killerogue Send a message via MSN to killerogue
Serializing Objects

Earlier, I was questioning Inverness concerning a few things and also leaning a few things from him concerning a system I wanted to create for a project. He suggested I create a way to serialize and un-serialize objects into strings.

It will be useful for the system I'm planning and required a bit of tweaking, but I decided it could be somewhat useful for the public.

Use: These two functions are for turning the values of all the vars in an object into a long string. And vice versa.

Credits:To Inverness, for the idea as well as quite a bit of help with the upper function. (I.E. Basically giving me the answer , brain isn't functioning to well right now.)

PHP Code:
public function serialize(obj) { 
  
temp.string;
  
temp.var;
  
  for (var : 
obj.getDynamicVarNames()) {
    
string @= obj.(@ var) @ ":";
  }
  return 
string
}

public function 
deserialize(string) {
  
temp.array;
  
temp.i;
  
temp.sobj;
  
  if (
string.type() != 1
    return;
  
  array = 
string.tokenize(":");
  
  for (
0< array.size(); ++) {
    
makevar("sobj.var" i) = array[i];
  }
  return 
sobj;

__________________


REMEMBER, IF YOU REP ME, LEAVE A NAME!

Quote:
Originally Posted by haunter View Post
Graal admins don't die. They go to hell and regroup.
Quote:
Originally Posted by Inverness View Post
Without scripters, your graphics and levels wouldn't do anything but sit there and look pretty.
Reply With Quote
  #2  
Old 06-13-2007, 07:35 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
You need to do temp.varname = 0; not temp.varname; =P
And the serializing is supposed to be specific to that type of object >_>
Anyone could use savevarstoarray() to serialize any graal object.
__________________
Reply With Quote
  #3  
Old 06-13-2007, 12:19 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Thank god we all assume no strings EVER will use the ":" character.
Reply With Quote
  #4  
Old 06-13-2007, 01:07 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Ideally your serialised string will have no bias towards certain characters. Like Novo said, your script will probably break if a string is used that contains the : character. It's safe to assume that variable names won't contain colons, so that makes it easier to process variable names, however the script needs to more reliably read the value.

The way that PHP does it is that your serialised string not only has the name and value of the object, but it also has a length value for the value. That way, you do not have to depend upon the positions of : characters.

An example:
foo:3:b:r:bar:4:ZOOM
... This would make a variable "foo" with the value "b:r", and another called "bar" with the value "ZOOM", because it's been instructed to read the 3 characters following the first instruction, and 4 characters for the second one, regardless of the position of colons in the string.
__________________
Skyld
Reply With Quote
  #5  
Old 06-13-2007, 07:40 PM
Novo Novo is offline
[TServerDeveloper]
Join Date: Jun 2006
Posts: 448
Novo will become famous soon enough
Quote:
Originally Posted by Skyld View Post
Like Novo said, your script will probably break if a string is used that contains the : character.
WHAAA?! Now you're just putting words in my mouth! =]
Reply With Quote
  #6  
Old 06-14-2007, 05:59 PM
killerogue killerogue is offline
Registered Omega
killerogue's Avatar
Join Date: Apr 2006
Location: United States
Posts: 1,920
killerogue is on a distinguished road
Send a message via AIM to killerogue Send a message via MSN to killerogue
Well, it's been working so far on my end without any breakage Skyld. :O
__________________


REMEMBER, IF YOU REP ME, LEAVE A NAME!

Quote:
Originally Posted by haunter View Post
Graal admins don't die. They go to hell and regroup.
Quote:
Originally Posted by Inverness View Post
Without scripters, your graphics and levels wouldn't do anything but sit there and look pretty.
Reply With Quote
  #7  
Old 06-14-2007, 06:19 PM
Skyld Skyld is offline
Script-fu
Skyld's Avatar
Join Date: Jan 2002
Location: United Kingdom
Posts: 3,914
Skyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud ofSkyld has much to be proud of
Send a message via AIM to Skyld
Quote:
Originally Posted by killerogue View Post
Well, it's been working so far on my end without any breakage Skyld. :O
I mean if you put a colon in one of your string values. It'll probably take it as a delimiter instead and cut off the string.
__________________
Skyld
Reply With Quote
  #8  
Old 06-15-2007, 02:25 AM
Inverness Inverness is offline
Incubator
Inverness's Avatar
Join Date: Aug 2004
Location: Houston, Texas
Posts: 3,613
Inverness is a jewel in the roughInverness is a jewel in the rough
Quote:
Originally Posted by Skyld View Post
<snip>
Thats a nice idea, I'll be sure to implement it if I ever serialize something that could have a colon in it.
Quote:
Originally Posted by Novo View Post
Thank god we all assume no strings EVER will use the ":" character.
Your sarcasm is most likely unappreciated by Stan. HINT HINT
__________________
Reply With Quote
Reply


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

Forum Jump


All times are GMT +2. The time now is 05:57 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.