"If Attribute" combos help

CheshireTiger
03 Jan 2020, 17:00so I've run into some errors while using an attribute on player character to learn skills from npc
the first happens here, when player can see details about next skill:
if (HasAttribute(Kurota, "Skills")) {
PrintCentered ("Dash & Slash {Blade} S dammage to all enemies")
}
else if (Kurota.Skills = 1) {
PrintCentered ("Breeze [Gust} S damage to all enemies")
}
Then there is another when given the option to learn the next skill:
if (Kurota.Skills = 0) {
msg ("Dalia teaches you how to slash while running past enemies. She says its a simple teqnique compared to other things she can teach you...")
PrintCentered ("~NEW SKILL: Dash & Slash~")
set (Kurota, Skills, = 1)
}
else if (Kurota.Skills = 1) {
msg ("After telling Dalia how you handled the trapper, she helps you work on that skill until it affects a wider area.")
PrintCentered ("~NEW SKILL: Breeze~")
set (Kurota, Skills, = 2)
Id also like to have an inventory item show a list of all curennt skills, displaying each one as the attribute value increases like:
if (Kurota.Skills > 0) {
msg ("Dash & Slash: {Blade} S damage all enemies")
}
if (Kurota.Skills > 1) {
msg ("Breeze: [Gust] S damage all enemies")
}
I Realize im asking about 3 issues at once, and I dont expect replies to tackle aal 3 at once, but any help would be apreciated
mrangel
03 Jan 2020, 19:30Not sure if someone else will have answered these before me, as I got called for dinner in the middle of writing my answer. But here it is anyway:
if (HasAttribute(Kurota, "Skills")) { PrintCentered ("Dash & Slash {Blade} S dammage to all enemies") } else if (Kurota.Skills = 1) { PrintCentered ("Breeze [Gust} S damage to all enemies") }
If they have an attribute called "Skills", the first part runs, regardless of what value the attribute has.
If they don't have an attribute called skills, Quest looks at the else if
statement, which compares the attribute to the number 1. This will cause an error, because you can't compare a number to a value that isn't defined.
(In Quest, an undefined attribute has the special value null
which is technically an object, so this will generate an error message telling you that =
can't compare an object to an int)
Looking at the others, I'm guessing that you probably meant:
if (Kurota.Skills = 0) {
PrintCentered ("Dash & Slash {Blade} S damage to all enemies")
}
else if (Kurota.Skills = 1) {
PrintCentered ("Breeze [Gust} S damage to all enemies")
}
However, this would still fail if Kurota.Skills
has no value at all. If the attribute isn't set to 0 initially, you might want to test it first with something like:
if (not HasInt (Kurota, "Skills")) {
Kurota.Skills = 0
}if (Kurota.Skills = 0) {
PrintCentered ("Dash & Slash {Blade} S damage to all enemies")
}
else if (Kurota.Skills = 1) {
PrintCentered ("Breeze [Gust} S damage to all enemies")
}
(setting the attribute to 0 if it hasn't been initialised yet)
or if you want "not yet defined" to mean something different from zero:
if (HasInt (Kurota, "Skills")) { if (Kurota.Skills = 0) {
PrintCentered ("Dash & Slash {Blade} S damage to all enemies")
}
else if (Kurota.Skills = 1) {
PrintCentered ("Breeze [Gust} S damage to all enemies")
}}
else {
Some code you want it to run if Kurota.Skills doesn't have a value at all
}
An aside: It's probably worth noting at this point that if you're going to compare one attribute to a lot of different numbers, it's more efficient to use switch
than if
. In your example, Quest is looking up the value of Kurota.Skills
every time it compares it to a number. It's not a big difference, but this version would only need to look it up once:
switch (Kurota.Skills) {
case (0) {
msg ("Dash & Slash: {Blade} S damage all enemies")
}
case (1) {
msg ("Breeze: [Gust] S damage all enemies")
}
}
The switch statement looks up the value of the attribute you pass to it, and then skips to the first case
with the same value. It's more efficient than using lots of else if
; like looking up the thing you want in the index of a book, rather than turning pages until you find it.
Probably won't make enough difference for a player to notice, but it's worth knowing.
Second piece of code:
You have:
set (Kurota, Skills, = 1)
I think you mean either:
Kurota.Skills = 1
or
set (Kurota, "Skills", 1)
Those two mean pretty much the same thing.
Note that you use the =
sign or the set
keyword, but not both; and the two have slightly different grammar:
- The
=
operator has two operands:- Left hand side operand: - a variable or an attribute
- Right hand side operand: - the value you want to set it to
- While the
set
statement has three parameters:- 1st parameter: An object
- 2nd parameter: The name of an attribute (so you either put it in quotes, or give a variable that contains the name of the attribute)
- 3rd parameter: The value you want to set it to
Third piece of code:
That looks correct.
If it isn't working, could you give some more information? When you have a problem, there are 4 things we need to work out what's wrong:
- Code
- Test context
- Expected result
- Actual result
(This is a useful step towards thinking like a programmer. If you can phrase a problem as "I'm trying this code: [quoted code] with the attributes set to [list of any attributes the code uses and their values when you tested it]. I expected it to output [quoted text] but instead it gave me [quoted text]" then that's probably all the information a programmer needs to spot the problem. And once you get used to the language, you'll probably find that writing it like that makes you more likely to notice the problem

CheshireTiger
03 Jan 2020, 19:48Suprise, Mr.Angel, youre the 1st!
So i can use 'switch' on the atribe value instead of 'else if'...
Dont use = with set
And ill try to rephrase if the 3rd part dosent work....
Thanks.
mrangel
03 Jan 2020, 20:17Ah… on second look I think the 3rd one might be off by one. You might want to use >=
("greater than or equal to") instead of >
("greater than")
Sadly there's no way to make case
work with greater than or less than. But you can have a case with multiple values separated by commas. (For example, I think case (null, 0) {
will make this option display if the attribute is zero or if it hasn't been set yet)

CheshireTiger
03 Jan 2020, 21:59Case wont work with </>? Noted