Print message based on range of integer attribute?

XanMag

What is the syntax for printing a message for an attribute that falls between a range of numbers?

My plan is to print a message when the health of the opponent falls inside a certain range. I also only want that message to print one time. Example: Attack 'fanged rat' --> "You strike the rat with your cup of pudding (?) and it winces in pain." That attack does 13 damage and the health of the rat falls from 100 to 87. After the attack, I would like to print a message like "The rat looks angered and ready to nibble your bum." I'd like it to print only once in the range between 80-90 though. Then, when the health falls in between 50-60 (the next range), print another message like "The Fanged Rat seems a bit weaker, yet still a dangerous foe."

Does that make sense?


hegemonkhan

within a range: you use the 'and' logic operator (and make sure you got your greater-thans/lesser-thans, correct!), an example:

// within 0-100:
if (player.strength >= 0 and player.strength <= 100) { /* scripting */ }

outisde of a range: you use the 'or' logic operator (and make sure you got your greater-thans/lesser-thans, correct!), an example:

// outside of 0-100:
if (player.strength < 0 or player.strength > 100) { /* scripting */ }

a way to help with understanding the use of 'and' vs 'or', is to use a VENN diagram:

http://inet.mountsaintvincent.edu/library2/venn.htm
http://instructor.mstc.edu/instructor/gwebster/Problem%20Solving/Sets%20&%20Venn.pdf (math-graphing example: unions=and, intersections=or)
https://www.allaboutcircuits.com/textbook/digital/chpt-8/boolean-relationships-on-venn-diagrams/ (circuitry/computer architecture - more advanced programming)

a pretty good link for this stuff


XanMag

So...

if (Fanged Rat.Health >= 80 and Fanged Rat.Health <= 90) { /* scripting */ }

hegemonkhan

just edited my last post, added in a link for VENN diagrams on helping with the logic of 'and' vs 'or' logic operators/operations


yes, that is correct, for WITHIN 80-90 (inclusive: including 80 and 90)

you would do something like this:

// obviously (hopefully), this example function is horribly incomplete/wrong/bad (infinite loop) -- it needs much more code lines, but it's just a simple-quick-brief example:

<function name="combat_example">
  if (Fanged Rat.health >= 80 and Fanged Rat.health <= 90) {
    firsttime {
      msg ("blah first time")
    } otherwise {
      msg ("blah otherwise")
    }
  }
  combat_example
</function>

XanMag

Cool. Thanks. I'll give it a try and let you know.


hegemonkhan

you can make the code more efficient, with something like this (guessing on your combat design):

the series of 'ifs/else ifs/else' does the same effect as using the 'and' operator, and is more efficient (less operations)

if (Fanged Rat.health >= 100) {
  msg ("100-positive_infinite")
} else if (Fanged Rat.health >= 90) {
  msg ("90-99")
} else if (Fanged Rat.health >= 80) {
  msg ("80-89")
} else if (Fanged Rat.health >= 70) {
  msg ("70-79")
} else if (Fanged Rat.health >= 60) {
  msg ("60-69")
} else if (Fanged Rat.health >= 50) {
  msg ("50-59")
} else if (Fanged Rat.health >= 40) {
  msg ("40-49")
} else if (Fanged Rat.health >= 30) {
  msg ("30-39")
} else if (Fanged Rat.health >= 20) {
  msg ("20-29")
} else if (Fanged Rat.health >= 10) {
  msg ("10-19")
} else {
  msg ("negative_infinite-9")
}

Silver

It can also be done with the text processor as discussed in this thread:

https://textadventures.co.uk/forum/quest/topic/t4zifniuyegnzqj6c4wrqa/easy-text-processor-question-s-s-solved


XanMag

Okay...

When the player leaves the room (runs away), here is the script I have in the After Leaving Room section. I want the rat health to reset to 100.

Fanged Rat.Health = Fanged Rat.Health = 100

But, when the player leaves the room, I get the following error message.

Error running script: Error compiling expression 'Fanged Rat.Health >= 80 and Fanged Rat.Health <= 90': CompareElement: Operation 'GreaterThanOrEqual' is not defined for types 'Boolean' and 'Int32'

DarkLizerd

Too many "="'s...
what you are accidently saying is...
Fanged Rat.Health = False...
you are assigning a Boolean to a number...
(hmm... just like the error says...)
all you need is:
Fanged Rat.Health = 100


XanMag

Doh. Will try. Thanks!


Log in to post a reply.

Support

Forums