Best way to improve a code

Hi there,

First of all, i sincerly apologize for my writing, english is not my native langage.

Now let’s talk about the techy thing !

I’am trying to think about a efficient way to write a perticular code. I have 2 point which need to be corrected/optimised.

And if there is experienced coder here, feel free to share if you have an idea.
Let me explain and let’s keep this simple :

I have 2 variables for exemple :
var 1 : recklessness
var 2 : cautiousness

This two variables are related (if one increase the other decrease, and reciprocally).

All the time and a lot of time, during the aventure, there is going to be this kind of line :

*set recklessness + 5
*set cautiousness - 5

1 ) First thing i am trying to solve :

Each of this two variables can’t be superior than 100. To verifie it, i have a general subroutine who is played relay regulary during the adventure. Including, Inside this subroutine, we have :
*if (recklessness > 100)
*set recklessness = 100

But to total of this two variable have to be equal to 100.
(cause this is how i display it in the choicescript trait bar)

So you CAN have :
[ recklessness 75 % / cautiousness 25 % ]
or
[ recklessness 12 % / cautiousness 88 % ]
etc
but you CAN’T have :
[ recklessness 12 % / cautiousness 33 % ]
or
[ recklessness 38 % / cautiousness 98 % ]
Because the total is not equal to 100 and it doesnt make any sens.

This bad thing happens i my story because when, in my subroutine, i do this :
*if (recklessness > 100)
*set recklessness = 100
I don’t take into account my other variable (here cautiousness) and i know i should make so kind of differencies (with the rest and adding to the cautiousness substraction) but i have no idea how to do it easily.

  1. Seconde point

I also have other character caracteristics (like strenght or stealth for exemple). And iam trying to make the psycological caracteristics (recklessness, cautiousness) have an impact to it.

I explain :
Stealth and strenght will only increase when you obtain a new level (when you spent you skill point). Anything else will modifif their value, exept that :
When your recklessness become superior than your cautiousness, you obtain a bonus to your strenght and when your cautiousness become superior than your recklessness, you obtain a bonus to your stealth skill.

So like you saw in point 1, if you understood how it works, you can ONLY have one bonus at the time.
So if you have a strength bonus for exemple, and your recklessness drop below the bar of 50%, then you loose if and you obtain a bonus to your stealth.

The matter i’ve met in the code is this :

*if (recklessness> cautiousness)
*set strength + 5
*set stealth - 5

BUT this is completly wrong. Cause I want to apply the bonus only once => Only each time one of the variables (recklessness/cautioness) are becoming higher the the other. Because otherwise, each checking will increase strenght and lower stealth as long as recklessness will be superior cautiousness…

So there is it. I am currently wondering how i could make that work in my general subroutine.
Subroutine which, i remember you, is played really often.

Thanks a lot if anyone have any tips , advice, or , lets be crazy : any solution !

I’m no coding expert, but I thought I would make a few suggestions, and you could see what you think.

If recklessness and cautiousness are opposed, so that when one increases the other one always decreases, why not make them an opposed pair? Those are explained about halfway down this page in the wiki. Then, you can either put *set recklessness +5, and cautiousness (perhaps better as ‘caution’?) would be automatically decreased by the appropriate amount.

If stealth and strength are also always changed together, you could make those into an opposed pair as well.

Then, when you want to check whether a character is more reckless or cautious, all you have to do is this:

*if recklessness >= 50
*set strength +5
*if recklessness < 50
*set strength - 5

As for stats not increasing over 100, you can do that with fairmath, which is explained on this page, also about halfway down.

I hope that’s helpful! Feel free to ask away if you have more questions, and perhaps others will chime in with better ideas as well.

Thank you very much for your answer. I’ve read carfully your answer and I check and read the pages you linked me, but i don’t think you understood all the aspect of my problem.

Let me explain :

  1. You recommand me to use opposed pair. I already use them but i think this is only for display utilisation (in the stat_chart for exemple).
    The thing is, i use both variables, caution and recklessness, independently, during my adventure. What i mean is, sometimes I use recklessness as a condition in a dialog. sometime i use caution in an other dialog, so i need them both to be real variable. In the exemple on the page you gave me, there is “intelligence” and “supidity” but stupidity is not a variable, its here only for display.

  2. Contrary to recklessness and caution, stealth and streanght are not always changing togheter. Like i explain, each time you level up, you can use to increase a skill by spending you skill point. So the player can choose to increase only one of them.

They are changing together only when recklessness/caution are becoming superior/inferior than their opposite, and only at this moment.

But I don’t think you understood i have a subroutine which is play really really often and where a lot of stuff are recalculated.

But all the core of the problem is about this subroutine, cause If i put the code you write :

*if recklessness >= 50
*set strength +5
*if recklessness < 50
*set strength - 5

It will be a endless incresement of one of the variables.

What i want, it the strenght/stealth to be increase/decrease by my subroutine, only each time the bonus have to change. And the bonus have to change only when recklessness become superior than caution or caution become superior than recklessness.

If caution/recklessness is already superior to their opposite, I don’t want to keep applying a bonus, otherwise, i will be increasing streath/stealth each time my subroutine is playing, and it’s a LOT OF TIME ^^

So there is it, i hope you understood a little bit more what is my matter now :slight_smile:

Thank you again for your answer :smile:

On your first point, the opposed pairs can be used for more than just display purposes. As you say, “recklessness” is a variable so using it for checks and balances won’t really be a challenge, caution is however not a variable. What you can do to track it though is use “recklessness” to track “caution”. Since they’re linked, and function on a scale of 1 - 100 (assuming you’ve set min/max values since it is possible to go beyond these) then you’ll know that when “recklessness” is 60% then “caution” is 40%, or when “recklessness” is 20% that “caution” is 80% ect.

From there you can code your checks as follows.
*recklessness >= 60 (to check recklessness as you normally would)
*set strength +5

*if recklessness >=40 (to use recklessness to check caution, so the “real” value here is caution 60%)
*set strength -5

I hope this helps?

Thank a lot. That help, indeed. I think this is even solving my first problem. You are right, i do not need 2 variables for recklessness/cautious

But the second problem stay here. Let me explain you why :

even if i do what you write, strenght will keep increasing or decreasing beacause of my subroutine.

Because its a bonus but its not cumulable.

For exemple : at lvl 2 the characater have :

Strengh : 10
Stealth : 35

Recklessness is at 60 and caution 40, so he get a little bonus on strenght and a little malus on Stealth. It look like this :
Strenght : 10 + 5 (15)
Stealth : 35 - 5 (30)
during lvl 2 and lvl 3, he become more cautious.
then, thank to my subroutine (who are played all the time), caracteristics are recalculated.
Strenght : 10 - 5 (5)
Stealth : 35 + 5 (40)
Then he reach lvl 3,
He get 20 skill point to spend. And he spend 15 in strengh and 5 in stealth.
So it look like that :
Strenght : 10 + 15 = 25 (then with the malus = 20)
Stealth : 35 + 5 = 40 (then with the bonus = 45)
And for exemple between lvl 3 and 4, he become reckless again.
Then caracterisitcs should look like this :
Strenght : 25 + 5 = 30
Stealth : 40 - 5 = 35
And he stay like this till he lvl up or untill he become cautious again.

So it may sound complicated but it’s not, what is complicated is to find a code which could be place into my subroutine and which will not increase/decrease indefinitly my variables but replay over and over the bonus / malus application.

Thank you :slight_smile:

Ok, so If I understand you correctly, you have a bonus value of 5 that can be applied either towards strength or stealth depended on your dominant trait (recklessness/caution) this value remains passive throughout the game even as you level (I’m assuming the value doesn’t go greater than 5?)

From there it really just be a matter of two *if statements paired with what I suggested in my previous post to check for the dominant value.

*if (recklessness > 50)
*set strength +5
*set stealth -5

*if (recklessness <50)
*set stealth +5
*set strength -5

Since the bonus remains constant and is ultimately dependent on which trait is above 50%, it becomes a simple matter of adding the 5 to the relevant skill and subtracting the 5 from the irrelevant one.

If you want to display something in the inventory screen to let the user know what bonus they’re receiving, you could:

*create bonus “none”

And then add a third line to the earlier code so it looks like:

*if (recklessness > 50)
*set strength +5
*set stealth -5
*set bonus “strength”

So in your stats menu you could have something:

You are receiving a +5 bonus to your ${bonus}

Which to the player would read as:
You are receiving a +5 bonus to your strength

If you want to get fancy and have different message displayed at the beginning of the game before the player has had a chance to affect their stats or if the trait values balance out (at 50%) in which case no bonus could be received, then you could have:

*if (bonus = “none”)
You are not receiving a bonus
*if (bonus != “none”)
You are receiving a +5 bonus to your ${bonus}

I’ve just tested this and it works fine on my end. The only thing is that it doesn’t really deal with a 50% value very well since it can’t tell if you’re coming up from recklessness or from caution, so I’ll need to think about that a little bit.

Thanks to your advices ! Thanks to you guys, i manage to find something not really disgusting.

I think this is working. I mean you can play it over and over indefinitly, it’s not gona to increase or decrease more than 20 points one of the skills.

At least I hope.

Take a look and Tell me what you think :

*line_break
[ BONUS AND MALUS ]___
*line_break
*line_break
*line_break

*if (sarcasm = 50) and (bonus = “bargaining”)
*set bonus “none”
*set bargaining - 10
*set leadership + 10

*if (sarcasm = 50) and (bonus = “leadership”)
*set bonus “none”
*set leadership - 10
*set bargaining + 10

*if (sarcasm > 50) and (bonus = “none”)
*set bonus “bargaining”
*set bargaining + 10
*set leadership - 10

*if (sarcasm < 50) and (bonus = “none”)
*set bonus “leadership”
*set bargaining - 10
*set leadership + 10

*if (sarcasm > 50) and (bonus = “leadership”)
*set bonus “bargaining”
*set leadership - 20
*set bargaining + 20

*if (sarcasm < 50) and (bonus = “bargaining”)
*set bonus “leadership”
*set bargaining - 20
*set leadership + 20

*comment [ for display purpose ]

*if (sarcasm > 50) and (bonus= “bargaining”)
You have a malus to your leadership and a bonus to your bargaining skill.

*if (sarcasm < 50) and (bonus = “leadership”)
You have a malus to your bargaining and a bonus to your leadership skill.

*if (sarcasm = 50)
You are not receiving any bonus or malus.
.
.
.
.
.
end

So if you are a sarcastic guy, you obtain a bonus in your bargaining skill and a malus in your leadership skill. But if you have more sincerity than sarcasm, you receive a bonus in leadership and a malus in bargaining.