I’ve just introduced a minor character who has a lot of power, but is only in the game for like two chapters. Instead of giving em a proper percentage relationship score, I decided to just assign a 1, 2, or 3 for what e thinks of you. This makes it possible to do easy multireplaces within sentences.
Except, the chapter is turning out more complicated than I intended, and I’ve lost track of how many time you might have had a chance to move er opinion of you up or down. Is there a way to set parameters to stop the score going below 1 or above 3? (Or, ideally, to get the code to return the multireplace option for ‘1’ for any score lower than 1, and ‘3’ for any score higher than 3.)
I think the best idea I can think of is to use a percentage variable, but name the different percentages. So,
Actually that’s not bad. Shall I post this now I’ve just answered my own question? Ummm… yes, maybe someone else will one day want to know how they could do this? Or maybe there is a more efficient way?!
Edited to add:
Of course, this does mean every time I want to use my multireplace, I have to call a gosub containing the above. Which is a bit of a faff.
Is there a way of having a variable automatically check what it is supposed to be against another variable without the code having to tell it to?
As far as I know there’s no way to clamp a variable to not go below a certain value. What you doing for a throwaway variable seems efficient to me. There’s input_number that accepts a range but that’s for user inputs and not normal variables
I suppose you could do something like this when you want to increase:
*if admiral_opinion < 3
*set admiral_opinion +1
And then this when you want to decrease:
*if admiral_opinion > 1
*set admiral_opinion -1
That way, if you’re at 3, it doesn’t increase further, and if you’re at 1, it doesn’t decrease.
Unlike elseif and else, ‘if’ doesn’t require a goto/gosub/return etc ending it.
ETA:
I don’t know if this would work, but you can also use multireplace to set variables. The issue is that it sets them as strings so I’m not sure if it’d work with numbers. Worth testing though?
Yeah, unfortunately percentile stats with fairmath are the only way to have numerical variables automatically bounded within a range.
However, with a little math you can convert a fairmath percentile into something multireplace can work with. The generic formula is (((x-(x modulo y))/y)+1), where x is your fairmath percentile and y is the lower bound of your second level.
The admiral's opinion of you is @{(((rel_admiral-(rel_admiral modulo 33))/33)+1) low|middling|high|high}.
More examples:
x
((x-(x modulo 20))/20)+1
1-19
1
20-39
2
40-39
3
60-79
4
80-99
5
x
round(x/20)+1
1-9
1
10-29
2
30-49
3
50-69
4
70-89
5
90-99
6
Edit:
I take back what I said; that’s a super cool and minimalist way to keep a stat bounded. It will work, because “numeric” variables are actually a lie; everything in CS is set as a string anyway. The only slight drawback is it’s not immediately obvious (or it wasn’t to me) that the purpose of the lines is to increment/decrement by 1. Not a problem if you’re the only one working on the code, though I’d personally write it as
for clarity. It would also work better if you needed increment/decrement by more than 1, or if your range was larger, like 1-20 for instance. But for small ranges that’s super slick.
Not precisely the cutoff you specified, but the best you can do with modulo. ↩︎
You could pair this with *gosub_scene and a label to make this callable from anywhere in the game too, but in this case, if it is only for a couple of chapters, it might be easier to just type that out each time.
OK, this does look extremely elegant, but I don’t understand! Could someone please explain how the 1|2|3 know what they should mean? Like, let’s assume rel_admiral is 70%. How does the code know which of the admiral_opinion options it should include?
The cut offs on this are at 25% and 75% which might be a bit too extreme i.e. ( <25% = 1, > 75% = 3, anything else = 2). Otherwise using a gosub is most likely your best option.
Edit: I actually use the method suggested here by LisHz for simple variable bounding where I have a very limited range of options which move in discrete jumps. For something with more nuance (i.e. can move in smaller increments which don’t necessarily push it into the next bracket) then I would be using ‘if’ statements in a gosub.
I would just set caps. Maybe store it in a gosub_scene so it can check if it is too high or too low with the same code. Sorry if there are errors, I’m slightly out of practice.
I’ve never gotten a multireplace to successfully modify my variables. It always errors for me, now that I know all variables are stored as a string—which doesn’t make sense to me since it does maths—I think I will test the suggestions in this thread on future projects.
When using multireplace to modify variables, you have to surround the multireplace with quote marks. You also cannot use quote marks within the multireplace. (I set many variables within my draft with multreplace. Usually though just so I can nest them).
And, yeah, I think just using if statements is easier, hence my initial suggestion.
In the first case, if admiral_opinion = 3, the stat remains unchanged. (And it can’t be over 3 b/c this prevents it from going over). In the second case, if admiral_opinion is 1, it remains unchanged. (And it can’t go under 1 b/c this prevents that, too).
Well, if greater than 3, it doesn’t change either. Then you just have to proof your code, ctrl+f for “set admiral_opinion” ensuring every instance uses the bounded code, whether that’s using if statements or multireplace.