x


Retaining GameObject data when switching scenes?

I'm making an RPG, and wanted to know how I would be able to retain a GameObject's possibly changed data/variables (such as HP, coordinates, etc.) when switching scenes. I am asking this because when a new scene is loaded all of the previous scene's GameObject's (such as the character object in that scene) data/variables (HP, etc.) will be set to their defaults and all of that mandatory info would be lost.

Any help would be appreciated. Thank you!

more ▼

asked Jan 31 '10 at 05:33 AM

LHP gravatar image

LHP
101 7 8 14

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

You could call Object.DontDestroyOnLoad(myGameObject); and then just keep the same gameObject from scene to scene.

more ▼

answered Jan 31 '10 at 06:01 AM

Azound gravatar image

Azound
668 19 22 39

But this will cause there to be two copies of the items in question when you return to the scene. You'd have the original object in the position you left it, and a carbon copy positioned right where it was in the authoring tool. That's not really helpful.

Mar 29 '11 at 09:35 PM scriptocalypse

You can create the permanent objects in one scene, set them to not destroy on load, then load the scene you really want to play. Then you never go back to the loading scene, just to the scene you want to play again.

Apr 07 '11 at 05:23 PM Azound
(comments are locked)
10|3000 characters needed characters left

You could also consider creating a "checkpoint" before level changes where all Data are written e.g. in a Savegame/File/Database and reloaded (respectively only the data needed for the new level) after every level change. Thus you can purge the memory from data unneeded for the scene (e.g in a indoor scene you barely need cooridates of visited places or other data related to outdoor levels)

more ▼

answered Jan 31 '10 at 11:12 AM

rawsteel gravatar image

rawsteel
113 1 2 9

I was sort of thinking of the same idea, but I don't have the slightest clue on how to write data, let alone load it and read it, is there some place you could point me to for help on this?

Jan 31 '10 at 10:54 PM LHP

I've seen commercial RPGs (e.g. Drakensang) using SQLite files for their savegames. They saved practically everything in there. Depending on your target platform you can use any embedded database, preferable SQlite since there is plenty of documentation and c# wrappers for it.

Feb 01 '10 at 09:11 AM rawsteel
(comments are locked)
10|3000 characters needed characters left

Another approach (not the best but an easy one) would be making static objects which can store player data. Those objects don't need an instance to be altered, but be careful to reinitialize those objects appropiately.

With static objects, you can also avoid passing lots of data from one object to another, or keep looking for objects every scene like in DontDestroyOnLoad() or store data Methods, but player data will be always on memory, and most programmers consider using static objects a bad programming habit.

more ▼

answered Jan 31 '10 at 11:36 AM

Dwair gravatar image

Dwair
358 1 2 4

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5099
x2097
x719

asked: Jan 31 '10 at 05:33 AM

Seen: 5512 times

Last Updated: Jan 31 '10 at 05:33 AM