I have a problem with arrays in blueprints.
Although it should seem straight forward using arrays, I somehow cant properly add an element.
It seems any reference to the array returns a mere copy, thus any element added is added to that (useless) copy, whereas the original array remains untouched.
First, I tried to get a reference to the array itself. - Doesnt work. A copy is referenced…
Then I made a “AddElement” function in the same BP where the array resides and grab the array directly from there. - Doesnt work. No data is added.
Also, if I have an array of arrays, or an array of a struct that contains an array as a member, then it seems impossible to access that embedded array either.
The for-each loop node will also offer the struct array member as a copy only.
A blueprint component, called “MotherbaseBPC”, has an array variable of that struct which is prepopulated with various units.
It has functions for returning the Staff limit and other values (which works. Reading from the array is no problem).
It also has a function for adding a staff member to a specific unit.
So far the setup.
Now I try to access/modify the array from another place. For testing purposes i chose the level blueprint.
As I try to modify the “Medical” team by adding a staff member, this is what happens on key presses:
X - a “100” is logged, correctly reading the staff limit for the medical team
C - a “0” is logged, correctly reading the initial staff size of the empty team.
M - an “added” is logged, showing that the code ran indeed.
C (again) - a “0” is logged, although the array is expected to have a new element added.
I love working with Blueprints for Game Data, but stopped doing any complicated control structures in them entirely, so much easier in C++. You can make Blueprint Callable functions, but then modify the array’s in C++. Just make a C++ base class for your GameState and add the array’s as Properties there. You’ll want your struct’s in C++ also.
When it comes to C++ im like a one legged guy in a butt kicking contest…
If I declare my structs and enums in a separate file (to keep things organized), how would i add that to my project?
All tutorials I see just extend from AActor… Floating cones, power-ups… all interactive stuff.
I would need a good tutorial for implementing basic classes / declarations that do not spawn or exist in the level.
In the editor just click to add a C++ class. Call it Structs or something. Then put your structs and enums in it. Other C++ files just #include the header and they can use enums or structs in it. The game will have it. I have a file full of static blueprint callable functions that I also have some of my enums in, for more global type enums. If only one class is using them I put them in the header with the class.
One thing. In C++, in structs everything is by default, it’s the only difference really from a class. People by convention often use them for data only, but they can do everything a class can and are not cheaper memory wise.
But, that shouldn’t cause your compile error. I don’t see your #includes where are they? you can’t put code before them, and you need the .generated.h file that the Editor should have made for you, that is how the Engine will “know” about your file.
I used:
-Right-click in the context browser
-Choose “New C++c class”
-Select “None” (or blank class) as parent.
The C++ files that the editor creates look then like this:
The .h file:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
/**
*
*/
class XOF_API DataTypes
{
:
DataTypes();
~DataTypes();
};
and the .cpp file:
// Fill out your copyright notice in the Description page of Project Settings.
#include "XOF.h"
#include "DataTypes.h"
DataTypes::DataTypes()
{
}
DataTypes::~DataTypes()
{
}
Thats all. There is no “generated” include and no includes in the .h file.
So the best parent would be UObject then.
Since its not something that exists in the level, (does not even define a single method), anything like AActor would be overkill.
Alas, UObject cant be selected in the “New C++ class” wizzard and I have no idea how to add new class files to the project manually (so that the build tool sees them as well).
Right. It just seems, comming from the Delphi world, a bit “sloppy”. Declaring a class although you never need it.
In Object Pascal, I would simply make a unit file that declares the structs, without declaring an empty, never used, dummy class.
Cant I declare stuff in just a header file (even dropping the .cpp entirely)?
Yeah, when I chose, for example, the ThirdPerson template project, it compiles.
It’s because that generated .h file links the file so it’s available.
I’m not positive, but you can probably delete out the class it creates, just keep the #includes
There is no real overhead for having a declared but not instantiated class in C++. I’m not really familiar with Delphi or Pascal. Maybe it is the sloppy way of linking in the generated.h but it is the way I know how to do it, just let the Editor make it.