How to make platforms disappear and then reappear?

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

I have a platform that I am able to have it disappear after 1 second, and I have used a timer to do so. Now I am trying to make that same platform reappear. I am not sure if I am approaching this the right way.

Stage script:

extends Node2D

var hiding_plat = preload("res://Scripts/DissappearingPlatforms.gd")
var x = 1


func _ready() -> void:
	pass # Replace with function body.

	
func spawn_plat():
	if x == 1:
		var new_plat = hiding_plat.instance()
		new_plat.connect("dissappear")
		get_tree().get_root().call_deferred("add_child", new_plat)
		new_plat.set_position(get_position(), Vector2(21, 6))

DissappearingPlatforms Script:

extends TileMap

var z = 1

func _ready() -> void:
	$Timer.start()
	pass

func _on_DissappearingPlatforms_visibility_changed() -> void:
	pass # Replace with function body.

func _change_z_back():
	z -= 1

func _on_Timer_timeout() -> void:
	emit_signal("dissappear")
	z += 1
	_change_z_back()
	queue_free()
	pass

The random z and x variables are just placeholders (I was tinkering with unsuccessful loops). Also I am aware that func _on_DissappearingPlatforms_visibility_changed() does nothing. That was a preexisting signal from Godot that I connected from the Node tab, I just don’t know what logic I would put in there to ideally make the platform invisible (the most straightforward way), instead of using que_free at the end of the timer (my platform is lost after this executes). Thanks for any help.

:bust_in_silhouette: Reply From: deaton64

I’m no expert, but I wouldn’t destroy the platform and recreate it. I would make the platform hide and show itself again.
self.hide() and set the collision polygon to disabled.
Add another timer to platform script to self.show() and enable collision polygon or send a signal to it.

Depends how you want your platforms to work. If there are a few and you want them to disappear at the same time, add them to a group and send a signal out to the group.

Ok, thanks for this. I just added $CollisionShape2D.disabled = true to my _on_Timer_timeout() function, but it’s throwing: get_node: Node not found: CollisionShape2D.

I think it’s because I am working with a tilemap and there is no regular collisionshape2d in my tree… I guess I have to do more digging!

eviking | 2020-04-28 21:20

Got it working with the following script (and help from some other friendly folks). But yes, your original method was the way to go for me. Thanks.

extends KinematicBody2D

var x = 1
var _timer = null
var _timer1 = 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:
			self.visible = false
			$CollisionShape2D.disabled = true
			x += 1
		
		elif x == 2:
			self.visible = true
			$CollisionShape2D.disabled = false
			x -= 1
			pass

eviking | 2020-04-29 20:44