How can I assign / manipulate actors data at runtime?

Hi there,

In my project I’m spawning actors at runtime with essentially an actor spawning gun. Imagine I spawn an NPC, then after this a UI pops up with fields for entering information about the NPC such as backstory and wealth, name and other things like what weapons it has.

I’ve been trying to work out how this UI can grab data from the last spawned actor (NPC) and enter new information about it where it will be saved. All of these actors seem arbitrary so I’m unable to just do ‘get all actors of class’ because I still can’t get the last one created and assign that one specifically this new data.

I’m at a complete loss here I think.

Will there ever be a scenario where two or more NPCs show the UI pop-up simultaneously?


What exactly is stopping you from creating the widget right after spawning an actor? Could that work? Or (relating to the 1st question), why not have the spawned actor create a widget for itself? This makes comms a breeze.

1 Like

Hi, first scenario will never happen. Second scenario is already happening. The NPC spawns the UI form to pop up. The issue I’m having is putting that data in the form and have it apply to the NPC actor specifically.

This is a job for the event dispatcher.

Could you elaborate on this? An example? Like punch in a name that we transfer from the widget to the NPC.

So to extend on this, the process is I spawn the NPC with my NPC spawn gun. The NPC then spawns the widget for me to fill out then I press confirm and the widget is removed from parent and I’m left with the NPC.

Ideally that NPC then takes data from that form and becomes the variables I’ve just set it including name and such. However these variables also need to be saved to some kind of data table which I can export out as a .txt or something at runtime,

The project is quite interesting if not slightly convoluted

  • in the widget:

  • in the NPC:

You can, ofc, pack all the data in a struct and send the whole thing instead. This is just an example.

You will need need a plugin for that.

1 Like

I’ve been looking at the plugins which will send this data to a CSV file. It all needs to be saved and send to a server where the CSV is incorporated into the database and that will be used for new instances of the game with these premade NPC’s based on the users creations.

Experimental for sure but it sounds cool.

I will give your solution a try and see if I can manage to get it working.

Otherwise how about the other way around?

If I have the form first, fill it out which then saves it as a data asset/ data table or something and the NPC which is spawned after confirmation takes on the properties of this data?

edit: Also you are blazing fast at prepping these blueprint screenshots. You’re brilliant

Where is the form created? Outside the engine? In a data table? Run-time?

It’s created at runtime in the game. So if I change my NPC gun to spawn the form first, then the form spawns the NPC

  • keep adding the data to a map variable
  • spawn actor, create widget (optional? still needed?)
  • look up the key in the map, get struct → push data in the actor and / or widget

Spawning actors from data table / map structs is one of the more common things to do. So you’re in luck.

1 Like

So with that in mind, does this mean:

create a map in the widget which takes the information from the fields/ variables

Confirm information which spawns the NPC

NPC look up the key in the map, get struct and then push this data to the NPC for it to take form

all this data in the map is also saved to a CSV upon completion.

Am I understanding this correctly?

Not in the widget, they’re too volatile. If this is supposed to work as a central database of sort, consider one of the framework classes - the Game Mode could work. Anyone can access it any time. But you could keep it in the gun, too if that’s more convenient.

I’d do it before the NPC is spawned:

Find all the info it needs to know about and feed it as it spawns. The actor can then, internally, transfer it to its widget.


Note sure I follow you fully since you wanted to do it the other way round. Have the data, and then create an actor + widget based on that.

1 Like

Ohh ofcourse! My mistake.

This seems like a good solution I think I’ll go with this way around. Yes it’s supposed to work as a central level-based database. The level itself will hold the data, so if someone else downloads the level they will also have those NPC’s previously created spawned there with the correct data. Ofcourse all of this data needs to be saved out to a CSV on a central server so I’ll work that bit out.

Rather than game mode, maybe something more level-based rather than global. Would casting to an actor in the world be a bad idea to send/ retrieve data from for each NPC?

1 Like

Never used these:

Perhaps that’s what you need?

1 Like

Could be, I’ll look into them

Thank you :slight_smile:

Hello again,

I’ve since refined the project and I think I can more clearly explain what needs to happen.

As it currently stands I am using the input game mode + UI type system to control and interact with both the 3D world and UI elements.

It goes like this:

  1. Click Create button which pops up a form for data entry, while simultaneously equipping my pawn with a ‘spawn npc’ gun.
  2. The data I enter into this form now transfers to the variables of the spawned NPC. So if I enter into the form that this NPC has a red shirt, spikey hair and boots it will spawn with those traits.
  3. I need this form to store itself as an editable item that I can access via my UI
  4. When I change the data of this form, it needs to update the associated NPC

Do you know how I should create that bond between the form and the NPC? like ‘Form 1 is responsible for NPC 1’ at all times, at runtime and accessible through clicking it in my saved forms box in the UI. If I click ‘form 1’ then I should be able to immediately edit the details, click complete and it will update my NPC.

Is this a thing?

I think if I know how to create this association or bond/ link then I can do the rest myself such as the form appearing in the UI in an ever updating list of spawned NPC’s.

Edit:

Perhaps what should happen is that there are editable parameters on the NPC itself. So if I click the NPC to select it at runtime, the params appear in the menu and I can update them directly and apply them. I can send this data out to a CSV for later use. Do you think that might be a better idea?

Use Event Dispatchers to communicate between the UI form and the spawned NPC. Probably the cleanest solution in which the NPC does not even know the form exists. There may be no need to manually reference the forms since they’re supposed to persists in the UI.

1 Like

Oof yea, event dispatchers. I might have to rework some things, I’ve been sending info to an enum which updates a map from UI casting to the NPC lol