How do I detect numbers and add them to check whether sum is 20 ?

0 favourites
  • 5 posts
From the Asset Store
20 high-quality 2D monsters. This asset is perfect for a side scrolling game, or even a turn based RPG type game.
  • I want to detect numbers in between text,add those numbers and then store them in a global variable if the result of their addition is 20 or otherwise if their sum is not 20 then I want to store the original numbers only,

    And below are two examples with their explanations

    A)"ac776sd"= Sum of numbers=20(7+7+6),"Global Variable Sum 20"=776

    B)"bc321sd"= Sum of the numbers=6(3+2+1),"Global Variable Sum not 20"= 321,


  • You can run a loop and check each letter is NaN or not.

    If it is a number, add each of them to variable.

    At the end, check the variable is equal 20 or not.

  • Does the number appear exactly at index 2, 3 and 4? Then you can use the mid( ) expression to extract each of them separately and add them up. You can do this in one line.

    It will look something like: int(mid(string,2,1)) + int(mid(string,3,1)) + int(mid(string,4,1))

    That will give you the sum, and the rest is easy.

    If the numbers can appear randomly in the string, then you can do what Maverick said (above).

    1. Use a For loop that runs for the length of the string
    2. Have a local variable called sum
    3. You can use loopindex for the mid expression, e.g.: mid(string,loopindex,1)
    4. System -> is value type. If that character is an integer, add it to the variable sum.
    5. After the for loop ends, check the sum and store accordingly.
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • In order to complete my project I need to make my project display a text when ever a 4 digit number whose sum is 20 or more appears(Which I can adjust in a Variable) and I tried code which is similar to your code because of the other code in my project and I am trying to replace any four digits whose sum is 20 with any string and because of this I created this forum topic and I do not know how to correctly use Loops(And may be because of this reason I am unable to get my project to work),and Can you tell me where to look for so that I can solve my problem and with a few changes complete my project,And a link to my project file is below-


  • I've created a very simplistic project that checks the input string and shows you the SUM of all the numbers in it.

    Here you go:

    Let me know if this helps

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