Coding Questions


#1

Hi everybody,
I’m new to this forum and Choicescript in general. I’ve played pretty much every “Choice” game I could get my hands on since I discovered Choice of the Dragon and thought it would be pretty cool to try my hand at writing one myself. Everything is going pretty well so far, and I’m making good progress in my character creation scene, I just have a couple of quick questions though.

First, how do I go about pass on variable off to another so that they share the same info, and do I do this in mygame.js or in my scene? Would it be something like this?
*choice
#red
*set hair “red”
*hair = other_guys_hair

Second, can I make a text variable? And, if so, can I nest a variable into another variable. For example, I was trying to write a scene where the player chooses a certain hair color and one of the characters comments on how the players hair is similar to another characters hair color, but I think I was having problems because I was trying to nest a gender specific variable in my text variable…if that makes any sense. I was trying to code something like this:

*temp guys_hair “${Gendersubjective} has the same hair color as you”
*if hair = guys_hair
*set guys_hair

${guys_hair}, blah blah blah

Sorry if this code if laughably amateurish but I’m just kind of learning it as I go.

Thanks


#2

You can definitely set a variable to be equal to whatever the current value of another variable is, but as far as I know you can’t tie them together. If you did something like this:

*create hair
*create other_guys_hair
*set hair “red”
*set other_guys_hair hair
*set hair “plaid”

After that segment of code runs, hair will contain the value “plaid”, and other_guys_hair will contain the value “red”. By the way, how I “*set other_guys_hair hair” is how you set a variable to hold the value of another.

As for that second part with setting variables to combinations of strings and variable values… that’s harder than you’d think. You can’t just use ${variable} to put the variable’s value into the string - you’ve got to end the string, and then put a “&” between the string and the variable. Example:

*create hair
*create stringadditionisannoying
*set hair “red”
*set stringadditionisannoying "I wish it was easier to say that your hair is " & hair

Don’t forget to leave a space at the end of the string so it doesn’t say “hair isred”. This doesn’t look bad, but it gets much worse when you have more than one “segment” to add to the thing. Choicescript only combines pairs of segments. Its way around that is combining a pair of segments, calling it a new segment, and then pairing that off with another segment. It figures out segments using the “(” and “)” guys. So if you wanted the last code I showed you to end in a period, you’d need to rewrite it as something like:

*create hair
*create stringadditionisannoying
*set hair “red”
*set stringadditionisannoying ("I wish it was easier to say that your hair is " & hair) & “.”

Gets so much worse when you have more than just one variable to include.

Anyway, back to your question. You cannot make a variable’s value depend on another variable’s, but you can make a pseudo-nested variable. Sort of. Just update your variable that has another “nested” in it every single time you call that variable. Alternatively update it whenever you change the value of the variable nested within it - whichever is easier depends on which one involves fewer updates. To make life easier and prove to the world that you are a fancy-pants, you can use the magical *gosub command when doing this. That command lets you make mini-programs within your program that your program can call whenever. So if you don’t want to retype something like below each and every time you update something big…

*set scenery ((((“Wow, look at all the " & wild_animals) & (”! You feel so with nature here, with your " & relationship_level)) & ((" " & lover_name)) & ((", the most popular " & lover_gender)) & ((" at " & school_name) & “!”)) ))

…then you don’t have to. Don’t yell at me if the I messed up the parentheses - I don’t really care. Anyway here’s how you do *gosub:

How is your date going?
*choice
#My date with Jessica is terrible
*set stuff (not important to include that code in demonstration)
*gosub update_scenery
*goto lovers_death
#My romantic outing with my husband Jamal (who goes to school with me) is great
*set stuff
*gosub update_scenery
*goto lovers_death
#I’m having a good time with my friend Jamie
*set stuff
*gosub update_scenery
*goto lovers_death

*label update_scenery
*set scenery ((((“Wow, look at all the " & wild_animals) & (”! You feel so with nature here, with your " & relationship_level)) & ((" " & lover_name)) & ((", the most popular " & lover_gender)) & ((" at " & school_name) & “!”)) ))
*return

You set up the subroutine (mini-program) like it is a label, but add a *return to the end of it so you go back to wherever you called the *gosub. It doesn’t only make it easier to read - it also makes it easier to debug and edit. If there’s a typo or you want to add or remove something, you don’t have to go looking around and find every single instance of *set scenery. You do, however, have to put a copy of the subroutine in every single text document/scene that you use it in. I usually put my subroutines at the end of my programs to make it all neat and stuff.

Random note - that whole pairing with parentheses rule thing I mentioned earlier… you need that for big if statements too, like *if ((hair = “red” and hair2 = “blue”) and hair3 = green)

Hope this helped, wasn’t waaay too long, and didn’t miss anything. Good luck with Choicescript (you’ll get the hang of it, don’t worry), and I look forward to seeing what you make.

Oh yeah, and before I forget, some nice tutorial stuff:

You’ve probably already read this - http://www.choiceofgames.com/make-your-own-games/choicescript-intro/

But have you read this? - http://www.choiceofgames.com/make-your-own-games/choicescript-advanced/