How to get Random row from DataTable then cast selected column to other blueprint actors

Hello everyone,

I am currently looking for a solution for my project, I have watched countless tutorials and looked through the forum with no exact answer. This is my first time posting and I hope someone will be kind enough to help.

I need to use unreal engine 4.27 for a coming event where there will be a lucky draw for employees. I need to be able to:

  1. Be able to import all Employee Name and ID to a data table (this is done)
  2. Create a Blueprint Actor to get a random row from the data table (done also)
  3. Select the UserName and UserID from the randomly selected entry and cast both strings to another Blueprint Class (issues here)
  4. Receive the string data on the Blueprint class that will display the result as a 3D Text (also issues here)


Main Problems:

  1. I can get Random Row or Specific Column, but can’t seem to get a specific cell (Get this string from this column from the random selected row) Most video tutorials show you how to print string data from a row or a column but none show how to print string the result of a specific cell.
  2. Cast the string to another blueprint, and also receive the string data on the new blueprint from the original blueprint.

Will really appreciate any help. Cheers

The Data Table holds a bunch of structs, each entry is a column, break the struct to get your data:

Cast the string to another blueprint, and also receive the string data on the new blueprint from the original blueprint.

You use the word Cast as if it was a method of communication in itself. It is not. Cast is a test + conversion. It tests whether the object is of a specific type and then converts it to that type so you can access it in a way unique to that object type.

In order to cast, you need a reference to the object first. In order to obtain a reference, we need to know where the object is.

Where are blueprints B and C? How did they make it into the world?

  • placed in the scene manually? How many are there? Do they differ in any way?
  • perhaps they’re spawned dynamically with Spawn Actor from Class? If so, where, what and when spawns them?
  • how is the interaction supposed to happen? What triggers the data transfer from the DT to the actor? Begin play? User action? An event? If so, triggered by what?

So many unknowns! :smiley:


Or are you literally talking about casting a string:

image

Even so, as you can see above, you still need a string first.


Do note that this can work the other way round too, the BPs B & C can pull the data out of the DT instead. It really depends on the desired result.

1 Like

Thank you for taking the time to reply, the first part works wonders! I can now basically complete my project if everything is within one single blueprint actor.

For the “casting” bit first forgive my lack of knowledge:/
Since I am still very curious, I will try to explain in more detail.

Both BPs are already in the scene/level.
BP A will have a trigger (for example keyboard press K) which will execute the first part (randomly get the UserID/winner from the data table) then BP B or C (same use anyway) is a BP Actor with a 3D Text component which should display the input from the string as a 3D Text when lets say I assign it to keyboard press L.
So press K = get random entry, then press L to display entry as 3D text.

To simplify here is a screen shot, what would be the best way to split that execution between two blueprints.

Thanks again! :grin:

Both BPs are already in the scene/level.

You have many options:

Direct reference:

Create reference variables in the BP_A that can point to BP_B & BP_C, ensure they are flagged as Instance Editable:

Those references are currently null, invalid - not pointing to any object. Since we have actors in the scene already, the BP_A can now tell which object will be referenced by which variable like so:

No casting required.


The Infamous Get (All) Actor of Class:

The above is somewhat clumsy and will only work well enough if there is only one actor of the specified class or you have a method of differentiating between them when using the All variant of the node. Setting the value of a text component can be done using the obtained reference.

Also, providing you choose a class from the dropdown first, you can easily promote pin returns - probably the easiest way to create variables of the correct type:

It is also possible not to create a hard reference and use an Event Dispatcher at this point - much safer. If you never intend on destroying any of the actors, probably irrelevant.

No casting required.

The less Infamous cousin - Get All Actors with Tag:

You can give actors (or components) tags:

(Almost) any blueprint can fetch actors from anywhere with a tag query:

Again, things get iffy if there’s more than one actor to fetch. Do note how the top bit requires casting since the node did not specify a class - it will return a generic actor. Casting will test whether it’s what we wanted, convert to the type we need and we can then assign actor to the type correct reference.

Use the Level Blueprint:

Any instantiated actor that is present in the scene can be referenced directly in the LB:

Alternatively, select it in the World Outliner and right click in the LB:

From now, more options open up:

image

You can dig into how Event Dispatchers work to efficiently send data between those actors. Do tell if you’d like to see an example of that.

Again, no casting required.


One note, we’re not making a game here right? Even so, processing user input inside an actor is generally not a good idea. Consider setting up a Player Controller - probably the most natural place to handle input. You may be OK with an actor intercepting keypresses - kind of depends on the scope and what the end goal is.

You could also use the LB as the input invoker, as seen above.

1 Like

I got everything working flawlessly. Thank you so much!

Yes this is not for a game but for a virtual event, the “keyboard press” is only for testing, the final trigger will actually be passed on to a third party software, I like having everything within the BP actor so that I can just copy paste it to future projects :grin:

Thanks again!

1 Like