0 votes

I am making a farming game and spawn an instance of a crop on a TileMap into a container TileMap. I want to be able to interact with the spawned nodes (e.g., harvest). I'm currently trying signals but nothing seems to want to work.

I have a generic plant scene (Plant.tscn, Plant.gd) and scene for the individual crops (e.g., Corn.tscn, Corn.gd) which inherit from plant. I then instance the desired crop on a TileMap (FarmTiles.tscn, FarmTiles.gd) in the container (CropContainer). The script for instancing a crop is in my FarmTiles.gd where I connect the signal.

FarmTiles.gd

extends TileMap
onready var crop_scene = preload("res://Crops/Strawberry.tscn")
    ...
    ...    
    if event.is_action_pressed("Plant"):
                    var crop = crop_scene.instance()
                    var crop_x = loc.x * 16
                    var crop_y = loc.y * 16
                    crop.initialize(crop_data)
                    crop_container.add_child(crop)
                    crop.position.x = crop_x
                    crop.position.y = crop_y
                    crop.connect("plant_clicked", self, "_on_plant_clicked")

func _on_plant_clicked():
        print("plant was clicked")

My plant/crop_scene's signal (Plant.gd)

extends Area2D
signal plant_clicked
    ...
    ...
    func _on_Plant_input_event(viewport, event, shape_idx):
        if event.is_action_pressed("Click"):
            emit_signal("plant_clicked")

My remote scene structure ends up looking like this after planting a couple strawberries:

Root
    Farm
        FieldTiles (TileMap)
             CropContainer (TileMap)
                   Strawberry
                   @[email protected]
in Engine by (16 points)
edited by

is crop_scene a plant script?

if so you are trying to connect the plant's signal to its parent right? you should call connect a signal in plant script's _ready(): function. something like connect("plant_clicked", get_parent(), "_on_plant_clicked")

Thanks for the reply. I edited my original post/code so that it makes more sense. I connect the signal each time I instance the crop on my TileMap.

at this line crop.connect("plant_clicked", self, "_on_plant_clicked") trs change self to get_parent() then try run again

I tried that after your first comment and no luck. get_parent().get_parent() also didn't work :/

you should call connect in plants script, not in crop scene. like in my case TilesController spawn a Tile:

here the script in Tile:

func _ready():
    connect("input_entered", get_parent(), "_on_entered_tile", [tileIndex])
    connect("input_exited", get_parent(), "_on_exited_tile")
    alive = true

then this is the script in TilesController:

onready var tile = preload("res://scenes/Tile.tscn")
#tile used to preload another scene before ready



func _spawn_tiles(words: String, textCount: int, wordListArray: Array):
    #function to spawn tiles with letters initialized
    for i in textCount:
        var tileInstance = tile.instance()
        tileInstance._set_property(words[i], i)
        add_child(tileInstance)
        wordArray.append(tileInstance)

func _on_entered_tile(idx: int):
    #function to be called by input_entered signal to update line points
    if GameController.currentLevel == 1 and GameController.currentCategory == 1:
        get_node("../AnimationTutorial").stop()
        get_node("../Sprite").visible = false


func _on_exited_tile():
    #function to be called by input_exited signal to update line points
    var correct = false
    var startIdxSlot = 0
    var selectedText
    for i in range(wordList.size()):
        if not wordList[i] == get_node("../Label").text:
            startIdxSlot += str(wordList[i]).length()
        else:
            correct = true
            selectedText = wordList[i]
            for j in range(wordRemain.size()):
                if wordRemain[j] == selectedText:
                    wordRemain.remove(j)
                    break
            break

The Tree:

-Main
--TextureRect
--TilesController(script here)
---Tile1
---Tile2

1 Answer

0 votes

Thank you for all the help! It turned out to be another bit of my code that was acting up and preventing the collision from registering... Spent way too much time on this, but definitely learned signals. Thanks again!

by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.