Best way to parse and compare a string in Construct?

0 favourites
From the Asset Store
Best car suspension with spring effect and very cool terrain generation.
  • I have 3 different string vars like:

    appleVar = "apple3"

    pearVar = "pear1"

    mangoVar = "mango4"

    what is the best way to figure out which one has the highest number?

    I thought about setting a new var from this:max(int(right(appleVar,1)),int(right(pearVar,1)),int(right(mangoVar,1)))

    but then I only get the number and not the name. anyone have an idea how to get the name and the number?

    EDIT: also I don't care about ties (apple2, pear2, mango2), in that case I'd take a random one.

  • With variables it's quite difficult. If there are only 2-4 of them, you can get away with an expression like (int(appleVar)>int(pearVar) & int(appleVar)>int(mangoVar)) ? appleVar : (int(pearVar)>int(appleVar) & int(pearVar)>int(mangoVar)) ? pearVar : .....

    But if there are more values, you should probably use an array or dictionary. Loop through all values, extract the integer part and compare it with the previous biggest number.

  • dop2000 there's only 3 variables. and only 1-4 possible numbers.

    However I don't recognize that expression, I assume its Regex or something? Would it return the whole string?

    Is this a System Set variable Action?

    Ultimately I need the string of the highest one.

    EDIT: I was able to do it with a bunch of find() calls, although I don't love it.

  • It's a conditional expression if?then:else.

    You can use operators like & too, so ifa&ifb?then:else.

  • Does the number have to be at the right? If you put it at the left you can do this. Pretty sure the text gets converted to a number to compare automatically.

    Best = (apple>pear)?apple:pear

    Best = (best>mango)?best:mango

  • Yeah, it's a condensed if-then-else statement.

    (A ? B : C)

    If A is true, then the result will be B, otherwise it will be C.

    And you can use nested statements: (A ? B : (K ? L : M))

    The full expression in your case may look like this:

    (int(appleVar)>int(pearVar) & int(appleVar)>int(mangoVar)) ? appleVar : 
    (int(pearVar)>int(appleVar) & int(pearVar)>int(mangoVar)) ? pearVar : mangoVar

    It may not work correctly if two numbers are the same.

    EDIT: Just realized that int("abc123") does not extract the number, you'll have to use right()

  • dop2000 okay thanks

    oosyrag thanks

    R0J0hound yes the number has to be on the right because it is used elsewhere and it's expecting it in that format. Although technically I could change it, but in some places I display the value "apple1" to the player and it might be a pain to always swap it when I want to display it.

  • I have costs associated with apple1,2,3,4. So apple1 costs 50 coins, apple2 costs 100 etc..

    so I end up using max(right()) to get the highest fruit, then I use find() to match the number back to the string. then I have my 'best' string, then I use left() to figure out which fruit it is AND right() to get the cost THEN FINALLY subtract that from your bank.

    it's complicated! It;s for the game's AI to buy the best fruit. It's working but I know its going to be hard to understand when I look at this again in 6 months! writing the best comments I can!

  • I would suggest using a separator, for example "mango_5". This way you can have prices with several digits. Use int(tokenat(var, 1, "_")) to extract the price.

  • dop2000 oh I like that! but I don't think it helps more than right/left in my case.

    I wish there was a way return a CONSTANT's variable name and then use it on-the-fly...

  • thanks all!

  • The solution sounds complicated.

    This would set best to the var with the highest digit.

    best = right(apple,1)>right(pear,1)?apple:pear

    best = right(best,1)>right(mango,1)?best:mango

    Then we can split the name and number.

    name = left(best, len(best)-1)

    num = int(right(best,1))

    And you can lookup the prices with:

    If name=“apple”

    — price = tolenat("50,100,150,200", num-1, ",")

    And similarly for pear and mango.

    Anyways, cheers

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • R0J0hound that does look easier! I have to get better at reading Regex and token stuff, for me its hard to glance at and see what its doing. I come from a strict C programming language background so maybe that's why it looks so strange to me?

  • No regex was used here. In c you’d have more options of ways to do it. You could do it the same, just the string functions would be named differently. C also has that conditional operator ?:

    Probably in c you’d use an array instead of the token at.

    This would be basically the same in c. I didn’t do the part to extract the name.

    char right(char* s)
     int i=0;
     while(s[i]) i++;
     return s[i-1];
    char* getBest(char* a, char* b, char* c)
     char* best = right(a)>right(b)?a:b;
     best = right(best)>right(c)?best:c;
     return best;
    int main()
    char* apple="apple3", pear="pear4", mango="mango1";
    char* best = getBest(apple, pear, mango);
    int prices[] = {50, 100, 200, 800};
    int cost = prices[right(best)-'0'];
  • R0J0hound ummm...I've been programming since 1995 and never knew C used '?' and ':' operators for conditional expressions! I don't know how I missed that! Is it just not widely used? I've never seen anyone use it before, and I've looked at a lot of code over the years! It must be like "goto" or something where maybe people frown upon using it?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)