• Advertisement

• ### Popular Now

• 9
• 9
• 10
• 10
• 9
• Advertisement
• ### Similar Content

• I'm having a weird issue with detecting a collision. I've tried everything I could find online but nothing seems to work. I have a brick object. It has a 2D Collider attached and I have also attached a 2D Rigidbody on it. I also have an EndScreen 2D Collider. The EndScreen 2D collider is tagged with "EndScreen". I am trying to detect when a brick collides with the end screen collider and simply print "game over" in the console.
This is my current code for this part of the program, it is attached to the bricks:
void OnCollisionEnter (Collision2D collision) { if (collision.gameObject.tag == "EndScreen") { Debug.Log("Game over"); } } Several things have happened depending on the set up. If I have the rigidbody 2D set as static, my ball object can still collide with the bricks, but I get no Log message. If I set it to Kinematic or Dynamic, I get absolutely no interaction between the ball and the bricks, and nothing when the bricks pass through the collider. I have tried to set the collider to a trigger and use OnTriggerEnter2D, no change. I have tried to put the rigidbody on the EndScreen object and tried to set it's body type to all 3 settings, no change. The only thing I can think of that I have not done is put the script on the EndScreen object and switch the tag to the bricks. The reason I have not done this is because I will have several types of bricks, some of which will have different tags.

Please tell me somebody can see what I'm doing wrong here, because I'm losing my mind over something I feel should be ridiculously simple. Thanks.

• Downloadable at:
https://virva.itch.io/sandman-academy
https://gamejolt.com/games/sandmanacademy/329088
https://www.indiexpo.net/en/games/sandman-academy
https://www.gamefront.com/@sandmanacademy
http://www.indiedb.com/games/sandman-academy

• Downloadable at:
https://virva.itch.io/sandman-academy
https://gamejolt.com/games/sandmanacademy/329088
https://www.indiexpo.net/en/games/sandman-academy
https://www.gamefront.com/@sandmanacademy
http://www.indiedb.com/games/sandman-academy

• Downloadable at:
https://virva.itch.io/sandman-academy
https://gamejolt.com/games/sandmanacademy/329088
https://www.indiexpo.net/en/games/sandman-academy
https://www.gamefront.com/@sandmanacademy
http://www.indiedb.com/games/sandman-academy

• Downloadable at:
https://virva.itch.io/sandman-academy
https://gamejolt.com/games/sandmanacademy/329088
https://www.indiexpo.net/en/games/sandman-academy
https://www.gamefront.com/@sandmanacademy
http://www.indiedb.com/games/sandman-academy
• Advertisement
• Advertisement

# Unity Serialising Game Objects

This topic is 878 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

##### 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

##### 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

##### Share on other sites

• Advertisement