How do I implement a 'Memory' for NPC's and Mobs?

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

This is more of a game design question then a Godot specific question.

I’m making a game similar to Rimworld where every NPC and Enemy has many properties and abilities.

What structure should I use to allow them to see something and take note of it so they can go to it later?

ie: A Hunter wanders the map… see’s some apples, and in the future will know they can return to that location on the map to retrieve the apples.

I already have ‘inventory’ for them - where each piece of inventory is a Scene and they have a list if scenes. I used a Node2d… not actually a list - so I guess my actual question is - should these scenes be stuffed into a Node2D or should I have been making an array or list of Nodes? (The nodes being inventory and memories).

Thank in Advanced for reading.

Thanks all - I’ll review and try all of this in detail.

mattkw80 | 2021-03-30 17:15

:bust_in_silhouette: Reply From: exuin

You don’t even need an Array of Nodes, you can just store Vector2s of the location of the item and the name of the item.

Using a custom class may be more manageable

class_name Memory extends Reference

var points_of_interest = []
var actions_already_taken = []
var items_collected = []

func update():
    pass

func nueralise():
    pass

NPC.gd

extends node

var memory = Memory.new()

func _ready():
    memory.update()

Wakatta | 2021-03-29 20:24

:bust_in_silhouette: Reply From: The_Black_Chess_King

There are many ways to do this, what you want as a ‘memory’ can be a value stored somewhere. You can use Array, Dictionary or Variables. For example, you could do:

var Memory:Array = {
    "Apples_1" = { Global_Map_Coordinates={ Vector2(512,-256) } }
}

This nested Dictionary can be accessed by calling:

Memory["Apples_1"]["Global_Map_Coordinates"] #Would return: Vector2(512,-256)

This is a example of a data structure using dictionaries, which you can make whatever you want and nest them whoever you want. For your Hunter to ‘remember’ where the Apples are is easy, you access them like mentioned above, you would get the coordinates for the Apple_1.

Whoever when the Hunter will be searching for Apples? And why he would want Apples? That’s where you will need a simple AI to handle that, maybe your Hunter will get Apples if FOOD < 20% MAX or HEALTH < 30% MAX.

OR, using your own method of having nodes as a list, maybe you could create a Position2D to them apples, and create a ‘Memory’ node to your Hunter, like you created a Inventory. So it can be a Inventory of Memories in a sense. :smiley: and maybe you can add a Label inside that Position2D to reference a itemname for your Hunter.

enter image description here

So you have the coordinates of the apples from the position2D and the inventory item reference stored in the label.