Is there any code like switch(case_var)?

may i ask, i’m new with these, but i wanna know if there is any code like these in choice script?

switch (case_var): case 1: Boy, kill yourself break; case2: Girl, GO GET EM! break;
or something

to shorten the goto and if or something like that?

if there is one, how do you implement it on the script?

do i do this?

*create case_var ""


*switch (case_var): case 1: Boy, kill yourself ; case2: Girl, GO GET EM! break

*if (case 1)
death
*go to a
*else
 running away
*goto b

*finish

or what?
thanks

I believe you’re referring to C++ switch case:?

There’s no equivalent command in CS afaik. You have to do the old way using

*if (condition1)
   asdf
   *goto
*elseif (condition2)
   fdsa
   *goto
*else
   and so on
   *goto

However, if you “declare” the variable

*create implicit_control_flow true

at the top of your *create list, you can cut out the *gotos. The text would flow from top to bottom, just like usual.

1 Like

how exactly can i do that??

*create implicit_control_flow true
*create name ""
*create condition1
*create condition2
etc..


*if (condition1)
   asdf
   *goto
*elseif (condition2)
   fdsa
   *goto
*else
   and so on
   *goto

that???
but what thise [*create implicit_control_flow true] do?

This is if you include the implicit control

*create implicit_control_flow true

*if (condition1)
   asdf
*elseif (condition2)
   fdsa
*else
   and so on

Without the implicit control, you have to write it just like on my first reply.


On another note, the conditions can be something such as

  • money > 1000
  • buddy = false
  • character = "dead"

This is the documentation for *create implicit_control_flow

1 Like

The alternative is this:

*create condition 0

Etc
*if condition =1
   Text
   *goto x
*elseif condition =2
   Text
   *goto y

Or

*if condition =1
   Text
*if condition =2
   Text
text 2

If it is meant to go to the same piece of text.

Choicescript is a coding language that does not require ICF as other languages might do.

Fun alternative using references and multi-replace:

*create index 0
*create branch ""

*rand index 1 3
*set branch "@{index label_a|label_b|label_c}"
*goto {branch}

*label label_a

Option A

*finish

*label label_b

Option B

*finish

*label label_c

Option C

*finish
5 Likes

Oh, my.
You can do that?


Yes!
What?!
They can’t do that before!

1 Like

Yup. Pretty sure they added all that fun reference stuff the same time they added pseudo-array support.

Edit: Yup…
https://forum.choiceofgames.com/t/new-choicescript-features-for-programmers/8423

1 Like

No. I mean, I remember playing around with it and got an error: “can’t use variable in *goto”, or something. At that time, I thought indirect label is no longer supported.

But it appears still does supported.
Wonder what happened last time.


Anyway, the possibilities open. :open_hands:t4:

You didn’t by accident put a $ in front of the {}? By reflex maybe?

oh i see i’ll try it then, see if it works

thanks guys

what is rand for? is it random?
and why using 1 3?
because there is 3 label for the branch?

and it said to go to {branch}? i test it and got random option… not the specific option

*create index 0
*create branch ""

man i'm hungry…
*fake_choice
	#Eat
		*set index 1
	#Maybe later...
		*set index 2
	#i guess i'll buy something along the way
		*set index 3

*rand index 1 3
*set branch "@{index label_a|label_b|label_c}"
*goto {branch}

*label label_a

Option A

*finish

*label label_b

Option B

*finish

*label label_c

Option C

*finish

this is not how it work right? because i try it and choose the same ans but i get random index because the index already set to random… right?

btw the *if condition is working perfectly (without the implict control flow)

if i used *create implicit_control_flow

man i'm hungry…
*fake_choice
	#Eat
		*set condition1
	#Maybe later...
		*set condition2
	#i guess i'll buy something along the way
		*set condition3

*if (condition1)
   i full now
*elseif (condition2)
   i'm starving
*else
  i have to eat these soon

they said "non-existent variable ‘condition 1’

*rand
Randomizes a number variable.
The 1 3 behind it tells the code what kind of die it shall roll, so to speak.
So 1 3 means a 3 sided die with 1 2 3
30 40 would mean 30 31 32 etc to 40.

If you use a choice you don’t need random.
(Ill add an example once im not on mobile)

so if i use *rand 30 i will use the 30 sided dice? okay

okay thanks

if you use

*rand 30 40

the dice will be ten-sided, with the numbers between 30 and 40

*rand 1 30

would go from 1 to 30

as for your bit before:

if you want the bit to be random:

*create index 0
*create branch
*rand index 1 3
*set branch "@{index label_a|label_b|label_c}" 

Man, I'm hungry. 
*goto {branch}

*label label_a
I'll eat something
*page_break
*goto next

*label label_b
I'll eat something later
*page_break
*goto next

etc

if you want to let the player pick, you don’t need to the random bit:

Man I'm hungry.

*choice
   #Eat something
      *goto label_a
   #Eat something later
      *goto label_b
etc

and if it’s just a bit of text that then continues to the same bit:

Man I'm hungry

*fake_choice
   #Eat something
      You eat something.
   #Eat something later
      You decide to eat later.
   #Buy something on the way
      You know you'll need to buy something on the way.
Then you leave.

The above will result in the text going, for example

You eat something. Then you leave

if one picked the first option.

1 Like

ah i see ok thanks

If you only need a variable in one scene instead of the whole game, you can also use
*temp
on top of the file instead of *create in the startup

In case you didn’t know, CS has a wiki (although some parts are outdated for a while).

oh yeah i did read it. i just want to know if there any switch case_var XD i’m learning there too

Oh, and another one.

The wiki didn’t cover some of the additional info contained there, IIRC.