I need help with setting signals

Godot 4.0.1

Hi! I am trying to make my script comunicate but godot is testing my sanity right now. I am trying to make game where you try to guess correct color combination, something like wordle but no words just colors. Every color is called a token and it is just a seprate scene that has a script. Script changes texture of 2d animated sprite (right now those are just some textures with numbers) when the button is clicked, but have a variable that keeps track of which color the token is. Then I intend to have logic of the game in main scene with the game. I figured how to emit signal from the token to main scene, but I have no idea how to use that variable. Right now it seems like code sees TokenIndex variable as a null all the time. All the help will be apreciated!

TOKEN SCRIPT

extends AnimatedSprite2D

var Textures = ["token_0","token_1","token_2","token_3","token_4","token_5"]
var i = 0
var TokenIndex = i

signal Token(TokenIndex)


func _ready():
	pass


func _process(delta):
	$".".play(Textures[i])
	TokenIndex = i
	emit_signal("Token")


func _on_texture_button_pressed():
	if i < 5:
		i = i + 1
	else:
		i = 1

GAME SCENE SCRIPT

extends Node2D

var t1 
var t2
var t3 
var t4 
var t5 

var rng = RandomNumberGenerator.new()
var g1 = rng.randi_range(1, 5)
var g2 = rng.randi_range(1, 5)
var g3 = rng.randi_range(1, 5)
var g4 = rng.randi_range(1, 5)
var g5 = rng.randi_range(1, 5)

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

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	pass
	
func _on_button_next_pressed():
	pass

func _on_token_token(TokenIndex):
	t1 = TokenIndex

func _on_token_2_token(TokenIndex):
	t2 = TokenIndex

func _on_token_3_token(TokenIndex):
	t3 = TokenIndex

func _on_token_4_token(TokenIndex):
	t4 = TokenIndex

func _on_token_5_token(TokenIndex):
	t5 = TokenIndex

Not sure where/how you connect Token, but try Token.emit(TokenIndex)

2 Likes

IT WORKED!! THANK YOU! Could you please expalin why it worked??

1 Like

Hello esteemed skugi11,

The syntax you were using was gdscript 1 syntax, which applies to godot 3 and below. Godot 4 uses the newer gdscript 2 syntax, as shown by the esteemed dbat.

Regards,

1 Like