Is there any way to change variable type from "string" to "int"?

Hi,

I read this article about type casting:

I would like to change variable (its value is number) type from “string” to “int” on my Verse code.
However, I cannot find the way yet.

var number : string = 1
var Index : int = [Int]number # Error

Thank you,

Let me know

You can’t do that because string and int does not share any superclass in common. However, I made this code a while back. Try to use it to convert from string to int

StringToInt(IntInString: []char): int=
    # if 'IntInString' is negative
    if (IntInString[0] = '-'):
        var Value: int = 0
        loop:
            set Value -= 1
            if (ToString(Value) = IntInString):
                Print("{Value}")
                return Value
            
    # if 'IntInString' is positive
    else:
        var Value: int = -1
        loop:
            set Value += 1
            if (ToString(Value) = IntInString):
                Print("{Value}")
                return Value 
3 Likes

Thank you for your reply.

It seems it will work well.
However, when I try to use your method in my code, some Error occur as the attached pictures.

Could you give me advice how to use it?

Thank you.


using { /Fortnite.com/Devices }
using { /Verse.org/Simulation }
using { /UnrealEngine.com/Temporary/Diagnostics }

# See https://dev.epicgames.com/documentation/en-us/uefn/create-your-own-device-in-verse for how to create a verse device.

# A Verse-authored creative device that can be placed in a level
song2 := class(creative_device):
    @editable
    AudioPlayerDevice<public> : []audio_player_device = array{}

    # Runs when the device is started in a running game
    OnBegin<override>()<suspends>:void=
        # TODO: Replace this with your code
        var number : string = "1"
        var Index : int = StringToInt(number)

        StringToInt(IntInString: []char): int=
            # if 'IntInString' is negative
            if (IntInString[0] = '-'):
                var Value: int = 0
                loop:
                    set Value -= 1
                    if (ToString(Value) = IntInString):
                        Print("{Value}")
                        return Value
                    
            # if 'IntInString' is positive
            else:
                var Value: int = -1
                loop:
                    set Value += 1
                    if (ToString(Value) = IntInString):
                        Print("{Value}")
                        return Value 


The function definition is nested inside OnBegin scope instead of the class scope. To fix it, select it and click shift + tab

Ops!

It 's my easy mistake. I confirmed that it works well on my Project.
Thank you very much!!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.