C++ Array?

Hi,

How can i make a node for my blueprint that outputs the array index from what my x is
Example, i have an array inside c++ code then for my blueprint i can specific what index i want from that array. Then it simply just outputs what on the array indiex

Isn’t this what a regular "get " does?

1 Like

Regular GET operates on arrays created and handled by Blueprints.
If you have array in C++ you can create new class derived from UBlueprintFunctionLibrary and there write getter function for an array. For example something like this:
MyBlueprintFunctionLibrary.h:

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "MyBlueprintFunctionLibrary.generated.h"

extern int MyArray[];

UCLASS()
class TESTPROJECT_API UMyBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
public:
	UFUNCTION(BlueprintCallable)
	static int GetElemFromMyArray(int index);
};

and

MyBlueprintFunctionLibrary.cpp:

#include "MyBlueprintFunctionLibrary.h"

int MyArray[] = { 1, 2, 3, 4 };

int UMyBlueprintFunctionLibrary::GetElemFromMyArray(int index)
{
	return MyArray[index];
}

Next in Blueprint you call GetElemFromMyArray function passing to it index you are interested in.

1 Like

As long as the array is a UPROPERTY, and accessible from blueprints (BlueprintReadOnly/BlueprintReadWrite), the regular get operator can be used.

Alternatively, one could make either a function in a library as @mzaleczny suggested, or a public UFUNCTION(BlueprintCallable) in the owning class itself for retrieving an element given an index from the desired array.

What if my array looks like this

Lets say i wanna get index 5, how can i return an array based on the array index

What would index 5 mean in this case?
The 5th array(16 elements), or the 5th index of every array(256 elements)?

1 Like

The 5th array that contains 16 elements :slight_smile:

Try something like this:

void UMyBlueprintFunctionLibrary::GetElemFromMyArray(int index, TArray<int>& array)
{
	array.Empty();
	for (int k = 0; k < 16; k++) {
		array.Add(MyArray[index][k]);
	}
}

This function in variable array returns specified subarray in Blueprint. Next on it you use regular GET.

2 Likes

God bless you people, i wish i had your knowledge on c++´
Once again thanks <3

Would it be possible to only get int that isn’t -1.
I removed it all -1 from my array, how can i loop k to the amount of element of the index? :slight_smile:

Hi, I am not quite sure if this is what you mean, but you can loop through the entire array on the index with:

	for (int k = 0; k < MyArray[index].Num(); k++) {
		array.Add(MyArray[index][k]);
	}
1 Like