Get a variable from a Class Blueprint that only exists in the Editor not in the level?


So I have this weird question and I’m struggling to find a solution to it because I am not a programmer.

To keep it simple, let’s imagine a top-down view game where you can build some different buildings selected from your HUD. These building types would be different classes, all derived from ABuilding class. In ABuildings class there would be a variable called ‘Price’ and it would indicate the amount of money the player must have in order to build the building. So each building type class would have a ‘Price’ variable and that could be set to a default value individually. When the player clicks a building on his HUD, the game would check if he has enough money to proceed, and if so, the building would be allowed to be placed.

So here is my question. When the player clicks let’s say ‘BarackBuilding’ the default value of the Price variable of that class needs to be checked in order to make the calculations of checking the player’s money. But that class is not existing anywhere in the level, it is only created in the Editor, so in my understanding there is no way to get a reference to it and read its stored values.

I am aware that the building could be spawned hidden and then only set it to visible when the transaction is actually happened but I was wondering if the method described above could work (probably not tho:P).

Thanks in advance!

Indeed. You need to create an instance of the class and then query for your value.
Alternatively, you could create a structure with a class refernce and the price and maintain the price default values there.
Each time you spawn a building, the bulding spawner queries that table and sets the price according to the building class to be spawned…

You can store the class of your BarackBuilding in the Widget and use “Get Class Defaults” to get the price and/or other values.
Just make sure u set the price in the class.

Thank you very much KVogler for clearing it up for me! I really like your alternative method, I will look into that for sure, spawning it hidden seems to be a ‘hacky’ way for me, the other one is much more elegant.

Thanks again!

Hi Urm92,

Thanks for your suggestion! What do you mean by storing it in the Widget, do you mean spawning the building actor class in the widget and storing the return value there or I misunderstood you?
I already have a system that spawns the building, I just have to select the desired type from an enum list in the widget, so I only had to solve the ‘get a value from it before spawning it’ problem.

You dont have to spawn it at all.
But it looks like you made it in a different way.

My thought was to create a widget with an array of the parent class and store every building you want as default in it.
On Widget construction you go with “For Each” and get default values of the classes.

At least thats the way i would try it.

Edit: Doing it with an enum sounds complicated.

Hi again,

Ah I see now what you meant, seems like a nice solution worth for consideration, thank you for that!
The enum version isn’t that complicated, I just made a ‘Converter’ function which has a Switch On Enum in it and that returns the appropriate class. Since I am not a programmer I can imagine that this might not be the most efficient and elegant solution and I’m making my own life harder, but I really liked the idea of just selecting the building I want to spawn from a list :stuck_out_tongue:

Thanks again both of you for your help!