Get a number from Display Name

So lets say Im spawning boxes, and so their display names are as I spawn them: “Box”, “Box_1” “Box_2” … “Box_999”

Is there any way to extract an integer that would be the number of the box?

In other words:
Can you convert a string “Box_9” to an integer “9”?

There doesn’t appear to be a built-in way to do this via Blueprint, but it is definitely doable using C++. Check out this page on the UE4 Wiki and also the FString documentation page from the official docs to learn more.

Bottom line, you should be able to write up some simple wrapper functions using the above references, that you can then expose to Blueprint for you to use anywhere in your project.

Good luck!

Follow-up: you may also want to read this article, which explains the troubling limitations of the (FString to Int) Atoi function. In short, you can’t always trust the output of that function, and, depending on your usage, you might be better off not attempting to convert strings to integers at all.

Just set an Int variable called Index in the box BP and set it once spawned. Then just get the index.

FName::GetNumber() might be what you’re looking for.

If you know the position of the number, you can use the Get Substring node, and convert the output to an integer.

Example:

Hope this helps!

You can use two methods, a split or a parse. A split works fine when you know for a fact it never changes from “ClassName_#”. I recommend a parse, as it will work on everything from “ClassName_#” to “ClassName_Child_Instance-test_#”. You get 0 for “ClassName”. Works without fail. Enjoy.

Hi sadly I dont know how many digits the number would be. It can be 9, 99, 999… so I wouldnt know the lenghts or start index.

Still thanks.

You could always use 009, 099, and 999 instead of just 9, 99, and 999.

I believe @Dank22x doesn’t want to set anything manually, otherwise there would be better ways to reach the goal than converting the display name into int.

But if the name always starts with Box_, the “Right Chop” node with Count 4 can be used, no matter how many digits follow it.

That is true, will try. Thanks!