firsttime not working?
Zabikai
10 Dec 2020, 00:00Hi. I use firsttime to do something the first time they hit X level, and then again later for the first time they hit Y level. The X level one works, but the Y level one doesn't? Sorry if it's a dumb mistake but thanks for helping!
mrangel
10 Dec 2020, 02:11What does the code look like?
If you want to know what's wrong with your code you need to show us the code.
You can copy and paste from code view into the forum. Put a line of 3 backticks (```
) above and below the code.
Zabikai
10 Dec 2020, 10:40Here you go:
if (game.score>=50) {
firsttime {
msg ("<br/>You have gained a bit of muscle, and can wield weapons better now")
}
}
else if (game.score>=100) {
msg ("<br/> Your muscles grow again, making you visibly stronger")
}
I want to puit a firsttime before the message in game.score over 100, but whenever I try the code breaks
mrangel
10 Dec 2020, 11:10That's already broken. The else
clause only triggers if the initial if
doesn't. So the second message will only display if your score is over 100 but not over 50.
There's a few different ways to do what you want; hopefully you can understand the differences.
The usual way to do this for a programmer would be something like:
if (game.score>=100) {
firsttime {
msg ("<br/> Your muscles grow again, making you visibly stronger")
}
}
else if (game.score>=50) {
firsttime {
msg ("<br/>You have gained a bit of muscle, and can wield weapons better now")
}
}
The first message appears the first time the score is over 100; the second if the score is over 50 but under 100.
This is probably the most efficient way to write the code, but can be confusing to a non-programmer because the messages in the script are in the opposite order to how they're shown in the game.
(in this case, if your score somehow goes straight from 49 to 101 all at once, it will only display the >100 message)
An alternative, often recommended on this forum, would be:
if (game.score>=50 and game.score < 100) {
firsttime {
msg ("<br/>You have gained a bit of muscle, and can wield weapons better now")
}
}
else if (game.score>=100) {
firsttime {
msg ("<br/> Your muscles grow again, making you visibly stronger")
}
}
This is the same as the above, but runs slightly slower and might be easier for a non-programmer to read.
Another alternative might be:
if (game.score>=50) {
firsttime {
msg ("<br/>You have gained a bit of muscle, and can wield weapons better now")
}
}
if (game.score>=100) {
firsttime {
msg ("<br/> Your muscles grow again, making you visibly stronger")
}
}
In this case, removing the 'else' means that each message is independant. If the player scores 101 points all at once, it will show both messages. I don't know if this can happen in your game, but if it can there's a few options about how it should behave.
One alternative would be changing the else
to an otherwise
; so that the second message only displays if the first one hasn't. For example:
if (game.score>=50) {
firsttime {
msg ("<br/>You have gained a bit of muscle, and can wield weapons better now")
}
otherwise {
if (game.score>=100) {
firsttime {
msg ("<br/> Your muscles grow again, making you visibly stronger")
}
}
}
}
In this case, going from 49 to 101 would display the first message; and the second message would display the next time the score changes.
Or if you just want the first message in that case (this only works if the script is in the changedscore
script attribute), it would be:
if (game.score>=50 and oldvalue < 50) {
msg ("<br/>You have gained a bit of muscle, and can wield weapons better now")
}
else if (game.score>=100 and oldvalue < 100) {
msg ("<br/> Your muscles grow again, making you visibly stronger")
}
Zabikai
10 Dec 2020, 12:10Thank you so much! However, I still get "An Internal Error Occurred" every time I try to add the second firsttime