I’m just trying to pass an array defined on the function line to the constructor for my struct. I’m generating the code based on XML, so it would just make it simpler to generate. I was just wondering if C++ has syntax similar to other languages for doing this. I found some information online that doesn’t work, guess the proposal never went through for the updated standards.
Here’s what I have:
DialogueNodes(FText dialogueFragment, TArray<FName> nextNode)
: DialogueFragment(dialogueFragment)
{
NextNode.Append(nextNode, sizeof(nextNode)); // Doesn't work for getting sizeof(array) across scope
}
DialogueNodes(
LOCTEXT("Dialogue26","You about ready to go?"), [TEXT("0x01000003000001D7"), TEXT("0x01000003000001DF")]))
I tried using FName nextnode] and TArray<FName> nextNode, without any success. I also tried surrounding the array memberes with ] and {}. Is there a macro, or something specific to unreal I can use? It looks like std::array<string, count> nextNode would have worked with brackets around members, but I’m pretty sure it’s a bad idea to use stl with Unreal, and I don’t know the size of the array before hand.
I’m confused, what exactly are you trying to accomplish here?
You claim that there is a problem passing TArray as a function parameter, however that works perfectly fine.
It seems that your problem stems from the sizeof(nextNode), which tells me that you’re trying to append raw data from one array to another. Try using the alternative:
NextNode.Append(nextNode);
As long as the Arrays are of the same type (TArray<FName>::Append(TArray<FName>)), it should just work.
I’m trying to not have an explicit TArray declared before calling the DialogueNodes constructor. Like in Ruby where [1, 2, 3, 4, 5] would get picked up by splat character in a method argument, and interpreted as an array, or this C++ examplefrom stl with {} around the parameters. I believe that works for all of stl, either since C++11 or 14. Was just wondering if there is an equivalent.
This works with vectors in C++14 (Also works on C++11), at least on Clang.
void test(std::vector<int> test)
{
for (const auto t : test)
{
std::cout << t << std::endl;
}
}
int main()
{
test({1,2,3,4});
}
I checked the docs and there is no constructor to help with that case. You could create a variadic function that builds the TArray from the parameters and returns it. Something like:
Alright, I’ll look into Variadic functions. If I wanted to hack {} initialization in, would that be possible in theory, or is that something that can only be done through the compiler?
I’m having a bit of a hard time getting variadic functions to work correctly. All of the examples I’ve found use some stl for this, std::initializer_list.
I believe it just causes infinite recursion when run, since the editor crashes when opening the project. I tried playing around with having a template on the base case, but was just getting compiler errors. Have any suggestions on how to implement this correctly?
Those all use things from std. Is it safe to use any std with Unreal? As long as it builds, I’m fine with it as will be using 4.13 by releasing time. Substance is already up to 4.13, we’re just waiting on NVidia.