Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | doogragdaba |
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
@Strawberry@2
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")
asetyowatir | 2020-10-23 09:35
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.
doogragdaba | 2020-10-23 13:32
at this line crop.connect("plant_clicked", self, "_on_plant_clicked")
trs change self
to get_parent()
then try run again
asetyowatir | 2020-10-23 13:55
I tried that after your first comment and no luck. get_parent().get_parent()
also didn’t work
doogragdaba | 2020-10-23 13:58
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
asetyowatir | 2020-10-23 14:16