How can I get information about a game object by clicking on it in game?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mattkw80

I’m trying to make a simple 2D Rimworld type game.

I can click on NPC’s and a window pops up. (Where the NPC info will show).

I’ve been struggling for months now - I cannot figure out how to get the NPC’s stats to show up in the window.

Can anyone give me an overview as to how they would approach this?

I can click on NPC’s and a window pops up.

Which node-type are you using for the window?

njamster | 2020-03-19 15:40

I’m so sorry I have not answered - I will get back to you on this and appreciate your taking the time to ask me. I’ll get better details and comment back asap - I really want to get this resolved as it’s holding up my ‘Godot’ learning and has been for months.

mattkw80 | 2020-03-21 14:51

To try to explain…

When an NPC is clicked, a rectangle pops up on the screen,
and has as many Richtext labels as needed

  • this is where I want list all the stats of the NPC.

The rectangle lives in a node called HUD…

HUD / Area2D / ColorRect / RichTextLabel.

I guess the question regardless of controls used…
once I’ve clicked the NPC… how do I go about
populating all the little RichTextLabels with the stats?

It’s hard to even explain what I’m trying to do here, sorry.

mattkw80 | 2020-03-21 15:44

So your problem is about assigning text to a RichTextLabel from a script?

njamster | 2020-03-21 16:04

No I’m good making controls do things.

It’s a matter of… I’ve click my NPC object… the rectangle (NPC information form) pops up, and not I want to populate it with all the NPC data, of the NPC that was clicked.

Im not sure I how reference the actually NPC. Let’s call him Bob. I click on Bob, the form comes up, I’m missing a probably very simple concept where the NPC information form doesn’t know I clicked on Bob so doesn’t know which NPC to pull values from.

Another way I might be able to tackle this - is if I could find a Godot example where someone else is already doing something similar.

mattkw80 | 2020-03-21 16:46

:bust_in_silhouette: Reply From: njamster

Alright, let’s say our NPC-scene looks like this:

- NPC (type: Area2D)
  - Sprite (type: Sprite)
  - Hitbox (type: CollisionShape2D)

Next, attach a script to the root-node (the one we called “NPC”):

extends Area2D

signal open_menu
signal exit_menu

var stats = {
	"health": 10,
	"max_health": 100,
	"occupation": "Game Developer"
}

func _on_NPC_input_event(viewport, event, shape_idx):
	if event is InputEventMouseButton and event.pressed:
		if event.button_index == BUTTON_LEFT:
			emit_signal("open_menu", stats)
		elif event.button_index == BUTTON_RIGHT:
			emit_signal("exit_menu")

We have three stats here (“health”, “max_health” and “occupation”) grouped together in a dictionary called stats. The function has to be connected to the input_event-signal of the Area2D-node, it’s the first entry under “CollisionObject2D”.

Now create another scene that looks like this:

- Root (type: Node2D, doesn't matter much)
  - NPC (inherited from the scene we just created)
  - Menu (type: Label, for simplicity reasons)

Add a script to the root-node here as well:

extends Node2D

func _on_NPC_open_menu(stats):
	$Menu.text = "%s / %s, %s" % [stats["health"], stats["max_health"], stats["occupation"]]
	$Menu.show()

func _on_NPC_exit_menu():
	$Menu.text = ""
	$Menu.hide()

Make sure you connect the open_menu- and exit_menu-signals of the inherited NPC scene to the functions in this script. If you now run the scene and click inside the area of the NPC’s hitbox, it will emit the open_menu-signal along with it’s stats. Which are then grabbed by the “Root”-node, converted into a string (using format strings) and assigned to our label called “Menu”. Clicking with the right mouse button will emit exit_menu, thus reset the labels text and hide it again.

Thanks in advance. I’m going to put this all together right now and see what happens.

mattkw80 | 2020-03-21 21:07

Thank you njamster !

This works great.

What I forgot to mention was my NPC’s are generated on game start in code - I actually load 1000 of them. So I did what you said but then also made sure to connect my signals upon initialization.

At long last this is working for me, thanks to you.

mattkw80 | 2020-03-21 21:48