Unsure of how implement this idea

In the game I’m creating, the player is able to interact with multiple objects and NPCs using the E key. (Sort of mmo-style) I had an idea of how I’d like it to work in my head but when it came down to implementing it, I was unsure of how to make it work.

Here’s the code I’m using so far

     void Update () {
		if (Input.GetButtonUp("Interact")) {
			Transform cam = Camera.main.transform;
			RaycastHit hit; 
			Debug.Log ("Attempting to Interact");
			
			if ( Physics.Raycast (cam.position, cam.forward, out hit, 5))
				Debug.Log ("Found Object");
				// Unsure of which way to go from here
		}
	}

The way I had it in my head, I wanted to use the hit.collider to see if it had a script called “interact” attached to it and if it did, call that script’s onInteract() function.

However, I’m not sure if this is feasible. Some obvious concerns would be that having million different scripts named “interact” could get confusing. Though, with proper folder structure, possibly not.

Does anyone have any recommendations for me on this? Another way to implement the idea or an easier idea altogether?

My RPG class structure goes like this:

interface Interactable { function Interact(); }
class Actor implements Interactable;
class Man extends Actor;
class Dog extends Actor;
class Container implements Interactable;
class Chest extends Container;
class Inventory extends Container;

All of those classes are Interactable, so all of them can have .Interact called on them.

Unity also has an Easy Button for this:
targetGameObject.SendMessage(“FunctionName”, parameter);
SendMessage calls all functions of that name attached to all components of the target object.

I love the Easy Button solution :P. I was trying all sorts of things with GetComponent and couldn’t quite get it to work right.

I do have one question, though. Are you defining the interact in Actor() or just doing a blank interact method there and then defining it in the child class? Because a Man and a Dog would have two very different Interact functions. But, you HAVE to implement Interact() in some way in the parent class or else you can’t compile.

I do define it in the Actor (it triggers dialogue by default, since most actors in my world can speak), but for something like Dog I just override the function, rather than ‘extend’ it using “super”.

Do you know the C# equivalent to something like this?

The problem I’m running into currently is that Actor currently has a blank interact method, because at the moment I cannot think of an interaction ALL of my actors have in common. I do however, have a specific Actor that I’ve setup an Interact for but am getting compile errors because I can’t override the Actor::Interact because it’s a child class.

This question might actually put me in the right direction. I assume your Actor Interact method creates the dialogue GUI and pulls the actual dialogue from the child(Man)? If that’s the case, then that might be where I’m running into the issue. Rather than trying to override the Interact function in this object, I may need to do that?

Thanks for the input, it really helps.

I haven’t used interfaces a lot but shouldn’t it be implemented at the level where you actually need it ? Interfaces are like contracts that owners must fulfill. Since not all actors have an interaction, you should implement Interactable only on subclasses of Actor that are eligible to this (like Actor → Merchand (Interactable)).

Edit : you could implement the method in Actor with the virtual keyword, also.

^ Implementing only on interactable child classes is a good idea too.

It’s either virtual public or public virtual before the function return type to allow child classes to override the function.

v p void Interact()

You might want to do what you suggested - implement an empty or warning-generating interact on Actor, then extend it on Man to do dialogue.

virtual public void Interact() {
  Debug.LogWarning("Interact called on pure Actor class!");
}

I actually have a dialogue ‘database’ object which runs dialogue on its own, based on actors. It uses actor names and game states to determine what dialogue to display, rather than having dialogue “on” an Actor. Essentially the benefit of that is I can talk to ants now, but I mostly created it because I have no ability to stop making complexity. xD

Get a bit carried away, Loius? xD

Last night I went ahead and made it virtual in Actor. However, Unity complains a bit. But whatever, this is just a practice project anyhow.