How do I check for a range of integer values? And how do I increase integers with addition?
Dongers
02 Feb 2022, 19:32I'm having a problem. I want to increase the value of an integer, but I don't know how. Simply putting a +2 at the end of the set to command doesn't work, neither does Object.attribute= Object.attribute +2
so how can I increase the integer? I don't want it to be set to a specific number, I only want it to increase the more times this command is being called.
I also want to check for a range of integer values, for example if the value is currently anywhere between 0 and 2 and tried this:
if (Object.attribute = 0 and 1 and 2)
But then it says that the AND function isn't supported for integers, so what do I do? I also tried with "or", but that isn't supported either. Do I have to create an Else If for every single possibility?
mrangel
02 Feb 2022, 20:06I'm having a problem. I want to increase the value of an integer, but I don't know how. Simply putting a +2 at the end of the set to command doesn't work, neither does Object.attribute= Object.attribute +2 so how can I increase the integer? I don't want it to be set to a specific number, I only want it to increase the more times this command is being called.
Object.attribute = Object.attribute + 2
is the usual form.
If you want to input this in the GUI, you will find it under "Variables" → "Set a variable or attribute". Put Object.attribute
in the left box, and Object.attribute + 2
in the right. Make sure the type is set to "Expression"; otherwise you'll be entering the command Object.attribute = "Object.attribute + 2"
which doesn't work.
Note that you can only use this if the attribute is an integer – you will need to set it to zero (or whatever its starting value is) before you can add something to it.
You could also do it using set (Object, "attribute", Object.attribute + 2)
, which is generated by "Set an object's attribute (named by an expression)" in the GUI.
I also want to check for a range of integer values, for example if the value is currently anywhere between 0 and 2 and tried this:
and
takes two boolean statements (things which can be true
or false
) and tells you if they're both true.
What you want is one of:
if (Object.attribute = 0 or Object.attribute = 1 or Object.attribute = 2) {
orif (Object.attribute >= 0 and Object.attribute <= 2) {
orif (Object.attribute <= 2) {
if you don't need to check for negatives.
If you want to check for several values that aren't consecutive numbers, it might be easier to use switch
. Like this:
switch (Object.attribute) {
case (0) {
msg ("The attribute is zero")
}
case (1,3,5,7) {
msg ("The attribute is a low odd number")
}
case (2,4,6) {
msg ("The attribute is a low even number")
}
default {
msg ("The attribute is 8 or more")
}
}
You can have as many options as you want in each case
, and as many cases as you need. If there's a lot of different numbers that count, it can be worth using switch
even if you only need one case
.
Dongers
02 Feb 2022, 20:46The if (Object.attribute = 0 or Object.attribute = 1 or Object.attribute = 2) {
only works once and causes an error the second time it is used. The switch however works so far.
"Object.attribute = Object.attribute + 2
is the usual form."
I guess the mistake I made was that I wrote +2 instead of + 2? Honestly thought that space wasn't that important.
mrangel
02 Feb 2022, 22:17only works once and causes an error the second time it is used
What error?
Error messages are there to tell you what the problem is with a line. If you don't understand the error message, you can share it here and get some help interpreting it.
I guess the mistake I made was that I wrote +2 instead of + 2? Honestly thought that space wasn't that important.
Spacing shouldn't make a difference. Is it possible you mistyped the name of the attribute or the object?