Error message when trying to write the result of a die roll to a "chat"

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

I’m using the following code under a tab in a tab-container.

func _on_DieRollButton_button_up():
	var ChatBox = get_node('Control/BASEUI/Chat/ChatBoxPanel')
	
	var number_of_dice = int($RollsTab/DiceNumberInput.text)
	var sides_of_dice = int($RollsTab/DiceSidesInput.text)
	randomize()
	var msg = rand_range(1 , sides_of_dice)
	
	ChatBox.display_message(msg)

Doing this, I get the error:

Attempt to call function ‘display_message’ in base ‘null instance’ on a null instance.

What i’m trying to do is access a function, named display_message, that I wrote in a script that’s attached to a different node that my tab is not a sibling with. It’s in the same scene, just not a child or a parent. Is this possible?

My Node Hierachy looks like this:

The end goal of the script is to generate a random number using the sides_of_dice var as a cap on how high that number can be, and then display that number in a chat log, basically just a dice roller.

:bust_in_silhouette: Reply From: kidscancode

This error means that ChatBox is null.

get_node('Control/BASEUI/Chat/ChatBoxPanel')

That is not the path to the node. If your script is running on TabContainer, then your target node is a child of a sibling. The correct path would be

get_node('../Chat/ChatBoxPanel')

You can also use absolute paths by calling get_node() on get_root(), but keep in mind if you’re instancing this UI in another scene, there may be more nodes between the root node and the Control.

Thanks for this! That got rid of my null error, but now i’m getting something to do with the way i’m assembling the string at the end.

$ChatDisplay.text += userid + ": " + msg + "\n"

This code gives me

Invalid operands ‘String’ and ‘float’ in operator ‘+’

Am I just assembling the string incorrectly? Is there an obvious thing i’m missing?

for context, the entire script is

extends Panel

var userid = "Ethan"

func _input(event):
	if event is InputEventKey:
		if event.pressed and event.scancode == KEY_ENTER and $ChatInput.text != "":
			send_message()
			
func send_message():
	var msg = $ChatInput.text
	#eventually stuff for ports etc.
	$ChatInput.text  = "" 
	display_message(msg)
	
func display_message(msg):
	#eventually stuff for reading ports etc.
	$ChatDisplay.text += userid + ": " + msg + "\n"

crookedvult | 2019-06-01 07:25

Stick a test print() before that line. It seems that one of those variables contains a float rather than a string.

kidscancode | 2019-06-01 16:05