add_child() problems

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

I am having trouble with add_child(). I am trying to make it so that when the timer stops, one of three different warnings is added. For some reason, no warnings are added. Can someone help me? Here is my code:

extends Node2D

const warning1 = preload("res://Warning1.tscn")
const warning2 = preload("res://Warning2.tscn")
const warning3 = preload("res://Warning3.tscn")

func _ready():
	$Timer.start()

func _process(delta):
	if $Timer.wait_time <= 1:
		return
	$Timer.wait_time -= 0.0002

func _on_Timer_timeout():
	randomize()
	var choice = rand_range(0, 4)
	if choice == 1:
		var Warning1 = warning1.instance()
		add_child(Warning1)
	if choice == 2:
		var Warning2 = warning2.instance()
		add_child(Warning2)
	if choice == 3:
		var Warning3 = warning3.instance()
		add_child(Warning3) 
:bust_in_silhouette: Reply From: MysteryGM

It could be the constant tag. Most languages treat constants as objects that can’t have a instance; that way there is only one constant value.

var warning1 = preload("res://Warning1.tscn")

I believe this could work.

:bust_in_silhouette: Reply From: jandrewlong

rand_range() generates a float between the two arguments, you want randi() % 3, then do if statements for 0, 1, 2.

This worked, thank you!

ThreeSpark | 2018-10-10 03:14