Checking if a variable has dropped by X?

Is there a way to check if a variable has dropped by an amount rather than checking a specific value?

In example, there’s a specific scene I’m looking for in this involving a faction’s supplies dropping too rapidly. Rather than “it went below X” it would be more akin to looking at a graph than looking at a static number.

One such example:

Supplies are at 90 then drop to 80; triggering the scene.

Supplies are at 30 then drop to 20; still triggering the same scene.

I’m not necessarily looking for it to trigger on a specific amount of supplies but rather the drop itself. I hope I’m being transparent here as I’m not sure what to say to explain this other than what I’ve already said.

I also have no idea what to look for on the C-Script wiki for this concept. I would assume variables but that didn’t seem to give a solid answer to this one.

Not that I’m aware of or that I can figure out off the top of my head. But, if the drop coincides with a specific event, you could always use a boolean and check that. Like
*temp drop False
if the character or whoever does the thing that causes the drop, set it to true, then later check

*if (drop) 
    bla bla bla
*if not (drop)
    bla bla bla

Or, if it’s a series of actions, you could add +1 with each action then check to see

*if (drop >= 3)
    bla bla bla
*if (drop < 3)
    bla bla bla

But without more context, I have no idea if that would work for you.

2 Likes

Thanks. Side question;

Does *if work to hide text outside of choice statements? I was considering using multireplace but I don’t know if it will work if there are 7 possible variables to check. Nor am I well versed enough in its use to start using it seriously.

Example:

Bla bla ba

*if (variable = “text”) You eat a cookie.

*if (variable = “text”) You die in frog rain.

It does, yeah. Typically, I use multireplace if what I’m changing is minor, like a few words, maybe a sentence at most, and I use *if when I know it’s going to be a bigger chunk, like paragraphs or involve so many dependent details that proofing it will be too much work. That’s just me though, I can’t speak to how other people do that.

Thanks. I haven’t actually seen anything worth using multireplace for just yet. At least in my own writing.

Lots of people use it for pronoun dependent verbs. So like if he runs but they run. You’d have something like

*create pc_pv 1
*comment pc he/she = 1; pc they = 2
$!{pc_they} run@{pc_pv s|}.

And get “He runs.”

I also use it to pull up variables that I assign numbers instead of strings to. Like if you set hair color to numbers instead of “red” “blond” etc.

*create hair 0
*set hair 2
*comment hair: 1 blond, 2 red, 3 brown
She brushes her @{hair blond|red|brown} hair.

In cases like that, it might be just as easy to set a string and just call the variable though. :man_shrugging:t3: But just as an example.

3 Likes

General tip: use the pre-formatted block to share code in the forum. It makes it easier for other people to read. You can wrap the code around triple back-ticks ``` at the beginning and end of the block. Or use the code symbol in the edit bar when making a post. It looks something like this: </>.


There’s no native way to get the value variation. But it’s fairly straightforward to calculate. Assuming you’re talking about fair-math, you could do something like this:

*temp difference ((variable %+ 15) - variable)
*set variable %+ 15
2 Likes

Nah not fair math. Talking about a variable that changes and marking the change as an *if statement to provide a specific scene.

Okay, but how is it changing? If it’s not fair-math, then it’s even easier to know the variation since you are the one implementing it. If you subtract 20, then the variation is -20.

It’d be tracking the variable change over X time to produce any Y value.

Say for example it changes from 40 to 20 in 1 scene (scenes being our “time” value here) then it registers. But not if it goes from 40 to 35 in 1 scene.

The idea here is not a set value but rather change/time. Change too quickly; the scene triggers. Change slowly and we’ve all heard the old bit about a frog in boiling water.

If you’re talking about callback functions, event dispatcher, observer pattern, state-machine or anything in the likes, then it is not possible with ChoiceScript.

You cannot program an event to trigger automatically when a certain condition is met. You have to check if the condition has been met manually at any interval you deem necessary.

For more complex logic, you have to build a “time-tracking” system: a variable for “time” that you increment manually. Then keep track of previous values for the variables that might trigger the event and check the state at checkpoints, which could be at after every choice body, label or page_break, which guarantees at least one check at every page break. Or make it more sparsed. Like I said, whenever you deem necessary.

Unfortunally, you have to everything by hand.

1 Like

No idea what any of that meant but I’ll assume you understood my poorly explained situation.

If you’re checking over a set period, what about using extra variables to keep track of the initial value and then check (via subtraction) how much it’s changed? For instance, at the beginning of chapter one, have var_original, then later check how much var has changed from var_original. Something like this:

Chapter beginning:

*set var_original var

(This sets var_original to have the same value as var.)

Then at the end of the chapter, when you need to see how much has changed – supposing a change of 20 is high enough to trigger the condition:

*set change (var_original - var)
*if change >= 20
     *goto condition

*label condition
The variable changed quickly!

Would that structure work?

I made a little sample scene to demonstrate:

*temp change 0
*temp var_original 50
*temp var 50

*fake_choice
    #Subtract ten from my value.
        *set var -10
    #Subtract twenty from my value.
        *set var -20

*set change (var_original - var)
*if change >= 20
    The variable changed quickly!
    *goto condition
*else
    The variable only changed a little bit.
    *goto the_end

*label condition

*page_break

Now we get a special scene.

*label the_end
*ending
6 Likes

It means this:

1 Like

Thanks.

1 Like

Well, that’s exactly what I said, but simplified. You’ll still have to encapsulate it in a subroutine and manually track time if you intend to use in more places. If not, then @Fiogan’s solution is the best you can ask for.

It’s the one I understood. Apologies.

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.