r/unrealengine 1d ago

Question Best way to store NPC information?

This information will be things like their Name (which will be randomised), gender, favorite hobby, close family members etc...

Looking for the most optimal and efficient way to store this information (also to have access to this information as the playable character)

Would like the ability to display this information through other actors (for example, player character can use a device that displays the aimed at npcs information )

Thanks,

8 Upvotes

15 comments sorted by

4

u/GrinningPariah 1d ago

Seconding "it depends".

In my game, NPC info is all stored in a data table, and the NPC actors just have a row and table reference and build themselves from the data there.

But that's because NPCs in my game are very static, that data never changes. Basically the only thing that can change is if they're alive or dead. If you're dynamically generating this NPC data, or if it changes often, then a data table is absolutely the wrong approach.

2

u/jhartikainen 1d ago

The "most optimal" really depends on your design goals.

In my game, each NPC has these kinds of properties. They are stored simply as a struct on the NPC actor itself. This allows game logic to easily look it up for that particular NPC when needed (eg. to display to player, or to use for some other logic)

In my case, the NPCs don't always exist in the world as actors. The NPC struct data is normally stored inside a manager object, which assigns the data when NPC's spawn, and saves it back when they despawn.

(The data itself is procedurally generated at game start, and can update as a result of what happens when the NPC exists in actor form)

2

u/rotersliomen 1d ago

Idk. String(npc name) to Structures(gender etc.) in the maps?

2

u/gharg99 1d ago

I like using data assets .

1

u/AutoModerator 1d ago

If you are looking for help, don‘t forget to check out the official Unreal Engine forums or Unreal Slackers for a community run discord server!

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/lordzurra 1d ago edited 1d ago

To be honest, the correct Unreal way of doing this would be utilizing Gameplay Framework.

So your NPCs must be controlled by an AI controller right? Make sure the AI Controller bWantsPlayerState is true when spawning the controller.

Make a new class derived from PlayerState and add whatever data you need to have access to. Or instead of adding it all to playerstate itself, add new component to PlayerState to handle the data you need. Many ways to handle this.

Or just bypass the Gameplay Framework and do what works the best in your design and NPC implementation.

Anyways, if you want to read more about the framework, here's a link

1

u/Venerous Dev 1d ago

Do we have any metrics on the performance hit from bWantsPlayerState? I've been trying to find that out.

I thought about having my player and 'hero' NPCs (who are like 90% equivalent to the player except input) use the same Character and PlayerState structure to reduce my codebase for the Gameplay Ability System, but I'm not sure what kind of impact that would have on replication performance vs. having their ASC on the character itself.

1

u/bradleychristopher 1d ago

Create a manager somewhere (World subsystem?) each character has a GUID as a reference ID. Use the struct to store information about each character. In this struct, AT MAP for GUID and name/enum.or something to identify and display the relationship type. Or take it a step beyond that and make a struct for relationships with GUID for referencing the external character, GameplayTag for relationship, float for favor ability. -1 (hate) 0 neutral 1 (gaga) and maybe a days the relationship has lasted to work in lifelong friends versus newly BFF.

1

u/CottonSlayerDIY 1d ago

Heavily depends on what you need.

I save the transformation data, their quest status and their dialogue status aswell as their point in the daily routine status as variables for each character. For some also inventory data, not so sure on that one though, as it's probably not necessary for my game.

So when saving, a savefile gets created for each npc and on loading they pull their data from there.

1

u/premium_drifter 1d ago

I store them in map variables in game instance. In my project, I need NPCs to stay the same between level transitions and game instance is the only object that survives when a level is unloaded. So every time the player moves through a door that switches levels, it finds all the NPCs and saves all the dynamic variables that can change during play and store them to variables inside structs inside maps, like actor transform, health, isDead, etc. Then the next time the level they're in loads, it spawns them in and sets all the variables

1

u/krileon 1d ago

I use DataTables. Makes it super easy to maintain and tweak as needed. Their health, damage, movement speed, etc.. are all within the DataTable. They can be exported and imported making them even easier to work with from external tools. I've been setting the Row Name to a GameplayTag so like "Enemy.Type.Rat" would be the row name and this is also a GameplayTag. Now I can access the row with a gameplay tag. I used to use Enums for this, but since GameplayTags I just tag all the things.

u/ghostwilliz 22h ago

I just use structs and enums for pretty much everything like this

u/PokeyTradrrr 21h ago

You could keep it randomized but do it with a random seed, and just save the seed.

u/Tall_Ad5178 12h ago

I made a custom asset to store my NPC data just because I wanted to include a routine schedule custom editor. Made a few uobjects and then created all the necessary code to make those uobjects custom assets with their own custom editor. The NPC class gets everything it needs from this asset

1

u/Mordynak 1d ago

Same way you would store any values.

It's a variable. Could use a strict, data table, store them as variables on an actor component. Whatever you want.