Simple dialog box based on clicking an NPC (like in Rimworld game)

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

Hello…

Spent 3 days trying to get this right, hoping someone can help.

I have a ‘Being’ class. It is the NPC’s.
I have a ‘BeingHub’ which is the canvas layer that is the hud and various menus.
I have a ‘mainPlay’ class - it is all the nodes together to form the game.

I am trying to implement: When you click on NPC (Being), a little window (BeingHud) pops up. And then when you click on a button on BeingHud, the little window hides. I have the hide working with no issue via a signal between BeingHud and mainPlay.

What I cannot get working … is when you click on a Being, I need the little window to open. I am for sure - able to get the being selected. I’ve done this code in the Being script.

func _input_event(viewport, event, shape_idx):
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
emit_signal(“beingwasclicked”)
print("clicked on " + firstname)

Ive realized, the mainPlay node is not picking up the signal.

Is there a better way to implement such a simple thing?

:bust_in_silhouette: Reply From: mattkw80

I’ve got this working, but it’s fairly sketchy I think.

I created a variable (singleton) in autoload called npcselected

When an npc (being) is selected it’s true, other wise it’s false.
When it’s true - the process(delta) of BeingHud .show() otherwise
it will .hide().

Pretty sketchy. I can’t imagine this is proper.

Somebody sent me a really great reply to this… I received it in email , but it’s not showing here…

mattkw80,

Your question on Godot Engine - Q&A has been answered by end
ragor:

You have omitted in your example how you determine which cha
racter is clicked. You have to ray-cast from the position of the click into
the world and see which objects are intersected by the ray. Then, you may
dispatch the click to the object closest to the ray origin (or have some ot
her logic to determine which object has to accept the click, if multiple ob
jects are intersected). Ray-casting from screen is described here: https://
Ray-casting — Godot Engine (3.1) documentation in English
g-from-screen . The way to organize this in code is, for example, to have a
WorldInputManager that implements _unhandled_input() and stores mouse clic
k positions, which are then processed in _physics_process (which is where r
ay casting should happen). _unhandled_input must be used to ensure that you
are not handling clicks that were already processed by UI elements. When W
orldInputManager determines that a ray intersects a clickable object, it di
spatches the event (calls a method) on the object in question. The object m
ay then display a HUD (BeingHud) near its location. To ensure there is only
one HUD shown at a time, showing a HUD would also have to be centralized i
n some kind of UIController.

Your question was:

Simple dial
og box based on clicking an NPC (like in Rimworld game)

If you lik
e this answer, you may select it as the best:

http://godotengine.org/q
a/59165/simple-dialog-box-based-on-clicking-an-npc-like-rimworld-game?show
=59193#a59193

Thank you,

Godot Engine - Q&A

mattkw80 | 2020-01-20 15:07