Arrays and Dynamic Arrays?

Sad Backstory

My current WIP aims to generate a new experience every time it’s played while maintaining quality, interest and, most importantly, entertainment value. I hope to do this by randomly generating the experience, characters, plots, world, etc. and as such, arrays have become a useful tool.

In CS, a simple array can be determined. This example generates and assigns random names to array elements (Character IDs) by incrementing the Array Index and repeating the process until a predetermined amount x is reached. For this setup, the *create CharID# needs to range from CharID1 to CharIDx, and needs to be established from the outset.

*label top
*set gennum + 1
*set gentext "charid" & gennum
*gosub random_name
*set {gentext} name
*if gennum = x
    *goto top

Clearly, this is tedious, but necessary.

It would be optimal if these arrays could be dynamic, where x is not pre-determined, but determined at the beginning of the playthrough itself, where CS would create as many array elements as needed.

So, instead of declaring at startup:

*create charid1 ""
*create charid2 ""
*create charid3 ""
*create charidx ""

We would like to say, "This playthrough is going to have 45 characters, so set X to 45 and create 45 distinct variables.

Are there any brilliant insights I might have missed, or does CS simply not allow for dynamic arrays to be created?

The ghostly apparitions we know as *temp may appear in the midst of all chaos, creating new and unique variables, but these ephemeral beings, as is their nature, seem to dissipate into the ether once we shift focus from them, forgotten by the CS engine, forever. (And also, while we could *set {generalvariabletext}, we certainly can’t *temp {generalvariabletext})


Perhaps I have the wrong impression of what you’re trying to accomplish, but it seems to me that you could generate necessary information manually (and randomly) simply by writing a subscript that goes at the beginning of a CS story before the story proper starts. Thus, variable elements would be generated at the beginning of a playthrough and stay consistent within that playthrough, but would be re-generated each time a new game is started.


I could be wrong and probably am, but if you are trying to make an array out of a text string (which is in of itself an array actually) you’ll need to store the index of where the next entry starts or pad them all the same length.

Its okay to tell me I am smoking crack and go back to writing instead. :slight_smile:


I mean, so long as the array can’t contain arbitrary characters (i.e. it isn’t tied to input text), or you can scrub those characters, you can fake your way through it with subroutines, searching by the character, and a single variable delineating different entries with a special character (e.g. a pipe |).

i.e. Something like this:

*create array "first|second"
*temp array_entry_wanted 2
*gosub print array_entry_wanted

*label print
*params i
*temp n 1
*temp a 1
*temp print ""
*label loop
*if a = i
  *if n > length(array)
  *if (array#n) = "|"
  *set print print & (array#n)
*if (array#n) = "|"
  *set a + 1
*set n + 1
*goto loop

My apologies, I think I’ve gone and used completely the wrong terminology. Thank you all for the input, but I’ll have to do some more homework on the matter and come back when I’m better versed in this topic myself. I’m out of my (array) element here.

@RETowers I’m working to understand this. It seems like it very likely could be useful, but I’ll have to do my homework on it to learn how to interpret this.

@MirandaRose You are absolutely correct, but feel free to hit the crack and continue writing. Sounds like a splendid time.

@Shawn_Patrick_Reed Yes, all the random things happen in startup and stay that way for the rest of the game.

1 Like

Are you trying to use this code to create random names for everyone?
Or, is it something else?

I’m trying to use the code to create everyone first.

What all does a single person have, then?
What pieces of information would go into the array for one person?

1 Like

Of note:

If you’re trying to write some script that randomly generates a character name… I’ve already done that. I could share my random name generating script with you, if you like. If you’re trying to create other information for a bunch of different characters, it gets a bit more… … tedious. I wouldn’t say tricky, just- you need to cover randomizing all the variables for all of the characters individually. Even if you write some script to shortcut (slightly) randomizing the variable for one character, then going to the next, and the next, and so on, in one step. There need to be variables created that ‘belong’ to the characters individually. Eg-
*create char1name “unknown”
*create char2name “unknown”
*create char3name “unknown”
*create strchar1 0
*create strchar2 0
*create strchar3 0
*create dexchar1 0
*create dexchar2 0
*create dexchar3 0

and so forth… And then these variables would be randomized and essentially define char1, or char2, or char3. Including what that character’s ‘name’ is. But referenced by code, they’d be referenced as something like char1, char2, char3, rather than the randomly generated name. I’ve done some work on this sort of thing, but in the interest of creating one randomized NPC, not a whole host of them. Still, I could dig up some of the script if you’d like.


I have this system in place and a fairly cool name generator (I think).

I’d like to, instead of having to iterate the process of
*create char1
*create char2
*create char3

in startup manually for an unspecified amount of characters, I’d like the script to say "We need 7 characters, so we’re creating seven instances of *create char(x), where x incrementally increases from 1 to 7.

Or 1 to 80, if need be.

I’d love to look at your name generator though.

1 Like

There’s, to my knowledge, unfortunately no way around needing to *create variables in startup, though not all variables need to be used, necessarily. So for example, if you had a maximum of 80 (somethings) you’d need to *create variables for all 80. Though you wouldn’t need to use all of them in the game; you could have a script that chooses how many are used.

Unfortunately, I don’t think what you’re seeking (being able to randomly define *create commands) is possible with CS. You can define a number of instances for characters, but you need to have the maximum possible number and their variables set with *create for those variables or it will give an error trying to go over. If you’ve prepped variables for 80 characters, but only use 7… there’s no problem. If you’ve prepped for 80 and the game tries making 81, there will be problems with that 81st in the code.

Also, I’ll dig up the name generator.


You could just put all of the names into one “superstring” and then have some code, similar to what Rachel suggested, pick out the names based on where they are.

In order to do that, you could use a special character, as she suggested, to delineate one name from another.

EDIT: Or, you could probably do something similar with multireplace. Not sure. :wink:


That is an incredible idea that certainly merits exploring. Thank you.


Yea, this is absolutely not the easy path, this is just me throwing out an alternative that fit the parameters that for an otherwise not supported usage. (i.e., this is the same mentality that prompted me to accidentally start working on a text parser.)


This is absolutely the most perfect thing in the world for making exactly this more inclusive and dynamic:

You’re a real hero.

1 Like

The string method will work but it probably won’t scale very well in terms of performance. You’re best bet is to actually just define a known MAX number of array elements. I.e. create 500 if you know that will never be surpassed. It doesn’t matter if some go unused. It is a pain to write all that out though (this is why we need *create_array).


This does seem to be the only *choice, but I’ll tell ya, Excel autofill and concatenate has helped a lot with minimising the tedium.

1 Like

The tedium certainly is a thing, but it can be therapeudically meditative, sometimes. One thing I’ve experimented around with in the time sinse this was a thing has been a sort-of… substate saving one variable with another using
Characters: You can extract the characters (letters/numerals) out of a variable, like this:

  *temp word "xyzzy"
  *temp first_letter word#1
  *temp second_letter word#2
  The first letter of the word "${word}" is ${first_letter} and the second letter of the word is ${second_letter}.

So this lets me create an ‘array’ of sorts (really not, more like a cluster of information) that is just a cluster of letters or numbers. Worth noting- a number cannot be more than 22 characters/numbers of length. Not sure yet on letters. Then, later code can break down the information ie
*if word#1 = “a”
*set npceyecolor “green”
*if word#1 = “b”
*set npceyecolor “blue”

or what have you…
*if word#2 = “a”
*set npcweapon “sword”
*if word#2 = “b”
*set npcweapon “axe”

Then later have some script like “!{npcname} swings their {npcweapon} at the {enemytype}, {npceyecolor} eyes flashing in anger.” (with the dollar signs, that don’t show up here for some reason.)
But rather than word, it could be an NPC’s name for the variable, which is itself *set in the code, creating a cluster of information that defines ‘something’ you want a cluster of information to define, such as a character. Of course, the information still needs to be defined via coding, in the script, but as a manner of storing which of the information this should ‘be’, it works rather decently to condense that down into a ‘saved’ sort of state. (which isn’t really saved, per-se, just one manner of clustering it into a certain grouping). Better if ‘word’ is *create at the beginning, rather than *temp, but it depends. Later on, coding like *set word#2 “s” can be done, to change the information in the information cluster, so long as it can be broken down later… but using *gosub and *return for points where information stored in this way is needed can be done. It’s just… kinda as you say, tedious… but there are some ways to actually do things you’re indicating, combining the information clustering with randomization or *set. Sorry if any of this is confusing; I hope it’s not, and I’m really only posting because of the recent post and likes.


No. That makes perfect sense, and seems like it will be very useful.

So instead of having, e.g. five different characteristics each for a hundred characters, I can have each character’s data stored in a single string variable and then just extract it when I need it.

If I understand you correctly, this is wonderful news. How does this do performance-wise?


Though it really depends on your usage. If you’re frequently looping over and updating numerous values, you are very likely going to notice. If you’ve just got a handful of strings you’re reading characters from on occasion then you’ll be fine. Have a play around, see what works for you.

1 Like