Why can't I print?

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

I wrote a simple function to create a random string of 3 letters of the alphabet. I’m trying to print that string to the console but I’m getting the error:

modules/gdscript/gdscript_tokenizer.cpp:1129 - Condition "tk_rb[ofs].type != TK_IDENTIFIER" is true. Returned: StringName()

Here’s the code:

extends Node2D

var decoded_string : String = create_letter_string(3)
print(decoded_string)

func create_letter_string(num):
	randomize()
	var alphabet := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	var random_letters : String
	
	for i in num:
		random_letters += alphabet[randi() % 26]
		
	return random_letters

I can print the string inside the function, but not outside. I can’t print anything actually. My scene has only one Node and a script attached to it.

I’ve been trying to research it more myself. It looks like I need to put the first var and print() inside the _ready() function, but I’m not 100% sure why.

andre_angelo | 2021-10-31 06:27

:bust_in_silhouette: Reply From: Ahmed

Hi, in order to use that print methud in your code, you have to put it into a function.
Please check this link Godot was imperative from the start to know why you can not use Imperative programming style in Godot.

for example: _

# Called when the node enters the scene tree for the first time.
func _ready():
	print(decoded_string)