Take a look at this screenshot and tell me whether I could do it in a less ugly way:
This is a function of a storehouse. The function should get a item from the storehouse. The items are saved inside of a struct. The enum which the function get’s is just all the names of the struct variables. So I can say to the function “Get 2 of Item Wood” and it removes the 2 wood from the correct struct variable and tells me yes, I’ve done that.
Now if I add new variables to the struct, I actually have to copy/paste all the stuff on the right from the switch again and change the break struct and add members of struct node to the new item. That’s really ugly.
As far as I know there is no better approach for this at the moment, so my suggestion would be: Make that every struct automatically creates an enum with all the variable names and add a node like struct.SetMember(Enum, Value). If it would work like this I would not need all this ugly stuff on the right, and if I add a new item I would not have to manually add new nodes which fit the new switch output.
I haven’t used Blueprints much in recent months, so am a bit rusty of what is possible and what isn’t, but another way of doing it would be to have a array of a struct. That struct just containing a Enum type (for what type of item it is) and a value for how many of those items are in the store. Ideally a hash map would be better than a array, but blueprints doesn’t support built-in hash maps as far as I know.
Then you would have a “reference to that struct” that you set to the required one depending of what type of item you wanted to fetch. One problem is that you can’t use a reference to structure in the array, so you would need to copy the values out of the array and then back in again.
So a quick example is [Note that I haven’t added the nodes to check there are enough items, but that would be simple to do]:
For new types of items, you would just set the Current Index variable to the correct array index. Or you could loop through the array until you found the struct with the same enum type as the required type.
MattW, this is awesome! It implemented it and it works perfectly. I don’t even have to switch on the enum since I can just convert the enum to an int and get the array on that number.
Thanks a lot!
Actually I think the Enum/Struct feature I suggested would still be a bit easier, since with this array it’s not very easy to set different default values for the different items. So the feature request stills stands
I completely agree that using Structs could be made easier, and your idea is one possible way.
What problems are you having in setting default values? If you want different blueprints to all have the same default values in their array, then you could create a base blueprint. Alternatively you could create a struct wrapper around the array. So a struct with just a single array of your other structs inside it. Then inside that wrapper you can add elements to the array and set their default values. Then just add that struct wrapper to your blueprints. Of course your blueprints will then be slightly bigger, because you will need to break the struct wrapper to be able to access the array.
Or you could create a new blueprint Actor component with the store array and the get items function in it, and then add that to your blueprints.
What you’re describing is just inherently not a struct, it is a map, which is something that will hopefully be available to blueprints in the future, but the approximation via enum and array indexing is a good short term solution for this kind of problem.