Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

class obj to string, and vice versa...

Ok, Here is what i am trying to do. Using the RGSS Web Kit, and some remote php scripts, I am trying to edit the scene save and load screens to save each object's data to a remote mySQL database. I think I know how to save the data. using obj.inspect, I can convert each object into a string... ie:

Code:
$gs_string = $game_system.inspect

p $gs_string #=> "#<Game_System:0x7ffc000 @opacity=255, @face_frame_width=100, @font="Arial", @face_graphic_justification=6, @axial_movement=false, @slave_windows={}, @name="", @train_actor=-1, @window_height=128, @wait=0, @magic_number=5944682, @message_position=2, @windowskin="", @show_pause=false, @shadow_color=(0.000000, 0.000000, 0.000000, 100.000000), @skip_mode=1, @timer_working=false, @animation_pause=80, @name_window=false, @face_graphic="", @default_movement=true, @map_interpreter=#<Interpreter:0x7ffbf88 @button_input_variable_id=0, @list=nil, @main=true, @parameters=[], @move_route_waiting=false, @index=4, @depth=0, @branch={}, @wait_count=0, @message_waiting=false, @child_interpreter=nil, @event_id=1, @loop_count=1, @map_id=1>, @shortcuts={}, @sound_effect="", @message_event=-1, @text_mode=0, @encounter_disabled=false, @resting_animation_pause=80, @text_justification=6, @shadowed_text=false, @ums_mode=0, @timer=0, @animated_faces=false, @comic_style=0, @window_justification=1, @save_count=1, @used_codes=["\\v", "\\n", "\\c", "\\g", "\\skip", "\\m", "\\height", "\\width", "\\jr", "\\jc", "\\jl", "\\face", "\\fl", "\\fr", "\\b", "\\i", "\\s", "\\e", "\\t1", "\\t2", "\\th", "\\nm", "\\font", "\\p", "\\w", "\\ws", "\\oa", "\\oi", "\\os", "\\ow", "\\tl", "\\tr", "\\tc", "\\ignr", "\\shk", "\\slv", "\\ind", "\\inc"], @text_shake=0, @choice_position=0, @write_speed=0, @menu_disabled=false, @back_opacity=160, @resting_face="", @font_color=nil, @face_graphic_position=1, @numpad_movement=false, @battle_interpreter=#<Interpreter:0x7ffbf70 @button_input_variable_id=0, @main=false, @move_route_waiting=false, @depth=0, @branch={}, @wait_count=0, @message_waiting=false, @child_interpreter=nil, @event_id=0, @map_id=0>, @indy_windows={}, @comic_enabled=false, @window_width=480, @message_frame=0, @window_image=nil, @shake=0, @choice_justification=4, @text_skip=true, @save_disabled=false>"

I can then split the string and store the variables in specific columns in the mySQL. I believe I can then retrieve them and put them back together again into the original string, but how would I convert the string and overwrite the object with it's data, including the unique identifier (ie: #<Game_System:0x7ffc000)? Or is this not possible?

Ok, I just found out, that hex number is actually a memory address where the object is stored, and it varies between games, even when opening saved games. The object id (found with $game_system.object_id and is a FIXNUM) will also change when loading a saved game from the id that it was when saved, so that is out as well. I suppose the next question would actually be, is there a better way to reload the data than manually editing each class instance variable from the string? for instance. I was thinking of creating the new class using (for example) $game_system = Game_System.new, then splitting the downloaded string and changing each @variable in the class to the value in the string if different. I would have to include an id of my own that is unique to each user's save to organize the data.

Is there a way to store a ruby object keeping the object intact without using marshal.dump, or can marshal.dump be used to store to mySQL? and marshal.load to load from mySQL?
 
Woohoo! I found it on my own!

http://ruby-doc.org/core/classes/Marshal.html

And as a test in RMXP:
Code:
$test = Marshal.dump($game_system)

$new_system = Marshal.load($test)

print "Original= "+$game_system.inspect

print "Marshaled="+$test.inspect

print "New= "+$new_system.inspect

And it passed!

RMXP help file had it wrong yet again:
Help File":hd91nqi5 said:
Marshal.load(port)
Reads marshal data from port (i.e., the string output from Marshal.dump) and creates an object with the same state as the original object. port specifies an instance of IO (or a subclass).

It should read something like this:
Marshal.load(source)
Reads marshal data from source (i.e., the string output from Marshal.dump) and creates an object with the same state as the original object. source may be either an instance of IO or an object that responds to to_str.

Now all I have to do is look at the "Gus Forum" code in the RWK, and modify it to save the marshal.dumped strings to mySQL, and then load them when needed. Unless someone beats me to this, I should have it done (with my amount of free time) within a couple of months.

Wish me luck!

[resolved]
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top