How could i get structure from randomly spawned actor (Spawn actor from class)?
- add the actors to an array as you spawn them, shuffle the array, Get 0
- add the actors to an array as you spawn them, get random int → Get this
- use Get All Actors of Class if you forgot to make an array, get random int → Get this
It really depends on your scenario, so you might need to clarify a few more details to get a precise answer, but I’m going to imagine a probable scenario and base my answer on that. If none of this is of help, then I suggest that you add more detail to your question.
So, if you’re spawning actors/objects from say an array of N different object types, and you don’t necissarily know which type you’re trying to access and you also are not able to have them inherit from the same base class, then you could instead use an Interface.
Below are a couple of links you might find useful (one for Blueprints, and one for C++), but effectively an interface will force any class that inherits from it to implement its functions and properties. Each class you might want to access your data on would implement the interface, so there’s no need to know the actual object’s type.
Blueprint Interface: https://docs.unrealengine.com/en-US/Engine/Blueprints/UserGuide/Types/Interface/index.html
C++ Interface: https://isaratech.com/ue4-declaring-and-using-interfaces-in-c/
So, in your case your interface could contain your struct and perhaps a GetMyStruct() method to retrieve the data.
Then, when you get one of your spawned actors you could perform a check to see whether or not it inherits from, and therefore implements the interface.
In C++ your code might be something like this when attempting to acces the data:
IMyInterface* ObjectInterface = Cast<IMyInterface>(OtherActor);
// Check to see whether the cast was succesful
if (ObjectInterface) {
// Then call the Interface method on the actor which will run its own implementation of the method and can return your struct
MyStruct ReturnedStructData = IMyInterface::Execute_GetMyStruct(OtherObject);
//... do something with ReturnedStructData
}
Are these actors of the same type, or of different types?
Implement an interface first and then you can use the above.
I am trying to get structure variable out of that random actor
And that’s exactly how you’d it… Get a Random Actor to get to its variable. What am I missing?
If you’re using an Interface as @Greywacke suggested, you can use the bottom bit to fetch actors implementing a specific interface.
There also may be some alternative, potentially more efficient ways of handling what you need, but you will need to include more details first.
I forgot to mention that these actors are different class. They are not the same, but always share same variable structure inside.