Deactivating Objects

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By razor7131

Is there a way to put an object into sleep like state?

I have a 2d project with a map that let player to explore around with enemy spawning each time the player come, however I want the player to have freedom to explore each area seamlessly so he can move one to another area without any level transitioning, but that come with price of the performance since the enemy will keep spawning.

I tried to only make them visible within certain radius but that didn’t work quite well, I had a thought about record their presence, freed them and load them again once player come within radius but I’m afraid it will become more and more complex in the future and become unmanageable, if bugs occurs it would be a nightmare.

Is there a better way to handle this? thanks in advance.

What node type is the object you need to deactivate?

yanb | 2020-11-03 16:36

It was KinematicBody2D and StaticBody. And just in case you are wondering, the type of project I made, is similiar to Rune Factory 1 but with larger area and more variety of monster each totem can reproduce. Now I think about it, to destroy them completely if player leave them, but it will be pretty unchallenging I think…

razor7131 | 2020-11-03 21:16

:bust_in_silhouette: Reply From: Surtarso

When you toggle visibility you dont disable all the code running behind the object.

On your enemy node use the ready() function set their physics to false

func _ready():
    set_physics_process(false)

add visibility enabler with pretty much the same size as your enemy sprite, and check all boxes on the inspector, to pause animations, particles, physics etc

With that, the only enemies that will be read are the ones on screen, everything else wont process.

Thanks for your reply, however as I said, I had been tried doing that by telling each enemy including the spawner to only visible within a certain radius, it works great in the early game and progressively gets worse. I got to the point that I think my game design kinda crap, it’s ridiculous to keep thousands of enemies in a single level, it took me forever to just reload the game state. Even I rarely see the big game industry doing that so yeah, it’s flawed by design. Thanks for your time anyway.

razor7131 | 2020-11-05 22:05

when you toggle visibility you dont disable all the code running behind the object

with the visibility enabler node you have the option to leave the enemy there, visible, but disable all his code reading (physics) and its pretty much like he is not there, becomes just a sprite

another thing you could check is texture size… if your character is 32x32 and your sprite is 1080p resized down in godot its gonna hurt, make sure your sprite is as lite as it can

also alphas etc are “read twice” since it render the image than alpha it in 2 steps it adds a lot of processing, so never alpha stuff down to 0 to hide, just use .show() and .hide()

Surtarso | 2020-11-05 22:19

on your enemy _ready() function set their physics to false with set_physics_process(false)

add visibility enabler with pretty much the same size as your enemy sprite, and check all boxes on the inspector, to pause animations, particles, physics etc

than the only enemies that will be read are the ones on screen, everything else wont process

Surtarso | 2020-11-05 22:30

I just tested that. I can’t believe it, any process/physics call is still being called. So all my enemies all this time is wandering as a ghost!? I don’t know where I got this superstitious thinking that if I make them invisible it will automatically turn off their script. My… this is terrible, thanks for your critical insight!

razor7131 | 2020-11-05 23:09

thanks for the upvote, I reformated the original answer for ease of access for future people =)

Surtarso | 2020-11-06 00:19