Cannot remove child from Node

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

I am trying to simply remove a child from my tree, and I am being presented with the following error: error(22,59): Expected ‘,’ or ‘)’. Now I know this is referring to the argument on line 22, but I don’t understand why it’s not accepting TileMap: 1243 as an argument… that is what the node is called when I print it out. I do not understand where it is expecting a parenthesis or a comma.

Also, will I be able to simply add the child node back in as it was previously, after I remove it? That was my intention of the timer. Any help would be great, thanks.

extends Node2D

var x = 1
var _timer = null


func _ready() -> void:
	_timer = Timer.new()
	add_child(_timer)
	
	_timer.connect("timeout", self, "_on_Timer_timeout")
	_timer.set_wait_time(1.0)
	_timer.set_one_shot(false)
	_timer.start()
	pass



func _on_Timer_timeout() -> void:
		if x == 1:
			get_node("DissappearingPlatforms").remove_child(TileMap:(1243)
	#		self.hide()
			x += 1
		
		elif x == 2:
			emit_signal("reappear")
			x -= 1
	#		self.show() Replace with function body.
			pass
:bust_in_silhouette: Reply From: njamster

but I don’t understand why it’s not accepting TileMap: 1243 as an argument… that is what the node is called when I print it out.

When you print something out, it’s a String. So if you would like to pass the text that was printed as an argument to a function, you’d put it in quotation marks: “TileMap:(1234)”. However, remove_child expects a Node as an argument, not a String! So what you actually should do (if your TileMap is called “TileMap”) is this:

var child = get_node("DissappearingPlatforms").get_node("TileMap")
get_node("DissappearingPlatforms").remove_child(child)

I do not understand where it is expecting a parenthesis or a comma.

At the end of the line. It simply checks if each parenthesis is closed later. Which is not the case in your code: remove_child _(_ TileMap: _(_ 1243 _)_

will I be able to simply add the child node back in as it was previously, after I remove it?

As long as you keep a reference to the node around (like the variable child in my example above` you can add it back to the tree at any time, yes:

var child = get_node("DissappearingPlatforms").get_node("TileMap")
get_node("DissappearingPlatforms").remove_child(child)
# do something else, let time pass...
get_node("DissappearingPlatforms").add_child(child)

Hey, thanks for your help again!

I unsderstand almost everything, but I may given the wrong information.

DissappearingPlatforms is a tilemap itself, and has no children. DissappearingPlatforms is a child of the root node, which is StageOne. The script you are seeing above is my StageOne script. Would what you just said still work? I am going to try it out now

eviking | 2020-04-29 17:55

Well, if DissappearingPlatforms does not have any children, you obviously cannot remove them. :wink: What you can do though is removing the TileMap itself:

var tilemap = get_node("DissappearingPlatforms")
remove_child(tilemap)
# do something else, let time pass...
add_child(tilemap)

If your goal is to only remove certain tiles, you cannot do it this way (as tiles are not individual nodes, but a set of three numbers the TileMap-node interpretes). However, you can reset a cell of the TileMap back to “No-Tile” using this:

get_node("DissappearingPlatforms").set_cellv(x, y, -1)

where x and y are the cell’s indices in horizontal and vertical direction.

njamster | 2020-04-29 18:07

Perfect, yes at this point I am fine with removing the Tilemap itself and then just adding it back in.

Handy stuff for resetting the tilemap back to No-Tile. That will definitely be useful in future projects. Thanks again!

eviking | 2020-04-29 18:44

:bust_in_silhouette: Reply From: Drachenbauer

remove_child(TileMap:(1243)

maybe it´s just because you forgot the closed bracket for the open one of the remove_child-function.
you only have both, that diractly surround the numbers.