playMaker

Author Topic: keep the object state between scenes?  (Read 18058 times)

aheng

  • Playmaker Newbie
  • *
  • Posts: 3
keep the object state between scenes?
« on: December 03, 2024, 10:36:17 AM »
I'm a novice in Unity, and I'm attempting to create a project similar to Hollow Knight or Castlevania. I've been following video tutorials and everything has been going smoothly until I encountered a problem that wasn't covered in the tutorials.
When I switch scenes, the enemies that have already been defeated are reloaded, which is not what I want. They should not be reloaded until I reach a checkpoint or die. However, I'm unsure how to achieve this. Perhaps I could store the information of defeated enemies and then check it each time the scence is loaded?
 Should I use a table or a dictionary for this purpose? I installed Array Maker, but it seems a bit too difficult for me to use. I'm not sure which type of table or action to employ.
Should I start by learning more about programming-related knowledge first? Please excuse my poor English; it's all translated from a translator.
I'm a novice both in Unity and forum posting, so please forgive me if I offend anyone in any way  :D

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: keep the object state between scenes?
« Reply #1 on: December 04, 2024, 01:03:25 AM »
Hi,

This is a common need for games, yet not a trivial feature to develop. Let me describe what you need to achieve that.

- you need a way to identify each object's item in the scene you load levels using an ID as a string. You could do it using the name of the GameObject, as long as you make sure that there are no two of these objects named the same

- theses items must save their status in a persistent way (inside playerprefs most likely). For example an enemy was killed, if you restart that level, the enemy should not be there anymore.

- during the loading of a scene level, each of these items, check the prefs to update their current status and must act accordingly before the game or level can start.

ok, the above seems doable, and it is infact not that hard to make a proof of concept for a couple of items, but the problem will grow as your game grow, and you'll have shortcomings such as:

- bloated playerprefs where you have many many entries, one for each of your items, for each levels. it quicky reaches hundreds of hundreds on a finished game.

- performances will suffer during level loading if dozens of items are each accessing the playerprefs, same on saving a current level progress.


if your game is fairly small in size, the above will do fine, if you have 10 levels with dozens of ennemies, and dozens of pickup items, and collectibles, etc... then it's not going to cut it...


ArrayMaker is a good solution to solve this.

- you can have a dictionnary of each items as keys, and their status as value, so during the gameloop you only access arraymaker, which is fast, and when you need to save, you can use EasySave ( arraymaker has support for easysave) to save in one call your level progress. Same with loading a level, you have one fsm that will read one playerpref entry which represent the state of your level. then each item in that level will check their status using that fsm, instead of accessing the playerprefs themselves.

Let me know if that makes sense.

Bye,

Jean


aheng

  • Playmaker Newbie
  • *
  • Posts: 3
Re: keep the object state between scenes?
« Reply #2 on: December 04, 2024, 06:07:41 AM »
Hi,

This is a common need for games, yet not a trivial feature to develop. Let me describe what you need to achieve that.

- you need a way to identify each object's item in the scene you load levels using an ID as a string. You could do it using the name of the GameObject, as long as you make sure that there are no two of these objects named the same

- theses items must save their status in a persistent way (inside playerprefs most likely). For example an enemy was killed, if you restart that level, the enemy should not be there anymore.

- during the loading of a scene level, each of these items, check the prefs to update their current status and must act accordingly before the game or level can start.

ok, the above seems doable, and it is infact not that hard to make a proof of concept for a couple of items, but the problem will grow as your game grow, and you'll have shortcomings such as:

- bloated playerprefs where you have many many entries, one for each of your items, for each levels. it quicky reaches hundreds of hundreds on a finished game.

- performances will suffer during level loading if dozens of items are each accessing the playerprefs, same on saving a current level progress.


if your game is fairly small in size, the above will do fine, if you have 10 levels with dozens of ennemies, and dozens of pickup items, and collectibles, etc... then it's not going to cut it...


ArrayMaker is a good solution to solve this.

- you can have a dictionnary of each items as keys, and their status as value, so during the gameloop you only access arraymaker, which is fast, and when you need to save, you can use EasySave ( arraymaker has support for easysave) to save in one call your level progress. Same with loading a level, you have one fsm that will read one playerpref entry which represent the state of your level. then each item in that level will check their status using that fsm, instead of accessing the playerprefs themselves.

Let me know if that makes sense.

Bye,

Jean


 can't believe how quick and detailed your reply is.
 I have read through it all and understood the principles, which are very helpful to me. Now I've decided to figure out how to use different actions in ArrayMaker and start experimenting with it. Thank you for your advice and explanation.
 love this forum ;) ;)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: keep the object state between scenes?
« Reply #3 on: December 04, 2024, 08:46:56 AM »
Hi,

 I am glad I could help :)

Bye,

Jean

colpolstudios

  • Sr. Member
  • ****
  • Posts: 365
Re: keep the object state between scenes?
« Reply #4 on: December 04, 2024, 06:40:43 PM »
Hi,

This is a common need for games, yet not a trivial feature to develop. Let me describe what you need to achieve that.

- you need a way to identify each object's item in the scene you load levels using an ID as a string. You could do it using the name of the GameObject, as long as you make sure that there are no two of these objects named the same

- theses items must save their status in a persistent way (inside playerprefs most likely). For example an enemy was killed, if you restart that level, the enemy should not be there anymore.

- during the loading of a scene level, each of these items, check the prefs to update their current status and must act accordingly before the game or level can start.

ok, the above seems doable, and it is infact not that hard to make a proof of concept for a couple of items, but the problem will grow as your game grow, and you'll have shortcomings such as:

- bloated playerprefs where you have many many entries, one for each of your items, for each levels. it quicky reaches hundreds of hundreds on a finished game.

- performances will suffer during level loading if dozens of items are each accessing the playerprefs, same on saving a current level progress.


if your game is fairly small in size, the above will do fine, if you have 10 levels with dozens of ennemies, and dozens of pickup items, and collectibles, etc... then it's not going to cut it...


ArrayMaker is a good solution to solve this.

- you can have a dictionnary of each items as keys, and their status as value, so during the gameloop you only access arraymaker, which is fast, and when you need to save, you can use EasySave ( arraymaker has support for easysave) to save in one call your level progress. Same with loading a level, you have one fsm that will read one playerpref entry which represent the state of your level. then each item in that level will check their status using that fsm, instead of accessing the playerprefs themselves.

Let me know if that makes sense.

Bye,

Jean

would using Array Maker work with spawned enemies and the dictionary of each item as keys, and their status as value?

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: keep the object state between scenes?
« Reply #5 on: December 04, 2024, 11:38:27 PM »
Hi,

 Yes, it would, any gameobject in that scene. the important aspect is that your spawned enemies must have a unique ID that is consistent as you reload the level again and again.

Bye,

Jean

colpolstudios

  • Sr. Member
  • ****
  • Posts: 365
Re: keep the object state between scenes?
« Reply #6 on: February 06, 2025, 06:07:19 PM »
Hi, it has taken me a while to get it, but here is a playable demo.

https://colpolstudios.itch.io/enemy-saving-and-loading-with-playmaker-arraymaker

The core C# scripts were collaboratively refined with the assistance of an AI model.

Supporting nested (hierarchical) enemies as well as dynamically spawned enemies at runtime with no additional assets required.

This project uses a custom saving and loading solution along with a safely deleted file structure.

Scalability: Thanks to its modular design, the system can be extended to handle more complex enemy attributes with coding.

Note: Red enemies are spawned!

Feedback is very welcome.

regards

Colpolstudios


aheng

  • Playmaker Newbie
  • *
  • Posts: 3
Re: keep the object state between scenes?
« Reply #7 on: May 02, 2025, 03:08:44 AM »
Hi, it has taken me a while to get it, but here is a playable demo.

https://colpolstudios.itch.io/enemy-saving-and-loading-with-playmaker-arraymaker

The core C# scripts were collaboratively refined with the assistance of an AI model.

Supporting nested (hierarchical) enemies as well as dynamically spawned enemies at runtime with no additional assets required.

This project uses a custom saving and loading solution along with a safely deleted file structure.

Scalability: Thanks to its modular design, the system can be extended to handle more complex enemy attributes with coding.

Note: Red enemies are spawned!

Feedback is very welcome.

regards

Colpolstudios

I'm really sorry for replying so late. Actually, I've been busy with trivial matters in my life for some time and didn't pay attention to the forum. Although my current project is still in its very initial stage, your solution might be helpful to me in the future. Anyway, thank you so much. By the way, the last part of this content comes from a translation software. I hope I didn't offend you. :D