Author Topic: Help! Casting string array elements to integers  (Read 3320 times)

0 Members and 1 Guest are viewing this topic.

Offline BulldogTopic starter

  • Jr. Member
  • **
  • Posts: 10
  • Activity:
    0%
  • Reputation Power: 1
  • Bulldog has no influence.
  • Referrals: 0
    • View Profile
Help! Casting string array elements to integers
« on: June 06, 2022, 02:17:35 AM »
Hello everyone I hope someone can help with this ..

I have a function that is returning durability on items as elements of an array these come out as arrayelement
  • /arrayelement[y] which is the durability say 234 / 255. I want to cast the strings into an integer but I am not sure what the Orion version of Integer.parseInt ?


eg:

 int numberx = Integer.parseInt(arrayelement
  • );

 int numbery = Integer.parseInt(arrayelement[y]);

Offline altiric

  • Jr. Member
  • **
  • Posts: 81
  • Activity:
    0%
  • Reputation Power: 2
  • altiric has no influence.
  • Referrals: 1
    • View Profile
Re: Help! Casting string array elements to integers
« Reply #1 on: June 06, 2022, 04:39:36 AM »
Orion gives you: pairIntInt.Index, pairIntInt.Value, pairIntString.Index, and pairIntString.Text to work with.

Offline Ali

  • Jr. Member
  • **
  • Posts: 14
  • Activity:
    0%
  • Reputation Power: 1
  • Ali has no influence.
  • Referrals: 0
    • View Profile
Re: Help! Casting string array elements to integers
« Reply #2 on: October 31, 2022, 05:49:03 PM »
Necroing a thread a bit, but figure I'd throw in a solution for this for anyone looking. In JavaScript there's a few options for changing a string to an integer. Number('1') and +'1' both work pretty much identically.

As for actually grabbing the durability values, RegEx is the way to go.

Code: [Select]
function DurabilityTest()
    {
    Orion.WaitForAddObject('myTarget');
    Orion.Print(GetDurability('myTarget'));
    }

function GetDurability(objSerial)
    {
    var obj = Orion.FindObject(objSerial);
    var durabilityArr = obj.Properties().match(/Durability (\d+) \/ (\d+)/) //evaluates to e.g., ['Durability 250 / 255', '250', '255'] OR null if match isn't found
    if(!durabilityArr) //if a match isn't found returns false
        return false;
    var minDurability = +durabilityArr[1]; //convert string to int
    var maxDurability = Number(durabilityArr[2]); //convert string to int
    return [minDurability, maxDurability]; //e.g. [250, 255]
    }

Tags: