how to realize large if-else jumble in blueprint?

Change the types from float to UInstancedStaticMeshComponent*

Don’t forget to include the header:
include “Components/InstancedStaticMeshComponent.h”

Please mark the answer as solution if it works!

Looks like this is getting sorted…
There are many ways to skin the cat with this stuff, so a lot of it comes down to personal preference.

As I’ve outlined above, my preference would be to use nested C++ switches within a custom Blueprint function node.
The solution has to not only be for you now, but also for in the future in a couple of years time when either you or someone else has to get back into this code and work out what the heck it all does.

Back in the day before self-documenting code, all code had to be well documented for the next guy who had to modify it or usually port it.
Nowadays, most languages are self documenting… big feature.

So, the C++ switch method is simple (just a dozen lines or so of code).
And it’s completely self documenting, in structure and variables… it’s blindingly obvious what it’s actually doing.

For commercial programers documentation (or self documenting) is essential… some poor blighter will need to modify it or port it a couple of years later.
I feel the C++ switch in a Blueprint Function node does this, “spaghetti” (no offense meant) blueprint networks in this case are trickier.
Anyway, that’s my 10 cents worth… hope it helps.

3 Likes

Based on your input, I changed the code as below for my purpose but there are 2 problems
1- Unreal registered the code at first when I copied yours (except the API name) and showed DoStuff in Blueprint but now it doesn’t register the new version below although there are no issues in VS and still shows the DoStuff function although it doesn’t exist anymore. How should I refresh? I tried restarting the project but it didn’t work
Btw, I didn’t do anything on VS side other than saving the last version of the code.

Don’t forget to include the header:
include “Components/InstancedStaticMeshComponent.h”

2- When I add include “Components/InstancedStaticMeshComponent.h” based on your recommendation, VS finds an issue in the line GENERATED_BODY(), so that’s why I had to remove that but I don’t know if this version works since I can’t see this in Blueprint as I told above

#pragma once

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

/**
 * 
 */
UCLASS()
class TEMP2_API UMyLittleHelpers : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

		UFUNCTION(BlueprintCallable, Category = "Helpers")
		static void PickISM(float D1, float D2, UInstancedStaticMeshComponent* a0, UInstancedStaticMeshComponent* a1, UInstancedStaticMeshComponent* a2, UInstancedStaticMeshComponent* a3, UInstancedStaticMeshComponent* z)
		{
			int mesh_Depth00[13] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };
			int mesh_Depth10[13] = { 0, 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 };
			int mesh_Depth20[13] = { 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
			int mesh_Depth30[13] = { 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };

			float eccentricities[13] = { 7.27, 9.52, 11.77, 14.02, 16.27, 18.52, 20.77, 23.02, 25.27, 27.52, 29.77, 32.02, 34.27 };

			int i, mesh_no;

			for (i = 0;i++;i < 13)  if (D2 > eccentricities[i]) break;
			
			if (D1 <= 10) mesh_no = mesh_Depth00[i];
			else if (D1 <= 20) mesh_no = mesh_Depth10[i];
			else if (D1 <= 30) mesh_no = mesh_Depth20[i];
			else mesh_no = mesh_Depth30[i];

			if (mesh_no == 0) z = a0;
			else if (mesh_no == 1) z = a1;
			else if (mesh_no == 2) z = a2;
			else z = a3;
			
		}
	
};

Are you compiling from the Editor or from Visual Studio? Sometimes live coding doesn’t work so I’m not a huge fan of it.

It’s failing to compile because you inverted the order of the for loop:

The i++ should be the last term.

Also, the Z should be UInstancedStaticMeshComponent*& (Reference to a pointer, as it was on my code) or the return type of the function, for example:

static UInstancedStaticMeshComponent* Z PickISM(…)

in this case you have to return the value instead of using Z = …


Your function:

image


Z passed as reference:
image

1 Like

ah yes stupid mistakes. Also made #include “MyLittleHelpers.generated.h” last include and i think it solved the other issue. the latest version is below.

This time I closed unreal, rebuilt solution on VS (Rebuild All: 1 succeeded, 0 failed, 1 skipped), opened Unreal but it still doesn’t show PickISM but shows the long gone DoStuff.
What should I to make Unreal see the changes?
Btw, the code block below doesnt show function name in red as in yours, any idea why? maybe thats the problem

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "Components/InstancedStaticMeshComponent.h"
#include "MyLittleHelpers.generated.h"
 
UCLASS()
class TEMP2_API UMyLittleHelpers : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

	UFUNCTION(BlueprintCallable, Category = "Helpers")
	static void PickISM(float D1, float D2, UInstancedStaticMeshComponent* a0, UInstancedStaticMeshComponent* a1, UInstancedStaticMeshComponent* a2, UInstancedStaticMeshComponent* a3, UInstancedStaticMeshComponent*& z)
	{
		{
			int mesh_Depth00[13] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 };
			int mesh_Depth10[13] = { 0, 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3 };
			int mesh_Depth20[13] = { 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3 };
			int mesh_Depth30[13] = { 1, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 };

			float eccentricities[13] = { 7.27, 9.52, 11.77, 14.02, 16.27, 18.52, 20.77, 23.02, 25.27, 27.52, 29.77, 32.02, 34.27 };

			int i, mesh_no;

			for (i = 0;i < 13;i++)  if (D2 > eccentricities[i]) break;
			
			if (D1 <= 10) mesh_no = mesh_Depth00[i];
			else if (D1 <= 20) mesh_no = mesh_Depth10[i];
			else if (D1 <= 30) mesh_no = mesh_Depth20[i];
			else mesh_no = mesh_Depth30[i];

			if (mesh_no == 0) z = a0;
			else if (mesh_no == 1) z = a1;
			else if (mesh_no == 2) z = a2;
			else z = a3;	
		}
	}
};

Try to remove the “TEMP2_API”

MyLittleHelpers.h (1.3 KB)

1 Like

I think building in Development Editor mode did the trick. Thanks a million!

1 Like

Yeah, always build for development editor if you need to see changes in the editor!
The other build profiles are used to build the game directly, or to build a dedicated server.

I’m glad you solved your issue, have a nice day!

2 Likes