Hi
I’m not sure what to connect the object node to when casting to another bluprint to access a variable. I’m guessing I need to create reference to what I need to connect to the object node to but I can’t seem to get it right. I’m trying to print string a value of a variable (shopWheatStock) in another bluprint (BP_buildingseedShop) from the ThirdPersonCharacter BP.
I’m guessing I need to create
reference to what I need to connect to
the object node
Precisely. Think about the blueprint where you SpawnedActorFromClass for the BuildingSeedShop.
You should have created a reference to store it, you will need to access that blueprint and get the reference from there.
I’ve never used SpawnActorFromClass. How do I go about doing that? BuildingSeedShop is a child from BP_MasterBuilding, which I also just added to the level directly.
Typically when dealing with casting, I will get the object reference from a collision event. There is an alternative method that I commonly used:
This allows you to call up each actor of a certain class and get the object reference. You do not need to use the cast to node, but it will call up the actions for every object within that specified class.
While this will work for certain scenarios, it is considered bad/sloppy practice. You cannot tell what order you’re getting your objects in, for example. For as long as you are able to manage your objects using this node, you’re fine, I guess. Maybe I’m biased.
If you have a map where everything is already placed and static (you did it in the editor, manually), you should consider doing the logic in the LevelBlueprint - all references are already there. Since the actors you’ve placed only exist in that very level, you do not even need to cast to anything. You basically drag the object reference from the persistent level into the graph, done. If you ever need your character, you get the global reference to base class and cast it back to your own, derived.
But lets say you’ll get what you want, and you obtain the reference to plug into your Cast node. It’s going to work in this level, sure. Then you go to level 2, objects are different there, your character is the same, though. Your press “N” and that Cast is going to fail like there’s no tomorrow.
Have a look here for more info.
SpawnActorFromClass
Imagine you’re doing an RTS and the players will add buildings to the game all over the map. You cannot design this upfront. The buildings are placed where they click. SpawnActorFromClass allows you to dynamically add an object to the level but you need to store your own reference.
The actor is spawned, reference created and you can use it straightaway for all sort of things.
Again, it all depends what kind of game you are creating. Logic that is unique to a level can be kept in the LevelBlueprint. If you need to add something on the fly, you can spawn it dynamically.
If you want to identify something in the level with your character, the suggestion that @Poneill mentioned is a solid one. Either the player will click on something, your will collide with it or there’s going be some tracing. You can obtain actor reference this way and then cast it.
Thank you. I’ve moved a lot of the logic to the Level Bluprint and it works. I think I’ll try to do as much of the logic in the Level Bluprint as I can, because Yes, I do pretty much have everything placed out on the map. And that way I won’t need to do as many casts. I just drag the object reference into the graph whenever I need to use something from it.
I couldn’t put everything in the Level Bluprint, though. For example, I have widgets that pop up with trigger boxes for my buildings. And when I bind a button or a text box in the Designer, it takes me straight to the widget Graph and from there I need to cast to other bluprint actors to access their variables. And for some reason, I couldn’t access a couple variables that I moved to the Level Bluprint. I could access pretty much everything contained in other bluprints working from the Level Bluprint, but when I tried working from inside other bluprints I couldn’t access any of the variables that are in the LevelBluprint. And it also didn’t give me the option to cast to the Level Bluprint. Would there be any way around that? In the widget Graph, I wasn’t able to get a variable that was in the Level Bluprint. For this particular problem, I moved the variables back to their original bluprint (BP_buildingSeedShop) and tried Poneill’s method using Get All Actors Of Class and For Each Loop, which worked. (Thanks Poneill!!!)
If there is a way to access the variables in the Level Bluprint from inside other bluprints please let me know. The more I can have in the Level Bluprint the easier.
Thank you, Poneill. This seems to be working well with a lot of what I am trying to do.
I am new at this and even though I have no idea about how object references work, this method seems to be working well.