- Edited
Hi everyone, I am trying to "plant a crop" when clicking a tile. So far, I have been successful at instancing the crop but it always shows up at the top left corner of the game window, never at the position of the mouse click. From what I was able to research on Google, this is what I have come up with so far:
func _unhandled_input(event):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT and event.pressed:
var clicked_cell = world_to_map(event.position)
var tile_type = get_tile_type(clicked_cell)
if tile_type == "Dirt_Dry" and DIRT_PLANTED == false:
var crop_path = "res://Crops/Turnip.tscn"
var crop = load(crop_path).instance()
crop.initialize(crop_data, clicked_cell)
crop.position = Vector2(clicked_cell)
add_child(crop)
From what I understood was, crop.position would set the Vector2 position for the crop, and then I would simply use add_child. But I must be missing something. I added the "clicked_cell" to the initialize function for the crop just to test if it is getting the right coordinates, a print statement inside the crop's initialize function confirms that it is getting the coordinates:
Initialize function in Turnip.gd (which inherits from Plant)
func initialize(crop_data, clicked_cell):
.initialize(crop_data, clicked_cell)
crop_position = Vector2(clicked_cell)
print("Newly planted crop position is:")
print(crop_position)
$Anim.play(str(0))
Sample Output:
Newly planted crop position is: (0, 3)
So it appears the crop "knows" its position put still just shows up in the top left corner.
The Turnip (and Plant) is an Area2D node with AnimSprite, Sprite, and CollisionShape2D.
Thanks for any pointers anyone can give!