Is there a better way to use instancing/inheritance to define a function differently for each instance?

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

I’m trying to wrap my head around a basic RPG-style dialogue system, where NPCs can give a series of responses to a player based on input, game state–whatever arbitrary condition needs to be checked.

At the moment, I have it worked out so that there’s an NPC_base scene with a script that handles all the nibbly bits of when a player can interact with an NPC and how to write to the dialogue box. Then I have an NPC scene that extends, so that all I have to write for each NPC is a dialogue() function with all the necessary messages, condition checks, and so on.

In practice, this means I add instances of the NPC scene, then go into the inspector, find the panel for its script, and use ‘make unique’ to write out a unique dialogue() function. I’m not super familiar with Godot though, so I don’t know if I’m making this harder than it needs to be.

Is there an easier way to make a base object and then create instances of it with custom code? (Like for example, a way to define what dialogue() does per-instance without having to give each NPC instance its own custom script?)

:bust_in_silhouette: Reply From: JulioYagami

I think the best way to do what you want is through inheritance. For example, you can put at the beginning of your NPC script:

extends "res: //"

Of course, if the dialogue() function is in NPC_base. Now you need to write code similar to this:

var case = 0

match case: