Need help making a scene saving / memory thing

Godot Version

4.2 web editor

Question

so i have a game world and i wanted to add an inventory. now im on a old laptop so i thought for preformance reasons that i would make the inventory its own scene. but i found an issue the scene resets itself on return. now i have allready done things such as tracking the players last position and achivements so it can be set on return but i have no idea how to track some things like what instances of enamys are currently dead. so does and one know how to track that stuff or a way to make the scene not reset itself.

I think I can give you some guidelines to help you on your way. I hope this amount of detail isn’t annoying, but the rest of the post assumes that you’re currently a beginner and I want to give you as much help as I can.

Edit: I just wanted to add that the answer to the question is not to replace the scene or change it, but to add another Node in the game. I’m so sorry! I got so caught up in the why’s and how’s that I forgot to give the direct and simple answer. Rest of the original post below:

First, about your old laptop: Are you able to run the real (downloaded) editor? If so, you should first try to move to that. It’ll likely run a lot faster if you can, as the web editor has a lot of “hoops” to jump through to work inside a browser.

For managing inventory, yes, you should use a scene. However, it’s not really for space reasons. I think you are misunderstanding the word “scene.”

In a large and technically complicated program like Godot (or UnityEngine, or git) there’s jargon that means a specific thing. They chose the word “scene” to mean “A stored description of a tree of Node that you can clone into the game.” And there’s more jargon: “instantiate” means “make an object from a class.”

I think this is a bad choice, because you’re very much meant to have many “scenes” all attached to the tree at once. And, on top of that, the function used to change the scene (as in, the current scene like a game/movie scene) will take the whole tree away, and put another whole tree back.

The manual for Godot is very good and thorough. Although, sometimes, it’s a bit out of date or forgets to mention something important… That being said, you should definitely read about how this tree works: Nodes and Scenes — Godot Engine (stable) documentation in English

If this is your first time making anything with Godot, and if you have the time to spare, you may be better off shelving this project for the time being. I suggest following along Dodge the Creeps. It can be less exciting to just follow instructions, but it can really help. It covers many basic things. Especially, it will walk you through creating scenes for the enemies and the player, and having multiple of them all active at the same time.

yes i consider my self beginner, 1 thing i didnt mention is that its also a school laptop. so while it can handle godot 4 i also cant active linex to download it. 2 preformance ist too bad. my game currently has many vfx and sfx with the game and it sti runs a smooth 60 but if battery drops below 20 i cannot get above 30 frames and if im on it for more than 2 hours it overheats and the preformance starts going haywire. that and i dont hate anything more than a laggy game so i want to try to be as optermised as possible to make up for any code that is pretty bad. and yea i dont know exacly the termanology and thats mainly because i gave a very active learning style where i pick a project and try tp learn the skill and things as and when i need them. i have spent about 2 months on this project and have about 2 - 4 min of game but it is very polished. btw “It covers many basic things. Especially, it will walk you through creating scenes for the enemies and the player, and having multiple of them all active at the same time.” i do my slimes are a slime logic scene ataached to there spawnpoint scene and then there are multiple spawnpoint scenes placed inn the world. (same with the player scene). i might not know the words or phrases 100% but i have been self learning for 1 year.

It sounds like you’re on the right track, keep it up.

First off, I think you’re right to use a scene for the inventory, because it will help make your code more organized. Having a Node means that the inventory’s memory is automatically created and freed when your game exists.

I apologize for misunderstanding your question.

When you say you make the inventory scene, you mean that the view, itself, changes to a menu to select items, correct?

The scene does reset itself when it exits. That’s because when you switch the scene, Godot first discards the whole scene, then creates an instance of the new scene. There’s also another way, which involves detaching the inventory scene and keeping it around in memory. I don’t see a problem with this unless the inventory is very, very, very big. I do recommend trying it and seeing if this is good enough: add_child() and remove_child().

soo i have no inventory yet its just a grey screen but in can press i to go to it and i while in it to go back. i see how your solution works and i will use that but idk how tostop the world scene from reseting

i have a get tree change scene to file (inventory/ world)

so should i just have the inventory open ontop of the world instead of changeing the scene to it. i guess that would be alot easier.

but if possible i still want to know if theres a way to keep the sceen saved so like if i return to the main menu so the world scene gets unloaded that once entered its exacly how it was left.

unlesss could the child thing work for the world root node? idk maybe a like game scene that i then instate the main menu or world or inventory as needed?

if you kinda get my drift do you know of and videos that could possibly go over the process. btw thanks so much for the help youve given when i started i didnt have a clue but now i have a couple of leads or theorys to try.

I’m glad I helped a bit. I’m not sure I know of any videos that cover this. Definitely search for some on YouTube, I’ve never been the kind of learner to seek them out, so unfortunately I haven’t gotten one already.

Now that I think about it, there’s actually perhaps a much better way of doing this since this problem seems specific to just your game/inventory screens. Instead of bothering with a scene manager, maybe it would be better to just make a new Node, called GameManager.

Inside of GameManager, put your current Game scene, enabled, and your inventory, disabled.

Then to swtich, enable one, and disable the other. the disabled ones aren’t processed, so there’s no need to worry about slowing down.

For reference, this is what covers manual scene changing using attach/detach child:

thanks ill defo go play with these new toys see how they tick. it was an honour to brainstorm with you. have a great morning, evening or night.

1 Like

“preformance reasons”
“but i have no idea how to track some things like what instances of enamys are currently dead”
do you know about queue_free() ?
so basically classes are a blue print and objects are a building, if you never destroy the buildings you no longer need even the most expensive of computers will run out of everything. queue_free() removes those things you no longer need.
it is in Creating the enemy — Godot Engine (stable) documentation in English but queue_free() easy to miss and it’s doesn’t really say why it’s important to delete them.
yeah old laptops basically are made to have their batteries fail so you are forced to upgrade, the voltage they require is listed on them and some times you can find ways… to power it but do so only if you really know what you are doing. we are using Godot so you’re among friends as far as getting things to work at least for me. As for performance enhancing… you can do strong typing func myfunk(myPram :int)->String: I personally love typing everything because it really helps with auto complete and error messages like Cannot Pass a value of type “int” as “String” that avoids “1”+“1”== “11”. look in to shaders sometimes magic can be done on the GPU to avoid doing something the CPU will have problems with. make sure the laptop fans are not blocked and dust free they should not be going on the lap despite the name. some times showing parts of the code in mostly unrelated examples to your game can keep the security of your game while being able get help.

I understand that the issue is solved, however, I feel the need to clarify a few things here. They are nuances, but very important ones. As a beginner, it’s important to get these basics right, because they can drastically improve the speed at which you comprehend newer, more complicated, ideas that build upon them.

“but i have no idea how to track some things like what instances of enamys are currently dead” @tdvditchfield

“do you know about queue_free() ?” @AlexanderLife

queue_free() is an important aspect of memory management. However, in Godot, there are two very different kinds of managing memory, and it isn’t clear at first which one is used or why.
Object Is the base class in Godot. It defines both queue_free() and free().
Object splits into two main groups:

  • RefCounted and all classes that derive from that Resource, ButtonGroup, etc., do not need to be freed. That is because they track how many variables are using (referencing) them. I need to explain references, but this post is too long, already.
  • All other classes (including Node) that don’t inherit from RefCounted need to be freed manually. With some things like regular Object, you can and should usually use free(). queue_free() tells the Godot engine “Hey, delete this as soon as you can.” If you were to call free() instead, it would be freed immediately, and then Godot would have no way to remove it from the tree, update the parents, etc.

As for performance enhancing… you can do strong typing func myfunk(myPram :int)->String: I personally love typing everything because it really helps with auto complete and error messages like Cannot Pass a value of type “int” as “String” that avoids “1”+“1”== “11”.

Strong typing is, indeed, a good thing in most cases. It helps prevent accidents before they happen, just like you said. However, it doesn’t help performance at all. GDScript is weakly typed, and it is also interpreted. Those typing hints are only there to tell Godot that something is wrong and to give you an immediate error message, rather than letting it silently continue with unintended behavior. Because it’s interpreted, it still runs the code which would allow you to call the above myfunk with myPram with a String. Only when the call happens, does it fail.

For a practical example of how this can be, have a look at the following minimal example:

extends Node

func _ready():
  var three = "3" # three is a Variant, not a String.
  # No error, the game starts fine. It breaks when you run it.
  add(three)
  # add("4") # This one has an Editor error though...

func add(i:int): # Can be int or Variant
  print(i + i)

Now, about tracking instances of enemies that are currently dead. That question was never answered.

Let’s say you add an enemy. In their script, let’s add a signal. Instead of calling queue_free(), let’s define a function which is a more specific kind of removing: When they die.

class_name MyDude
extends Node

signal killed(which:Node)

func kill() -> void:
  killed.emit(self)
  queue_free()

Now, in your spawner:

class_name EnemySpawner
extends Node

@export var my_dude:PackedScene

func make_enemy():
	var instance = my_dude.instantiate()
	get_parent().add_child(instance)
	instance.killed.connect(_on_dude_killed)

func _on_dude_killed(dude):
	print("he's dead, jim!")

This creates an enemy instance, and connects the signal. When that specific dude’s kill() function is called, the signal is emitted with that dude’s self (so you can see which one was killed.

Because queue_free() didn’t actually delete him (yet), you can still do stuff without getting errors.

I enjoy helping beginners out, it’s one of the two reasons I became active here recently. If you need more information than this, it’s best to message me. If you do, as a reminder, only use the built-in messages to talk to users on this platform, because they’re moderated.

“However, it doesn’t help performance at all. GDScript is weakly typed, and it is also interpreted. Those typing hints are only there to tell Godot that something is wrong and to give you an immediate error message,”

personally I think it does help with optimization, from what I read(godot/modules/gdscript at master · godotengine/godot · GitHub) GDScript seems like it’s a little bit like java’s just in time compiler and it uses a analyzer (godot/modules/gdscript/gdscript_analyzer.h at master · godotengine/godot · GitHub) to match with C++ types and make sure their is enough memory reserved to hold the 1 and 0. so like the computer needs to know even with interpreted languages how much memory var myvar should be reserved. so if it’s a type less language it must reserve something that can ask for more memory as needed. but a strongly typed language you make int myvar and it says ok show me the open addresses and reserve a address for myvar and then reserve 4 extra memory byte spaces for the rest of the actual data that will go inside myvar. so say you have int array and say this array can only hold 2 ints so it takes 8 spaces in memory plus the overhead because 24. but let’s say you do some work to resize that array so it holds 3 ints it now takes 12 spaces in memory 34. so that’s how you can have something that ask for more but it takes work resize stuff, and that’s what the truly type less languages have to do, but usually those are just brand new languages and even if they don’t show the type checking in syntax they try when they can. GDscript is a gradually(mixed) typed that basically means typing when they can, but it has the benefits of having variable type so you can have the freedom to handle mypram being 1 or “1” in the logic of the function not requiring all things be type. GDscript really is one of the best languages for putting ideas down fast and growing them.

thanks for the info on queue_free! post like this can help people that find it years down the road.
yeah I didn’t really go in to the detail of explaining it like you did, the op did not really give much code so I was just throwing out anything that might help him and cut the frustration and had a place to start looking.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.