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():

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

func _on_Timer_timeout():
	var choice = rand_range(0, 4)
	if choice == 1:
		var Warning1 = warning1.instance()
	if choice == 2:
		var Warning2 = warning2.instance()
	if choice == 3:
		var Warning3 = warning3.instance()
: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