Altering different stats with only the change in a single line of code

I was messing around with choicescript just and found a way to alter multiple variables with the change only requiring a single line of code.

For the test I used three variables;

[target] which determines which variable I want to change.
[hit_points and mana_points] the variables we want altered.

Then I coded a choice of being attacked by either a Bandit (lowers your hit_points) or a Demon (lowers your mana_points.)

Then all I need to do is write *set {target} %-5 at a single point then it will reduce whatever I choose as the target.

Test code is below;

*create hit_points 100
*create mana_points 100
*create target ""

*label fight
You have ${hit_points} HP remaining.
You have ${mana_points} MP remaining.
*choice
  #The bandit hits me.
    *set target "hit_points"
    *goto next_scene
  #The demon curses me.
    *set target "mana_points"
    *goto next_scene
  
*label next_scene
*set {target} %-5
*goto fight

Try this and whoever you pick as the attacker either your hit_points or mana_points will be reduced by 5%. Which I think it pretty neat and might be useful to others :slight_smile:

Edit: This code also allows you to change booleans, strings and number variables all using that single target variable.

The code below using the target variable for changing a boolean, a string and a number variable!

*create hit_points 100
*create mana_points 100
*create bill false
*create ben false
*create target "" 
*create partner_name ""

*label fight
You have ${hit_points} HP remaining.
You have ${mana_points} MP remaining.
*choice
  #The bandit hits me.
    *set target "hit_points"
    *goto next_scene
  #The demon curses me.
    *set target "mana_points"
    *goto next_scene
  #Next test.
    *goto new_scene
  
*label next_scene
*set {target} %-5
*goto fight

*label new_scene
You are with…
*choice
  #Bill.
    *set target "bill"
    *goto another_scene
  #Ben.
    *set target "ben"
    *goto another_scene
  
*label another_scene
*set {target} true
*if (bill)
  *set partner_name "Bill"
  You are with ${partner_name}.
  *set target "partner_name"
  *goto final_scene
*if (ben)
  *set partner_name "Ben"
  You are with ${partner_name}.
  *set target "partner_name"
  *goto final_scene

*label final_scene
After a few days ${partner_name} has to leave. They are replaced with…
*page_break Megan
*set {target} "Megan"
They are replaced with ${partner_name}.
*ending
8 Likes

Dynamic coding here we come!

I practically abuse the poor *gosub and *params keywords. They’re so useful for coding like that.

I’m familiar with *gosub but not *params? what is that?

Let me tell you about the miracle that is *params. Essentially, you can pass specific pieces of data to a *gosub and *gosub_scene.

Where to go?
*fake_choice
    #Go to dinner
        *gosub outing "dinner"
    #Go to club
       *gosub outing "club"

Such a good way to spend the night.
*ending

*comment Params down here takes the string passed from above.
*label outing
*params where
*if (where= "club")
     You had a lovely time at the club.
    *return
*elseif (where = "dinner")
    You had a lovely time at dinner.
    *return

If you’re familiar with other programming languages, it works like an argument for a function. The post that has a better explanation is here.

2 Likes

Nice trick @Nocturnal_Stillness . You can do really cool stuff when you start using that kind of redirection.

@StorybookParagon please note that params in goto and gosub are not “scoped” like in a function in most programming languages. Their name may collide with a temp or stat, and you may modify your data by mistake - I had some nasty bugs that way! But yes, it’s a miracle. :slight_smile:

3 Likes

The one thing I wish I could figure out is how to just substitute part of the check.

I.e *set {target}_health %-10

But that throws up an error.

Try this:

*create mc_health "50"
*create target "mc"

${{target & "_health"}}

Output:

50
2 Likes

I’ll try that when I get home from work.

Edit: can you use that to *set a variable as well? Not got access to my laptop atm.

But could it do this.

*set {{partner & “_rep”}} %+10

So it increases the rep of whoever “partner” is?

Just tried it and it doesn’t seem to work for what I wanted to do. Thanks anyway.

1 Like

Would the array syntax work? I didn’t try.

*set {target}[health] %-10

1 Like

No it didn’t it doesn’t read the variable right. I’m not sure if that’s possible.

1 Like

Ok, so you will have to do it the hard way. This should work if I remember correctly.

*temp varname target&"_health"
*set {varname} %-10

2 Likes

Yeah that works for the test. Now to experiment a little. Thanks!

EDIT: And it works for what I was after.

*create alan_rep 50
*create billy_rep 50
*create charlie_rep 50
*create alan_training 50
*create billy_training 50
*create charlie_training 50
*create target ""

*label start
Who do you want to train?
*choice
  #Alan.
    *set target "alan"
    *goto increase
  #Billy.
    *set target "billy"
    *goto increase
  #Charlie.
    *set target "charlie"
    *goto increase
  
*label increase
*temp name target&"_rep"
*set {name} %+10
*temp training target&"_training"
*set {training} %+10

Please check your stats.
*goto start

For this example each NPC has two stats reputation and training and with that code you suggested I can change either or both for any of them reducing the lines of code required.

2 Likes

If you want to cut the number of lines further, you can eliminate the temps:

*set {target&"_rep"} %+10
*set {target&"_training"} %+10
3 Likes

I wondered about that myself. But that should work as well.

1 Like

If you wanted to use string values in the fake array syntax, you have to enclose it in double quotes:

*set stat["health"] 10

… or so.

2 Likes