0 votes

I want an object to destroy the previous node as soon as it is created.

I'm building a spawner system for my highway racing game, but when a node is created in my game, I want it to destroy the previous node.
But I couldn't find anything. Please help me.
The code I use for this is:enter image description here

Godot version v3.4.4.stable.official [419e713a2]
in Engine by (95 points)

1 Answer

+1 vote
Best answer

Hey! I think your image link isn't working properly.

But if you want to achieve this effect, you could do the following:

1) Create an empty Node2D or Node3D and call it "ObjectHolder" or similar. This will hold the new nodes of interest you are trying to create and destroy.
2) When it is time to create a new node, first queue free all of the children nodes of "ObjectHolder" and then add the new node as a child of the object holder node.

So this will ensure that "ObjectHolder" only has one child node at a time and deletes the old nodes before adding the new one

I'm sure there's another/better way to do this but without any additional information this is what I came up with. Good luck!

by (177 points)
selected by

So the difference between load and preload is that load starts loading the scene when the line of code is reached, and preload loads the scene ahead of time. You should in theory be able to prevent some lag by loading your scene ahead of time via preload

Somewhere outside of your function, you can use

const roadScene = preload("res://scenes/maps/test_road_load.tscn")

That will make it so that roadScene is able to be instance without needing to re-do the loading of it every time the function is called. It has to be outside of all functions. Ideally towards the top of your script so you can see it clearly in the future.

Then you simply do

var road = roadScene.instance()

when it's time to instance the new roadScene in the function. Did you already try this and it's still lagging?

Yes. Even after I did that it still hangs.

Gotchya, I was asking because the code in the picture you shared is different than what I was suggesting.

Hmm well it will be hard for me to diagnose this without going through the rest of the project haha.

Instead of that, you can try to look at the Profiler. In the output console window, there is a tab called Debugger. When you click that, there is an option called Profiler. Hit start to begin the profiler and then test the game. It will help you narrow down the cause of the lag further

There was one HTerrain in the road scene. Although HTerrain is invisible, the game hangs because of it. I deleted it. It doesn't hang out anymore, but the road looks very lousy like this. So I can't add anything to the road scene. Because there are lags in the game.

Is there a way to add something to the road scene without creating lags in the game?

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.