Ultimate Noob Coding

It is indeed that easy.

*rand dieroll 1 99
*set dieroll +stat

You could also keep the stats separate while still getting its sum.

*rand dieroll 1 99
You rolled ${dieroll +stat} (dieroll: ${dieroll}, stat: ${stat})
1 Like

Is there a way to get choicescript to select the highest (or lowest) of a number of variables - apart from the obvious way of lots of *if ((x > y) and (x > z)) type bruteforce code

1 Like

CS donā€™t have built-in command to find highest or lowest value from a set of variables. The good thing, at least, is that you can build your own code and use that on multiple places, even on different projects, once you get the subroutine done.

3 Likes

thanks,

another question: Is it possible to modify the text of a choice itself based on a variable?

Example, I have a standard choice

*choice
    *selectable_if (action_count <= 3) #Do something.
        *set action_count + 1
        [text]

is it possible to modify the ā€˜#Do somethingā€™ based on the value of the variable: ā€˜action_countā€™ within the choice text itself?

I am aware I can work around this with something like:

*choice
    *if (action_count <= 3)    
        #Do something.
            *set action_count + 1
            [text]
            *goto somewhere_else
    *if (action_count > 3)
        *selectable_if (action_count <= 3) #You are too tired to do something.
            *finish

but it feels clumsy to have to do it in pairs.

The Multireplace function should be able to handle it. You could do something like this:

*choice
    *selectable_if (action_count <= 3) #@{(action_count <= 3) Do something. | You are too tired to do something.}
        *set action_count + 1
        [text]
2 Likes

hmmmā€¦

like

*label somename
*set result 0

*comment the label "highest" in the "number" 
*comment scene will compare two variables and
*comment store the value (or name if you know how to 
*comment work with pointers) in the variable "result"

*gosub_scene number highest (var_1) (var_2)

*gosub_scene number highest (result) (var_3)

*gosub_scene number highest (result) (var_4)

*gosub_scene number highest (result) (var_5)

*gosub_scene number highest (result) (var_6)

*gosub_scene number highest (result) (var_7)

*gosub_scene number highest (result) (var_8)

*gosub_scene number highest (result) (var_9)

*gosub_scene number highest (result) (var_10)

*gosub_scene number highest (result) (var_11)

The higest value is: ${result}

but
I highly sugest do it in a loop so it would be something like this:

*label somename
*set result var_1
*temp parameter ""
*temp i 1
*temp total 11

*label loop
*comment let the looping begin! (11 variables total)
*comment parameter should work as a pointer to val_x


*if (i < total)
    *set parameter ("var_"&"${i}")
    *gosub_scene number highest (result) ({parameter})
    *set i +1

    *goto loop

The higest value is: ${result}

Ta DAAAAAA!! :smiley:

By the sheer power of coding I command you to return me the Highest value among these variables!

*comment number.txt

*label highest
*param

*if (param_1 > param_2)
    *set result param_1
    *return

*set result param_2

*return

Done!

1 Like

Itā€™d only work if you name your variables exactly ā€œvar_123ā€. What about simple ā€œstrengthā€, ā€œagiā€, ā€œintā€?
Returning the VALUE and the VARNAME itself are two different things.

But yes, as youā€™ve shown, once you gott the system set up, you can use it anywhere you like.

1 Like

Hmm. I guess if you got variables with names like ā€œstrengthā€ or ā€œagilityā€ or ā€œintelligenceā€ you would have to point ā€œvar_numberā€ to them before the loop. And in the loop make it compare the last value

Make sense?

Would using two arrays work?

* comment - In startup file ...
*create varNames[1] "Str"
*create varNames[2] "Agi"
*create varNames[3] "int"
*create varValues[1] 30
*create varValues[2] 50
*create varValues[3] 40
*create mycounter 4

* comment - In Scenes File(s)
*set i 1
*label looper
          *if (i < mycounter)
           The value of ${varNames[i]} is ${varValues[i]}
          *set i +1 
          *goto looper
1 Like

My memory is a bit hazy, but I think thatā€™s not how array declaration works. But you donā€™t even need two arrays:

*create var_1 "str"
*create var_2 "agi"
*create str 0
*create agi 0

*label bigfinder
*set loop 0
*set looplimit 2
*set returnvar ""
*set returnval 0
*label beginloop
*set loop +1
*if {var[loop]} < {var[loop +1]}
   *set returnvar "var[loop+1]"
   *set returnval var[loop+1]
   *goto beginloop
*elseif loop = looplimit
   Your biggest stat is ${{returnvar}} scoring at ${returnval}.
1 Like

If you can remember that it was ever any better, then you werenā€™t doing it right :wink: :stuck_out_tongue:

ā€¦

And youā€™re correct on both counts

I like the array thing though, because its a neat way of keeping stuff together (if only in my head). So the (full) - and now tested! - way of using two arrays (and some non-array variables, just for added fun) to do the same thing and handle where the stats are equal ā€¦

*comment - in the startup file
*create varNames_1 "Str"
*create varNames_2 "Agi"
*create varNames_3 "Int"
*create varValues_1 40
*create varValues_2 10
*create varValues_3 40
*create mycounter 3
*create i 0
*create remi1 1
*create remi2 0
*create remi3 0
*create highest_val_is 0

*comment - Add more remiX - and increase mycounter - if you got more variables



*comment - In Scenes File(s)

*comment set highest value and rememberID to be first value pair (at the moment, they are highest!) ... note NOT i!
*set highest_val_is {varValues[1]}
*set remi1 1

*comment start the loop
*label looper
*set i +1
*if (i < mycounter)
    *if highest_val_is < {varValues[i +1]}
        *comment if new > old add new
        *set highest_val_is varValues[i+1]
        *set remi1 i+1
        *comment Reset any earlier "ties/equals!
        *set remi2 0
        *set remi3 0
        *goto looper
    
    *elseif highest_val_is = {varValues[i +1]}
        *comment - New val = Current Highest - check whether a tie already exists
        *if remi2 !=0
          *comment - we already have a tie for high value, so remember another index/item.  
          *set remi3 i+1
          *goto looper
          *comment -----  NOTE: If you have more than 3 stats, you'd add ELSEIFs here

     
        *comment - no current tie for high value, so remember the index/item
        *set remi2 i +1
        *goto looper        
    *else
    *comment highest value is still old value - do nothing
    *goto looper

*comment --- Print To Screen
*if remi2 = 0
    Your highest value stat is: ${varNames[remi1]} with a value of ${varValues[remi1]}
    *finish
*elseif remi3 = 0
    You have two highest value stats: ${varNames[remi1]} with a value of ${varValues[remi1]} and ${varNames[remi2]} with a value of ${varValues[remi2]}
    *finish
*else
    All three of your stats - ${varNames[remi1]}, ${varNames[remi2]}, ${varNames[remi3]}  - have a value of ${varValues[remi1]}.

So, look, I know ā€¦ but at the moment, itā€™s probably safest if I stay indoors anyway :slight_smile:

Credits:
String Stuff (ā€œfunā€, but didnā€™t use it): Reverse Concatenation - #9 by CJW
Arrays: Initialising arrays (and programming language implementation)

2 Likes

Yes this is Perfect.

I have a question. What is *delay_break and *delay_ending and how to use them Ā“properlyĀ“?

1 Like

Itā€™s for official use, used to timegate the story (especially for non-paid ones).

1 Like

here is my new question - regarding *gosub_scene.

It seems it is not possible to *set temp variables initialised in the ā€˜master sceneā€™ in a scene which has been gone to via gosub_scene. Is this true or am I missing something.

example of what I mean:

*temp big_counting 0
Hi
*gosub_scene event_1
*set big_counting + 2
*return

This spits out a non-existent error variable ā€˜big_countingā€™ in quicktest.

Is there something I am missing here or do variable set in a subroutines not call back to the *temp variables initialized in the ā€˜master sceneā€™. They work fine with *create variables in startup.txt.

It also spits out the ā€˜non-existent variableā€™ when play-testing.

edit: obviously I fixed this by just migrating those variables to the startup scene, but its slightly frustrating as it means I have even more perma-variables

Afaik they donā€™t, the *temp will only work for the current file.

You could pass these to the other *gosub as parameters or change them to *create for global access.

In your case, you could have a global ā€œreturn_resultā€ variable that receives the result of whatever goes on in event_1. And afterwards you *set big_counting return_result.

1 Like

*gosub_scene shouldnā€™t produce such error while *goto_scene does. Simple *gosub and *goto also do produce the errors. Hmm.

1 Like

Thank you both.

If it is like this, it seems like a limitation of choicescript itself since it seems to mean you canā€™t do any temporary variable modification within a subroutine. Iā€™ve just ported those particular variables across to startup.txt as a workaround. I will just have to bear it in mind.

Maybe Iā€™ll finally get to use that *delete command after all :upside_down_face:

Iā€™d still advise against using *delete in almost any case, though. If anything, that command would serve nothing but adding a layer of confusion, ā€œhave I deleted this variable? Where did I delete it?ā€

CS isnā€™t a mandating engine as Unreal, Unity, or even RPGMaker. You can get away with hundreds of active variables without the need to clean them up every scene and so to clear up the phone/PCā€™s memory.

1 Like

ill make sure to include one, just one, *delete just for you szaal :grin:

I mean sometimes its just a good practice to delete stuff youā€™re not gonna use anymore. At work we do some deletions because we want to avoid memory leaks and such, not saying they might happen here but sometimes you get used to making sure you close stuff you wonā€™t use in case something goes wrong and they are never deleted automatically.

Yesterday I was about to do the same thing, add *delete on *temps that were just part of some specific *gosubs, I even did a *gosub just to *delete n number of temp variables passed as params. However I decided to just move them all to stuff I would use on *gosub_scene and ran to the same problem @Alice-chan had with the *temps not being modifiable on other *gosubs we got to.

1 Like