» «

# Checking if a variable is the sum of any combination of variables

2 replies to this topic
matinat0r
• ##### matinat0r

Player Hater

• New Members
• Joined: 28 Jul 2014

### #1 Posted 30 July 2014 - 07:54 PM Edited by matinat0r, 30 July 2014 - 10:32 PM.

Hey there, ran into a bit of a problem.

I have 5 variables, 2@, 3@, 4@, 5@, and 9@.

I want to check if 9@ is equal to the sum of any combination of the first four variables.

Based on what I know in CLEO, the only way I can think to do this is to create variables for every possible value, and then check to see if 9@ is equal to any of those variables, a bit like this:

```0085: 10@ = 2@
005A: 10@ += 3@
0085: 11@ = 2@
005A: 11@ += 4@
0085: 12@ = 2@
005A: 12@ += 5@
0085: 13@ = 2@
005A: 13@ += 3@
005A: 13@ += 4@
...etc

if or
003B:   9@ == 2@
003B:   9@ == 3@
003B:   9@ == 4@
003B:   9@ == 5@
003B:   9@ == 10@
003B:   9@ == 11@
...etc```

But not only does that feel a bit messy, it also uses up a lot of vars that I might be needing in other areas of the script. I thought I could do the same thing with just one extra variable and some nested conditionals, so I came up with this:

```if 003B:  9@ == 2@
then jump @true
else
if 003B:  9@ == 3@
then jump @true
else
if 003B:  9@ == 4@
then jump @true
else
...etc
0085: 13@ = 2@
005A: 13@ += 3@
005A: 13@ += 4@
if 003B:  9@ == 13@
then jump @true
else
...etc
else jump @false
...
end```

Which is a bit better, as I only use up one more variable, but it's even longer and probably harder to look at than before. What's more, if I come across a similar situation in the future with even more variables to combine, the code is going to get exponentially longer.

So, I was wondering if there was any easier, cleaner way to do this. Maybe I'm missing something, or there's just a gap in my knowledge, or maybe I'm approaching this completely wrong. Either way, I'd really appreciate it if anyone could lend a hand.

Thanks.

Shmoopy
• ##### Shmoopy

Lost interest in GTA

• Members
• Joined: 16 Jun 2009

### #2 Posted 31 July 2014 - 01:38 PM Edited by Manfred Von Karma, 31 July 2014 - 01:39 PM.

If the combinations are just of two variables then here is a code :

```//2@ 3@ 4@ 5@ - 9@
\$Index = 1
0A8E: \$Sum(\$Index,7i) = 2@ + 3@ // 2@ + 3@ equals \$Sum[1]
\$Index += 1
0A8E: \$Sum(\$Index,7i) = 2@ + 4@ // 2@ + 4@ equals \$Sum[2]
\$Index += 1
0A8E: \$Sum(\$Index,7i) = 2@ + 5@ // 2@ + 5@ equals \$Sum[3]
\$Index += 1
0A8E: \$Sum(\$Index,7i) = 3@ + 4@ // 3@ + 4@ equals \$Sum[4]
\$Index += 1
0A8E: \$Sum(\$Index,7i) = 3@ + 5@ // 3@ + 5@ equals \$Sum[5]
\$Index += 1
0A8E: \$Sum(\$Index,7i) = 4@ + 5@ // 4@ + 5@ equals \$Sum[6]
// example :
if
07D6:   9@ == \$Sum[1] // 9@ equals 2@ + 3@
then

end
if
07D6:   9@ == \$Sum[2] // 9@ equals 2@ + 4@
then

end
// ...
```

If not then it can't be done without a long code .

DK22Pac
• ##### DK22Pac

• Feroci
• Joined: 12 Apr 2009
• Best WIP Mod 2014 [Grand Theft Auto 3D Contribution]
Contribution Award [Mods]

### #3 Posted 31 July 2014 - 10:25 PM

```for 31@ = 0 to NUM_OF_VARIATIONS
if
07D6: 9@ == \$Sum[31@]
then
// true
// ...
break
end
end```
• Shmoopy likes this

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users