Download

Data from actors

Hello everyone,

I was creating an inventory for my game. All of my items will be actors with data stored in them as variables. I created an inventory struct with just the actor and the amount as pins. How can I get a variable from an actor without reference to that specific actor? Calling a get or something asks for specific actors and I need one that works for all of them since I will be using a variable.

For example, I am trying to create a list of things in my inventory. I broke apart the struct and have the actor. How can I extract the name of the item which is stored as a text variable in the actor?

I figure that I will need to pull data from actors all the time in the future that have no reference to a specific actor.

One method would be using Blueprint Interfaces.
You would make an interface and declare a function like “GetName” that returns the name. You implement it then in your inventory item class.

As far as I know, there is no way. If you have spent time studying a programming language, you may know that it can be a pain to get data from other places, as you have to have some sort of reference to them. I believe the only way you can do this is by casting using some sort of reference, are you familiar with casting?

He said he has a name reference. So he could use “GetAllActorsFromClass”, iterate and inspect until he finds his actor…
Brute force, but could work…

In other programming languages, it is difficult to grab variables from other places sometimes. The difference is that when coding with C I can type whatever I want. The blueprint system is a little more restrictive, which I can see helps in a way. If I were to try to pull a variable from an actor that didn’t have it, bad things would happen.

Can I call a function when creating my inventory list with a variable reference to an actor that has the actor as input and text as output which triggers an event inside of the actor that was input that then calls a function inside of that actors blueprint and returns the information to the first function which in turn returns that variable? Also, that run on sentence was confusing.

So in my function library it would look like this:

get item name ++++_____+++++++ i dont know how to do this+++++++++++++ReturnNode
+++++++|>===============<|++++++++++++++ |>==============<| ++++
++++item O================O item​||||||||||||| name O===============O Name

i dont know how to do this would trigger an event in the referenced actor that would call its own function to return the name variable

I hope you can understand my ramblings. Would this work and if so, how would I go about it?

So if this is not a thing that is possible, is there another method to store this information? I am fairly certain that people store information on actors or pawns and then retrieve it all the time.

A change just went into the engine today to let you get the ClassDefaults from a class variable reference in blueprints. It will probably be a while though before a release includes it. There’s an experimental naive implementation below if you can’t wait.

All you need to do is store a reference to an objects CLASS somewhere and not store the actual object. In that way you can create data structures that use “data only blueprints” to define data that can be used in your scripts.

For example I just got done making a radial menu system using data-only BP widgets. Each asset is a BP of a custom UIWidget class, and each has a struct of info similar to what you have. Such as the widget name, enums for functionality etc. An array of ‘child’ widgets. In that way the system can destroy and re-create any menu depth by getting defaults from the classes.

Ie, if the user selected “Place Objects”, all current widgets get destroyed (and I use a “struct” of components for that), then the selected widget children get read as simple class references. Then the BP can create each of the widgets from that data at each step. The nice thing about a system like that is the final menu rendering BP doesn’t have to know much of anything about the menu structure and it doesn’t need ANY references to start other than a Root Widget link (and the root’s only settings are its list of children since the rest never gets used).

It will probably be a while before that change makes it to an approved build. I am actually on a separate project that does not have the changelist merged yet so I added a VERY PLACEHOLDER BP node to get class defaults. It can be risky since it will give you the ability to WRITE to the default object which you should NOT DO.

If you need to set up a workflow like this and cant afford to wait a few months here is all you need.

I added this to “KismetSystemLibrary.cpp” and .h.

Add to KismetSystemLibrary.cpp (or any of the kismet library files):


//@TODO RYAN REMOVE THIS TEMP FUNCTION AFTER PHILLIP Ks PROPER VERSION IS CHECKED IN!!!!
UObject* UKismetSystemLibrary::GetDefaultObject(TSubclassOf<UObject> ObjectClass)
{
	if (ObjectClass)
	{
		return ObjectClass->GetDefaultObject();
	}
	return nullptr;
}

Add to matching .h file:


	//@TODO RYAN REMOVE THIS TEMP FUNCTION AFTER PHILLIP Ks PROPER VERSION IS CHECKED IN!!!!
	UFUNCTION(BlueprintCallable, Category = "Utilities|Object")

Warning once again that this code is experimental and probably can do dangerous things if you do anything besides “Get class defaults”. You still have to cast with this node.

The other case that comes up a lot is when you have a reference to something and need to cast to its actual object type before you can access any data.
In those cases you can sometimes make it easier by “pre-casting” which is try to do the cast ahead of time such as on begin play then store an actual object reference so you can access all the variables without casting. May be better for performance and is certainly cleaner in your script.

I was unsure which case you were talking about so I answered both :slight_smile:

If that is your main problem, you should be able to “Cast” to the correct object type. If you first make all of your inventory assets parented off of the same BP (which is just another BP you make first where you add all the struct variables), then you should be able to cast to that shared type for all of them and access the variables. Furthermore you could then refer to each item type simply by “Class” which would be a reference to the class of that exact blueprint name.

Ie you make first “BaseInventoryClass”. This has all the settings all items get.
Then you made a child of that called “InventoryItem1”

You should be able to cast InventoryItem1 to “BaseInventoryClass” and access data that way. Then you don’t have to worry about what type things are.

My suggestion with the above “get default objects” is to refine things to the point where all your player needs to know is an array of Classes that are stored in inventory. More efficient unless you need to communicate to the exact item in invetory. If you are actually drawing the items for the most part it may not be any better.

Thank you very much for your response. I will do a bit of research and attempt to implement your suggestion.

My main problem was not having a function to use to get things from a variable. I can get things from a specific actor using a function from that actor, but I am not able to create a function that works on variables.

I had tried to make a parent class earlier today and my editor promptly crashed. I will try again.

You are much appreciated.

I did some experimenting and tried to make a “master class” that is the parent of all. It did not have the desired effect. I could not give all of my actors the same variable names, so a function that would be called would not know what to get. Even if I could, I would have to name the functions all differently. I just need one function to be able to work for all of my actors.

Maybe I am not able to explain myself clearly. I am a novice and I think that my problem is relatively simple. I hope that what I want to do is possible. All I want to do is bridge this gap:

problem.png

I have several items that I want to pull a variable from using the same function (or some set of functions.)

I think that most people, from what I have seen, save their items as actors. I figure that you would want to save some parameters in these items, such as the attack power on a sword for instance. If this is the case, I will probably need to do this very often as I interact with items in the game. If not, is there a better way to go about it?

RyanB, I found the new change to be very interesting. I understood about 80% of it. Maybe I will learn more when it is released.

Why don’t you simply make the properties such as “Item Name” and Type variables within your Inventory Struct? Then any time you add something to inventory you just pass some info about it. It seems like in this case you already have an actor created so there maybe be nothing to benefit from using the class defaults thing.

I had considered that. I wanted to know if I can pull variables from inside of these actors for the future mostly. I would have to add all attributes from every item such as useable, weapons, armor, accessories, etc, and make a huge struct. Then whenever I want to interact with an item, I would have to find it in the struct. It seemed a bit messy. It also raises questions about how to implement equipment and things. I was also worried about how to make pawns interact with things if they could not access data from each other.

I will do a bit more research. Thank you for the advice. I was just used to doing one of these -> when I wanted to pull data from something but I understand that actors are different from this.

this may help you: How to create Inventory | Shooter Tutorial type can be class.

If you just want to pull data directly from the actors you can do a cast to the object type and then get anything.

But using the parent class makes this a bit easier since currently you may need a different cast for each unique object type.

I tried using casting but I would have to make a node that casts to a specific thing and I need this to work for any variable.

I have just read through the shooter tutorial and it says that he wants to store damage, price, etc in the struct rather than inside of the actor itself. I guess that there is not a way to get data from actors. It’s really not about the inventory at all. I wanted to know this for many other applications. I honestly have no idea now how I will make anything interact with anything else or where to store its data. Like the stats of a mob or something.

There are probably some things that I do not understand. The way in which I envisioned doing things is likely impossible so I will find a new way.

Thank you all for your responses. I appreciate all of your help.

You should look into the get class defaults thing. Then any time you store an item in invetory, also store the class reference alongside the actor.

The only other option I can think of is parent BPs which you mentioned having some trouble getting set up correctly. They were really intended to help this situation.

I will look into the class defaults. It seems really interesting and may be what I am looking for.

The problem that I had with parent BPs is that I had to have different variable names and function names. So I could not have the variable “name” for example on each of my actors. It would have to be 001name, then the function, if I were able to call one, would not know what to look for.

I am just getting used to the blueprint system. I used to program for a MUD in C where all of my characters and objects were data that I could pull things from easily with just a reference to which one I wanted and I could create functions to pull things if necessary.

I will find a good way to implement this and see it through. Thank you again for your time.

So I figured that I would never need to have my items spawned in the world. So instead of referencing the actor, I would reference the class. Kind of like the class defaults things.

I knew that there was a way that variables were taken from actors because to make a struct of variables and pull it to create your inventory menus is possible. I have actually seen this done. How silly of me.

Instead of a kind of get function, I would initiate a “push” function from the actor class called by an event dispatcher. I would need to set up each actor class with these functions.

So here is my question: Can I create an event dispatcher and have 500 different classes where that event is called, but only target one of them?

Im not seeing how event dispatch is related to this but maybe I am missing something. I have never used dispatch for any reason.

I am using class only variables for everything in my radial menu prototype and the structure is pretty much exactly like your image above. The only difference being that i store a class variable not an actor reference.

The whole point of going class only is to make it data-only so i am very confused by the mention of dispatch. The inventory classes shouldnt need any script or events… Unless you need to actually use the inventory Items to run their own scripts but i would not approach it like that. Even if you did, only the current selected items(s) should execute script or get spawned in the world.

Also reading through your previous post,i think you are getting caught up on the difference between placing properties on “the actor” vs a struct. Again im not really understanding the difference since if you place the variables in a struct the actor still has access them. Its not like they went away. I still think simple parent structure is all you need. Im sitting ina hotel bored atm but when i get hmeill Ipad driving me nuts abort sentence