Using IF statement to change property value

This forum is the place to learn about and discuss ADRIFT 5. Feel free to mention any bugs you find here, but please also add these to the Bugs & Enhancements list.

Please also refer to the ADRIFT 5 Wiki for more information.
devileye
Posts: 11
Joined: Mon Mar 01, 2021 4:12 pm
Points: 10
Location: Bristol, UK
Contact:

Using IF statement to change property value

Post by devileye »

I have added the Integer property 'Arousal' to my characters. It's initial value is 0 but I want it to increase as certain Tasks are carried out. With one such task, I want it's value for one particular character to increase by 1 each time the task is performed upto a maximum value of 3. I have therefore added a Set Property Action to the Task which sets the Arousal property for that character to the following:

IF(%character%.Arousal < 3, %character%.Arousal = %character%.Arousal + 1, %character%.Arousal)

However when I run the task I get the following error message:

Bad Expression: IF(%character1%.Arousal < 3, %character1%.Arousal = %character1%.Arousal + 1, %character1%.Arousal)

And the value of the character's Arousal property remains at 0. I have tried substituting %character1% for %character% in the Set Property action but it makes no difference. I have even tried using an Integer Variable instead of a Property, but this generates a different error message:

Bad Expression: IF(0 < 3, 0 = 0 + 1, 0)

What am I doing wrong, or is this something Adrift just can't handle?
Thanks!
User avatar
Lazzah
Moderator
Posts: 2468
Joined: Thu Mar 31, 2011 5:54 am
Points: 90
Location: Clacton-on-Sea, Essex

Re: Using IF statement to change property value

Post by Lazzah »

Hello devileye, welcome to the forum.

In my opinion you are over-complicating this, although no doubt one of the ADRIFT boffins will prove me wrong.

Why not simply use a variable called Arousal? When the player performs the task you simply have an action in the task INCREASE VARIABLE AROUSAL BY 1.
The Axe of Kolt, The Spectre of Castle Coris, The Fortress of Fear, Die Feuerfaust, The Lost Children, Run, Bronwynn, Run, The Call of the Shaman, The Lost Labyrinth of Lazaitch, Magnetic Moon, Starship Quest, Revenge of the Space Pirates
devileye
Posts: 11
Joined: Mon Mar 01, 2021 4:12 pm
Points: 10
Location: Bristol, UK
Contact:

Re: Using IF statement to change property value

Post by devileye »

Thanks for your reply. The reason is that I want the task to only raise the value of the property/variable by a certain amount, so that the player has to find another task to raise it any higher. Such a task might only become available once Arousal has reached 3, or might even lower the value if used too soon.
User avatar
Lazzah
Moderator
Posts: 2468
Joined: Thu Mar 31, 2011 5:54 am
Points: 90
Location: Clacton-on-Sea, Essex

Re: Using IF statement to change property value

Post by Lazzah »

devileye wrote: Mon Mar 01, 2021 6:04 pm Thanks for your reply. The reason is that I want the task to only raise the value of the property/variable by a certain amount, so that the player has to find another task to raise it any higher. Such a task might only become available once Arousal has reached 3, or might even lower the value if used too soon.
I understand that, but this is easily done using variables. I think you are trying to apply "ordinary" programming using IF - THEN statements to ADRIFT and this doesn't work AFAIK. If you want to check if Arousal has reached 3 then you have a restriction VARIABLE AROUSAL MUST BE EQUAL TO 3. If you want to lower the value of Arousal if used too soon, then you have a task in which there is a restriction VARIABLE AROUSAL MUST BE LESS THAN 3 and an action DECREASE VARIABLE AROUSAL BY 1.
The Axe of Kolt, The Spectre of Castle Coris, The Fortress of Fear, Die Feuerfaust, The Lost Children, Run, Bronwynn, Run, The Call of the Shaman, The Lost Labyrinth of Lazaitch, Magnetic Moon, Starship Quest, Revenge of the Space Pirates
devileye
Posts: 11
Joined: Mon Mar 01, 2021 4:12 pm
Points: 10
Location: Bristol, UK
Contact:

Re: Using IF statement to change property value

Post by devileye »

I can see how that might work, and I'll give it a go. What I find confusing is that the Version 5.0 manual clearly talks about IF - THEN statements in the section about Expressions. To quote:

"Expressions are the closest ADRIFT comes to "programming". They can often provide a more efficient way of calculating values than using restrictions and actions alone. For example, say you were implementing a time system which had hours and minutes. Every time Minute reached 60 you would want to reset it to 0 and increment Hour.

"One way of doing this would be to have a task that incremented Minute by 5. It would need to have a restriction that Minute was < 55. You would need a second task that had a restriction that Minute was = 55, and if so this would set Minute back to 0 and run another task to increment Hour. You would then need to add checks on the increment Hour task to make sure it didn't go past 23. This is getting complicated with lots of tasks.

"Another way of doing this would be so have a single task "increment time" which sets Hour to the following expression:
IF(%Minute% + 5 >= 60, %Hour% + 1, %Hour%) MOD 24"
ralphmerridew
Posts: 2616
Joined: Fri Dec 13, 2002 11:56 pm
Points: 10
Location: Missouri
Contact:

Re: Using IF statement to change property value

Post by ralphmerridew »

In ADRIFT, expressions don't have side-effects (with a few exceptions that only pedants like me care about). You can't put an assignment in the middle of an expression.

I can't change the exact way to do this in the GUI, but it should look like:
Assign property %character%.Arousal to expression IF(%character%.Arousal < 3, %character%.Arousal + 1, %character%.Arousal)
Bloodhounds can make you laugh and cuss in the same breath. They are endearing, faithful, and can sling drool ten feet in any direction. -- Virginia Lanier
User avatar
Lazzah
Moderator
Posts: 2468
Joined: Thu Mar 31, 2011 5:54 am
Points: 90
Location: Clacton-on-Sea, Essex

Re: Using IF statement to change property value

Post by Lazzah »

I have no programming skills, so use Expressions as little as possible. Those that I do use I have asked more knowledgeable Drifters to provide for me, e.g. %Player%.Held(False).Count <= ?

As for IF-THEN statements, they are rocket science as far as I am concerned - wayyyy above my head! :haha:
The Axe of Kolt, The Spectre of Castle Coris, The Fortress of Fear, Die Feuerfaust, The Lost Children, Run, Bronwynn, Run, The Call of the Shaman, The Lost Labyrinth of Lazaitch, Magnetic Moon, Starship Quest, Revenge of the Space Pirates
devileye
Posts: 11
Joined: Mon Mar 01, 2021 4:12 pm
Points: 10
Location: Bristol, UK
Contact:

Re: Using IF statement to change property value

Post by devileye »

ralphmerridew wrote: Tue Mar 02, 2021 1:01 am I can't change the exact way to do this in the GUI, but it should look like:
Assign property %character%.Arousal to expression IF(%character%.Arousal < 3, %character%.Arousal + 1, %character%.Arousal)
I've now tried this, and it works! It was the equals sign in my version that was causing the problem. As you say, the IF statement allows you to 'Set' the value of the property or variable to one of two values.
ralphmerridew
Posts: 2616
Joined: Fri Dec 13, 2002 11:56 pm
Points: 10
Location: Missouri
Contact:

Re: Using IF statement to change property value

Post by ralphmerridew »

Actually, I think ADRIFT has MIN and MAX functions. You can also try

set %character%.Arousal to expression MIN (%character%.Arousal + 1, 3)

When you're only changing by 1 at a time, they'll have the same effect, but if you can change by more than one at a time, the MIN version would mean that overshooting the max gets the max; the IF version has to get there by exact count. Also, the MIN version doesn't repeat as much.
Bloodhounds can make you laugh and cuss in the same breath. They are endearing, faithful, and can sling drool ten feet in any direction. -- Virginia Lanier
devileye
Posts: 11
Joined: Mon Mar 01, 2021 4:12 pm
Points: 10
Location: Bristol, UK
Contact:

Re: Using IF statement to change property value

Post by devileye »

Thanks ralph, that works well and is a more elegant solution.
devileye
Posts: 11
Joined: Mon Mar 01, 2021 4:12 pm
Points: 10
Location: Bristol, UK
Contact:

Re: Using IF statement to change property value

Post by devileye »

Actually I've noticed a problem with ralph's idea of using a MIN expression instead of an IF. If '%character%.Arousal' already has a value greater than 3 (e.g. 5) then the MIN expression will reduce it to 3. The IF expression, on the other hand, will maintain it at it's current value, e.g. 5.
ralphmerridew
Posts: 2616
Joined: Fri Dec 13, 2002 11:56 pm
Points: 10
Location: Missouri
Contact:

Re: Using IF statement to change property value

Post by ralphmerridew »

Sorry, I thought that would be the desired behavior / that the values wouldn't already be above the max.

If you want the behavior you described, you can get it with MAX(%character%.Arousal, MIN(%character%.Arousal + 1, 3))
Bloodhounds can make you laugh and cuss in the same breath. They are endearing, faithful, and can sling drool ten feet in any direction. -- Virginia Lanier
devileye
Posts: 11
Joined: Mon Mar 01, 2021 4:12 pm
Points: 10
Location: Bristol, UK
Contact:

Re: Using IF statement to change property value

Post by devileye »

Imagine you are fighting an ogre and there are various weapons around, some difficult to find. When used against the ogre these weapons increase the ogre's Damage property by a certain amount, and when that Damage property reaches, say, 20 the ogre dies. However each weapon has its limitations: a knife, for example, may only increase Damage by a factor of 1, up to a maximum of 10. To increase Damage beyond that requires something else, perhaps, a sword which has an upper limit of 15. What he really needs is an axe with an upper limit of perhaps 30.

Your MAX/MIN solution would work, but I think the IF solution expresses it better.
ralphmerridew
Posts: 2616
Joined: Fri Dec 13, 2002 11:56 pm
Points: 10
Location: Missouri
Contact:

Re: Using IF statement to change property value

Post by ralphmerridew »

Just do whatever is appropriate for your game.

Question: What should happen if the player is using the sword, which increases damage by a factor of 2, up to a maximum of 15, and the damage is currently at 14?

Also, if you're going to be using that construction a lot, you should look into creating an appropriate User Defined Function. Do that, and you can abbreviate each time to something like Set %character1%.Arousal to expression CLAMPED_INCREASE (%character1%.Arousal, 1, 3)
Bloodhounds can make you laugh and cuss in the same breath. They are endearing, faithful, and can sling drool ten feet in any direction. -- Virginia Lanier
devileye
Posts: 11
Joined: Mon Mar 01, 2021 4:12 pm
Points: 10
Location: Bristol, UK
Contact:

Re: Using IF statement to change property value

Post by devileye »

The sword stroke would result in damage of 16, which is above the maximum for a sword so it would be clamped at 15 and the sword would no longer have any effect. The player would have to find some other weapon capable of doing more damage.

Thanks for the suggestion on User Defined Functions. I haven't explored those yet and find the manual a bit baffling on the subject, but I will do some experimentation.
Post Reply