• 2D
  • Instance object at location of mouse click

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!

I apologize for the poor formatting, I was trying to add a tag and accidentally posted the question before I could preview it.

Here's what the two code samples were supposed to look like:

Snippet 1

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)

Snippet 2 (Plant.gd)

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))

The "position" property of the new object is expecting the pixel position, not the tile coordinates, that's why it appears on the corner. Use "map_to_world" to get the pixel position of the clicked tile.

@Pefistep said: The "position" property of the new object is expecting the pixel position, not the tile coordinates, that's why it appears on the corner. Use "map_to_world" to get the pixel position of the clicked tile.

Thank you so much, that makes perfect sense! I did just that and now my positioning is now working and I was able to plant the seeds in the center of the tile! Really happy now.

Edit: Trying to mark your answer as the accepted one but I don't see where to do it. I was able to do it with my other thread so I am not sure what I'm doing here "

I've changed the topic type to a question so you should see prompts on all the posts to mark one as the answer.

@Megalomaniak said: I've changed the topic type to a question so you should see prompts on all the posts to mark one as the answer.

Thank you for all your help Megalomaniak. I will be sure to post as a question next time I have something to ask!

@saku_rpc One similar mistake this reminded me of (that took me 2 days to fix): Careful not to blanket cover your game screen area with a control/menu node. It can cover up then disable all your mouse interactions.