x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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"?

Product Version: UE 4.21
Tags:
more ▼

asked Jan 02 '19 at 06:06 PM in Blueprint Scripting

avatar image

Dank22x
15 1 2 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

5 answers: sort voted first

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.

Solution

capture4.png (70.1 kB)
more ▼

answered Jan 03 '19 at 07:20 PM

avatar image

MajinSephiroth
1.2k 31 8 32

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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!

more ▼

answered Jan 02 '19 at 06:40 PM

avatar image

the_batch
1.2k 4 5 8

avatar image the_batch Jan 02 '19 at 06:53 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

index.png (90.8 kB)
more ▼

answered Jan 03 '19 at 06:21 PM

avatar image

Tuerer
1.8k 12 7 8

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

more ▼

answered Jan 03 '19 at 05:51 PM

avatar image

Artemis_
45 2 3 6

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

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

Example: Get Substring

Hope this helps!

capture.png (174.9 kB)
more ▼

answered Jan 03 '19 at 06:10 PM

avatar image

cheesyball232
50 3 1 5

avatar image Dank22x Jan 03 '19 at 06:33 PM

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.

avatar image cheesyball232 Jan 04 '19 at 07:55 AM

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

avatar image Tuerer Jan 04 '19 at 08:16 AM

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.

avatar image Dank22x Jan 04 '19 at 01:55 PM

That is true, will try. Thanks!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question