Setting Variable Variables?


Ok bear with me here. Let’s say you have a set of variables pertaining to a number of individuals, staff for instance.

*create st1_cooking 0
*create st2_cooking 2
*create st3_cooking 1
*create def_cooking 10

Is there a way, to set a variable changing only the enumeration/part of the designation?

*set st(semi random number)_cooking def_cooking (Mod Edit: I assume this is what you meant?).

Also to do the same with strings and booleans, and to do so in a fashion that doesn’t require a ton of code. If not, I want that, so bad. Would save sooo much work.

Oh and is there a way to add quotes inside a string?


Yes, thank you, unknown mod.


Oh and here:
First thing might be possible with *setref, but it would take me a while to puzzle it out.

As for your second question:
Quotes: You can put quotes in your text by using backslashes, like this:
*set joke “she said it was “ironic”!”
If you write ${joke}, you’ll get:
she said it was “ironic”!


Oh right backslashes, I’ve read that before somewhere, thanks! That solves one problem at least. =)
I was hoping *setref would work, but in the example on the wiki, they only used it for labels, and those examples also seems like a close but no cigar kind of thing. There might be something there, I can almost taste it, but it looks like, on first glance, that there would be just as much code as just a set of *ifs, if not more… You would have to determine the *setrefs with *ifs anyways.

*if (st1)
…*setref “st1_cooking” 10

And that would be fine if it was just the one, but I’m dealing with over 53+ variables per individual here.


With a series of variables, you can almost set up arrays. I think this is what may work.

Let’s say you have 50 variables, st1 through st50. You want to set them all to 2.

*temp count 0
*temp hold “”

*label setarray
*if count = 50
*goto endloop
*set count +1
*set hold “st”&count
*setref hold 2
*goto setarray

*label endloop
*comment this is where you go after the loop.


As JimD says^
Here’s a working example of that sort of thing, in an inventory context:


Would that work on numerals and booleans as well? Does the number have to be at the end or could you bake it in, like in my example? {“st”&n"_cooking"} ? Having real arrays would be the simplest though, is there a reason why we don’t have them?


The most likely reasons we don’t have real arrays is because a. They might be a pain to implement and b. The demand/requirement for them isn’t large enough.

You can refer to variables like that via name/string of course, but it wouldn’t work as an array, for obvious reasons (though you can emulate objects and their properties).


Well we do have support for logarithms…Can’t really imagine a use for those. And I’ll definately raise my hand and say I would very much like arrays if at all possible. And I would think many programmers would join me in that wish.

Would you perhaps care to make an example, please? I assume it has something to do with an alternate way of calling on these fake arrays? I only half-half know what I’m doing yet. =P



Most “programmers” would probably rather just use raw JS over CS, so that argument won’t get you far. Part of the appeal of Choicescript is how simple it is for people who haven’t programmed before (it’s designed for authors, not coders etc).

That’s not to say I wouldn’t personally like to see arrays, objects and many other things implemented into CS though (of course I would) but I can understand why they might not be.

What is it you want an example of, object emulation?


Well one could argue that the whole controversy with JS and the whole ‘could affect your chances of being published thing’, would be an incentive to see more official and supported tools being implemented. Naturally most people may not end up using it, but better a well stacked toolbox than one with just a hammer and a screwdriver, is how I see it. Those who know to use it will find joy in it, and arrays could definately ease things even for a hobby coder. I didn’t even know the term array until yesterday but I knew what it was I wanted to be able to do.

If these objects can help me do what I’m after, then yes please. Which is to set a large number of variables of all types, using as little code as possible, and to do so in an automated fashion.
So instead of doing this;

  *set st2_name h1_name
  *set st2_surname h1_surname
  *set st2_fullname h1_fullname
  *set st2_quote h1_quote
  *set st2_nickname h1_nickname
  *set st2_age h1_age
  *set st2_cooking h1_cooking
  *set st2_cleaning h1_cleaning
  *set st2_serving h1_serving
  *set st2_managing h1_managing
  *set st2_bouncing h1_bouncing
  *set st2_stabling h1_stabling
  *set st2_entertaining h1_entertaining
  *set st2_gossiping h1_gossiping
  *set st2_farming h1_farming
  *set st2_fishing h1_fishing
  *set st2_brewing h1_brewing
  *set st2_nursing h1_nursing
  *set st2_trait1_numcode h1_trait1_numcode
  *set st2_trait1_name h1_trait1_name
  *set st2_trait1_desc h1_trait1_desc
  *set st2_trait2_numcode h1_trait2_numcode
  *set st2_trait2_name h1_trait2_name
  *set st2_trait2_desc h1_trait2_desc
  *set st2_trait3_numcode h1_trait3_numcode
  *set st2_trait3_name h1_trait3_name
  *set st2_trait3_desc h1_trait3_desc
  *set st2_heshe h1_heshe
  *set st2_hisher h1_hisher
  *set st2_himher h1_himher
  *set st2_malefemale h1_malefemale
  *set st2_fathermother h1_fathermother
  *set st2_husbandwife h1_husbandwife
  *set st2_fiancefiancee h1_fiancefiancee
  *set st2_manwoman h1_manwoman
  *set st2_menwomen h1_menwomen
  *set st2_boygirl h1_boygirl
  *set st2_isfemale h1_isfemale
  *set st2_pregnant h1_pregnant
  *set st2_pregnancy h1_pregnancy
  *set st2_dis h1_dis
  *set st2_dis_desc h1_dis_desc
  *set st2_dis_counter h1_dis_counter
  *set st2_dis_deadly h1_dis_deadly
  *set st2_job h1_job
  *set st2_respect h1_respect
  *set st2_energy h1_energy
  *set st2_wage_g h1_wage_g
  *set st2_wage_s h1_wage_s
  *set st2_wage_c h1_wage_c
  *set st2_job h1_job
  *set st2 true
  *set h1 false

One can change the numbers so one doesn’t have to set each set of variables, e.g. st1 -st30, and h1-h8, individually. You can see why this would be a right slog to do the other way. And yes, there are all three kinds of variables in there, which is why I hesitate to use that fake array method.


Oh, no - for that, the array method will work just fine.
The data type doesn’t matter, I thought you were asking if you could loop through variables without a number (index) in their name, and to that the answer would have been no.

Yeah just see the above example and change strings to booleans/numbers or whatever you like as needed!
: )


I have been experimenting, but what I find is that it can only do about half of what I want it to. You can randomize or automize what you put into a SET variable, but you cannot randomize or automize a set variable?

*create cooking_st1 0
*create cooking_st2 10

*temp n 0
*rand n 1 2
*temp cooking
*set cooking {“cooking_st”&n}
*set cooking_st1 + cooking
St1: {cooking_st1} *line_break St2: {cooking_st2}

What’s worse, the number has to be at the end of the variable for it to even work at all. Which means I would have about a gazillion lines of code to change just to rename the variables.

I could be wrong though, I am feeling rather sluggish atm. Or am I right in that you can’t do the array thing with the variable that you are setting, only with the value you are setting it to?


I’m not quite sure what you mean there, but…

*temp new_boolean_value false
*temp target_var "my_object_property_"&n
*setref target_var new_boolean_value

Does that help at all?

As for the number needing to be at the end, that’s definitely not true:

*set variable {("my_object_"&n)&"_my_property"}


Aaaah! That was like staring at a mosaic or something, suddenly it just made sense. That’s great! And I don’t have to change the variables’ names either! Awesome, thank you! =) Now that I think about it, that was what JimD was talking about as well, d’oh. I’m slow on the uptake sometimes. Thanks y’all.