0

I'm trying to transfer data from one scene to another for a simple modal pop up. I'm using a sensor to sense if the player passes an area (Using a node with a collisionShape2D) and then updating a variable(named Collision) cross nodes to make the "Modal" appear. When I try to update the variable, I get this error "Invalid get index 'collision' (on base: 'String')"

Collision Detector Code

extends Node2D

var collision: int = 0 

# 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 transerData(old, new):
    new.collision = old.collision
    

func _on_area_2d_body_entered(_body):
    $Sprite2D.visible = false
    collision = 1
    transerData("res://Area2d.tscn", "res://Modal.tscn")

The Modal Code

extends Node2D

var collision: int = 0 

# Called when the node enters the scene tree for the first time.
func _ready():
    $Sprite2D.visible = false # Replace with function body.
    position = Vector2(617, 313)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
    if(collision == 1):
        $Sprite2D.visible = true

func transerData(old, new):
    new.collision = old.collision
    

Note: When I hover over the variables it shows the correct values, though the modal also does not set Sprite2D's visibility to true. ???

1 Answers1

0

Read your code. Here:

transerData("res://Area2d.tscn", "res://Modal.tscn")

You are calling transerData passing two Strings.

So here:

func transerData(old, new):
    new.collision = old.collision

The parameters old and new will receive Strings. Specifying the types of the arguments might allow Godot to you give early warning of this situation.

And the error is correct, there isn't a collision member in String.

Note: As far as I can tell transerData in modal is not being called. Instead the collision detector is calling it own transerData, and that's it.


I cannot give you specific advice for how to do this because I do not know how your scene tree is setup or how the scenes are being instantiated.

Thus, the generic advice: If you do not have a reference to the other object (or objects) you need to communicate with, use either: a Signal Bus or Resource Based Communication.

Theraot
  • 31,890
  • 5
  • 57
  • 86