Jump to content
  • Advertisement
Sign in to follow this  
Angelic Ice

Unity Serialising Game Objects

This topic is 970 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello gamedev.net community!

I have a question about serialising objects to files or Lua-tables.

 

My application is a visual novel engine. For those who do not know what a visual novel is: They mainly display characters on a background while they show up a dialogue text box in the front.

 

First of all my main program is written in C++ using Lua files for the dialogues.

These Lua files look like this:

	   location "school_outdoor"

           --blendin is adding a new object to the character pointer list
           --m and y are shortcuts for names
	   blendin ("friendly", "m") 
	   blendin ("angry", "y")

When the player closes the game, the position at where the player left is being saved via a lua-table file. So once the player starts the application again, the current position in the game is being executed. This is mainly a say()-function calling text onto the screen.

 

However I am not so sure how to save serialise characters. They feature exactly four attributes: Name, mood, x- and y-position.

 

One idea I had was like this:

--let us assume these are proper names: character1, character2, character3
characters = {
  ["character1_mood"] = "Angry",
  ["character1_x"] = 500,
  ["character1_y"] = 400,
  ["character2_mood"] = "Happy",
  ["character2_x"] = 100,
  ["character2_y"] = 400,
  ["character3_mood"] = "Sad",
  ["character3_x"] = 700,
  ["character3_y"] = 400,
}

Is this an efficient way? Specially because characters will be added and removed making this a bit messy to work with.

 

How would you serialise multiple objects (with multiple attributes) which can disappear at certain moments?

Would an approach via a Lua table be a proper way? If yes, how would an efficient structure could look like?

 

Thanks for taking your time to read my thread!

I am looking forward for your ideas and suggestions!

Edited by Angelic Ice

Share this post


Link to post
Share on other sites
Advertisement
I would just encode and decode your characters as JSON. Since you only store strings and numbers in the characters object it should be pretty simple too.

Here is a list of json modules you could try
http://lua-users.org/wiki/JsonModules

Using lunajson, you could have the code work like this
-- when loading
characters = lunajson.decode(fileContents)

-- when saving
fileContents = lunajson.encode(characters)

EDIT:

I would also switch up your characters format

I would have a single function to serialize and deserialize a single character, then have an array of serialized data that you pass to and from the serialization functions


function serializeCharacter(character)
  return {mood = character.mood, x = character.x, y = character.y}
end

function serializeCharacters(characterList)
  local result = {}

  for character in characterList do
    table.insert(result, serializeCharacter(character))
  end

  return result
end

-- then to encode the characters
fileContents = lunajson.encode(serializeCharacters(characterList))

I would not worry about performance for this. It will be performant enough. I would focus on clean code first, then performance once you know it runs too slow and you have identified a bottleneck in your code. Edited by HappyCoder

Share this post


Link to post
Share on other sites

Thanks! This looks really interesting!

It says Lunajson needs no dependencies but on the other hand the installing part talks about Lua Rocks. Is it worth to implement Lua Rocks for this? What kind of different way could I go in order to use Lunajson? Well, I partly understand copying the src but then again it mentions the package.path which is probably a part of Lua Rocks?

I am just a bit confused and would be happy if you or someone could answer these few questions.

Edited by Angelic Ice

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!