Support of atoi (convert string -> int)

Heya,

The case is very classic for stackoverflow questions, but, I want convert string type Custom String(“123, 123, 123”) to an integers Array(123, 123, 123).
Do we support raw conversion with some built-in method, or i should (try) write my own one?

Assume, that i know how to split string, I just need convert “123” to 123.

Have a nice time of day!

Unfortunately there is no such built-in method to convert a string numeric into it’s raw numeric integer type, it’s only one-way from int->string. Though i am not sure if high level parsers like OverPy or OSTW have a wrapper to convert a numeric string natively in their written language and than pass it into the desired type on overwatch workshop script language side. But i would bet they don’t.

For an approach to do an atoi on your own you can try this, but be aware when you scale your project, my example is a waitless loop, and depending on how many times you cast literally from numeric strings to integers the server might crash, you can test it by starting the custom game with the following script and as you have spawned press the interact key, idk your level of workshop knowledge but i assume u know how to proceed:

Edit: I made some optimazations and removed some redundancies, it’s now more compressed, more readable and is generalized it works now for any numeric input like, negatives and floating point numbers -with two kind of seperators-.

Here are the snippets:

Workshop script syntax
tvariables
{
    global:
        0: Chars
        1: Numerics
        2: FloatingPointSeperator
        3: returnValue_Atox
        4: ret
        5: sign
        6: it
        7: ot
        8: iaf
        9: s
        10: ret_0
        11: sign_0
        12: it_0
        13: ot_0
        14: iaf_0
        15: s_0
        16: ret_1
        17: sign_1
        18: it_1
}

rule("Initial Global")
{

    event
    {
        Ongoing - Global;
    }

    // Action count: 2
    actions
    {
        Set Global Variable(Chars, Array(Custom String("0"), Custom String("1"), Custom String("2"), Custom String("3"), Custom String("4"), Custom String("5"), Custom String("6"), Custom String("7"), Custom String("8"), Custom String("9")));
        Set Global Variable(Numerics, Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9));
    }
}

rule("ATOX")
{

    event
    {
        Ongoing - Global;
    }

    conditions
    {
        Is Button Held(Host Player, Button(Interact)) == True;
    }

    // Action count: 62
    actions
    {
        If(String Contains(Custom String("12,12"), Custom String(".")));
            Set Global Variable(FloatingPointSeperator, Custom String("."));
            Set Global Variable(ret, 0);
            Set Global Variable(sign, 1);
            Set Global Variable(it, 0);
            Set Global Variable(ot, 0);
            Set Global Variable(iaf, 0);
            Set Global Variable(s, String Split(Custom String("12,12"), Global Variable(FloatingPointSeperator)));
            If(Compare(Char In String(First Of(Global Variable(s)), 0), ==, Custom String("-")));
                Set Global Variable(sign, -1);
                Modify Global Variable(it, Add, 1);
            End;
            While(Compare(Char In String(First Of(Global Variable(s)), Global Variable(it)), !=, Custom String("")));
                Set Global Variable At Index(iaf, 0, Add(Multiply(First Of(Global Variable(iaf)), 10), Value In Array(Global Variable(Numerics), Index Of Array Value(Global Variable(Chars), Char In String(First Of(Global Variable(s)), Global Variable(it))))));
                While(Compare(Char In String(Value In Array(Global Variable(s), 1), Global Variable(ot)), !=, Custom String("")));
                    Set Global Variable At Index(iaf, 1, Add(Multiply(Value In Array(Global Variable(iaf), 1), 10), Value In Array(Global Variable(Numerics), Index Of Array Value(Global Variable(Chars), Char In String(Value In Array(Global Variable(s), 1), Global Variable(ot))))));
                    Modify Global Variable(ot, Add, 1);
                End;
                Modify Global Variable(it, Add, 1);
            End;
            Set Global Variable(ret, Add(First Of(Global Variable(iaf)), Divide(Value In Array(Global Variable(iaf), 1), Raise To Power(10, String Length(Value In Array(Global Variable(s), 1))))));
            Set Global Variable(returnValue_Atox, Multiply(Global Variable(sign), Global Variable(ret)));
            Skip(38);
            Else If(String Contains(Custom String("12,12"), Custom String(",")));
            Set Global Variable(FloatingPointSeperator, Custom String(","));
            Set Global Variable(ret_0, 0);
            Set Global Variable(sign_0, 1);
            Set Global Variable(it_0, 0);
            Set Global Variable(ot_0, 0);
            Set Global Variable(iaf_0, 0);
            Set Global Variable(s_0, String Split(Custom String("12,12"), Global Variable(FloatingPointSeperator)));
            If(Compare(Char In String(First Of(Global Variable(s_0)), 0), ==, Custom String("-")));
                Set Global Variable(sign_0, -1);
                Modify Global Variable(it_0, Add, 1);
            End;
            While(Compare(Char In String(First Of(Global Variable(s_0)), Global Variable(it_0)), !=, Custom String("")));
                Set Global Variable At Index(iaf_0, 0, Add(Multiply(First Of(Global Variable(iaf_0)), 10), Value In Array(Global Variable(Numerics), Index Of Array Value(Global Variable(Chars), Char In String(First Of(Global Variable(s_0)), Global Variable(it_0))))));
                While(Compare(Char In String(Value In Array(Global Variable(s_0), 1), Global Variable(ot_0)), !=, Custom String("")));
                    Set Global Variable At Index(iaf_0, 1, Add(Multiply(Value In Array(Global Variable(iaf_0), 1), 10), Value In Array(Global Variable(Numerics), Index Of Array Value(Global Variable(Chars), Char In String(Value In Array(Global Variable(s_0), 1), Global Variable(ot_0))))));
                    Modify Global Variable(ot_0, Add, 1);
                End;
                Modify Global Variable(it_0, Add, 1);
            End;
            Set Global Variable(ret_0, Add(First Of(Global Variable(iaf_0)), Divide(Value In Array(Global Variable(iaf_0), 1), Raise To Power(10, String Length(Value In Array(Global Variable(s_0), 1))))));
            Set Global Variable(returnValue_Atox, Multiply(Global Variable(sign_0), Global Variable(ret_0)));
            Skip(15);
        Else;
            Set Global Variable(ret_1, 0);
            Set Global Variable(sign_1, 1);
            Set Global Variable(it_1, 0);
            If(Compare(Char In String(Custom String("12,12"), 0), ==, Custom String("-")));
                Set Global Variable(sign_1, -1);
                Modify Global Variable(it_1, Add, 1);
            End;
            While(Compare(Char In String(Custom String("12,12"), Global Variable(it_1)), !=, Custom String("")));
                Set Global Variable(ret_1, Add(Multiply(Global Variable(ret_1), 10), Value In Array(Global Variable(Numerics), Index Of Array Value(Global Variable(Chars), Char In String(Custom String("12,12"), Global Variable(it_1))))));
                Modify Global Variable(it_1, Add, 1);
            End;
            Set Global Variable(returnValue_Atox, Multiply(Global Variable(sign_1), Global Variable(ret_1)));
            Skip(1);
        End;
        Big Message(Host Player, Custom String("{0}", Add(Global Variable(returnValue_Atox), 1)));
    }
}

OSTW or del syntax
globalvar String[] Chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
globalvar Number[] Numerics = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
globalvar define FloatingPointSeperator;
public static Number Atox(in String number)
{
    if (number.Contains("."))
    {
        FloatingPointSeperator = ".";
        return Atof(number);
    }
    else if (number.Contains(","))
    {
        FloatingPointSeperator = ",";
        return Atof(number);
    }
    else
    {
        return Atoi(number);
    }
}
public static Number Atoi(in String number)
{
    Number ret;
    Number sign = 1;
    Number it;
    if (number.CharAt(0) == "-")
    {
        sign = -1;
        it++;
    }
    for (; number.CharAt(it) != ""; it++)
    {
        ret = ret * 10 + Numerics[IndexOfArrayValue(Chars, number.CharAt(it))];
    }
    return sign * ret;
}
public static Number Atof(in String number)
{
    Number ret;
    Number sign = 1;
    Number it;
    Number ot;
    Number[] iaf;
    String[] s = number.Split(FloatingPointSeperator);
    if (s[0].CharAt(0) == "-")
    {
        sign = -1;
        it++;
    }
    for (; s[0].CharAt(it) != ""; it++)
    {
        iaf[0] = iaf[0] * 10 + Numerics[IndexOfArrayValue(Chars, s[0].CharAt(it))];
        for (; s[1].CharAt(ot) != ""; ot++)
        {
            iaf[1] = iaf[1] * 10 + Numerics[IndexOfArrayValue(Chars, s[1].CharAt(ot))];
        }
    }
    ret = iaf[0] + (iaf[1] / 10 ^ s[1].Length);
    return sign * ret;
}

rule: 'ATOX'
Event.OngoingGlobal
if (IsButtonHeld(HostPlayer(), Button.Interact))
{
    BigMessage(HostPlayer(), "{0}".Format([Atox("12,12") + 1]));
}
1 Like