Initialising arrays (and programming language implementation)

Hi.

I’ve been experimenting with how to simulating arrays. Is my understanding correct that you have to “manually” define each “array” index?

e.g.
*temp MyArray_1
*temp MyArray_2
*temp MyArray_3
*temp MyArray_4

Also, has someone else been insane enough to try and implement a (toy/esoteric) programming language in Choicescript and I’ve just missed it?

Andrew

Yes, currently you have to define “arrays” by hand, but can reference them as you normally would, i.e. myarray[1].

Do you mean like a scripting language ontop of Choicescript? Which is already on top of JavaScript. Not yet…

2 Likes

Excuse for my noobness but what exactly is an “Array” for?

An array is used to group together related objects, so that you can refer to them by a number, rather than separate names.

For example, if you wanted to hold a list of birds, instead of having to think up as many variable names as there are birds (the amount of which you may not know when you are writing your program), you can have an array with a whole bunch of birds and refer to them by number (array index), including a number in another variable.

Instead of
*create first_bird “sparrow”
*create second_bird “robin”
*create third_bird “finch”
you could do
*create_array birds 3
*set birds[1] “sparrow”
*set birds[2] “robin”
*set birds[3] “finch”

It may seem in this example that arrays are pointless, but in more sophisticated programs they are very useful, especially when you use a variable for the index like
*input_text birds[current_bird]

Feel free to ask me any more questions or for clarification.

1 Like

Well I may be a regular on this forum but I have no idea how actual code works. @amca would you mind ellaborating more on how an array is useful? Also it seems like it would be a lot of work to keep track of which bird is bird[1], bird[2], etc. Is it?

There are two main types of situation where you would use such an array.

The first situation is where you don’t care what order they are in, you just want them all grouped together so they are easier to manipulate.

The second situation is where the index (number) actually means something. For instance they could be in order of favourite bird. So the sparrow would be the favourite bird, the robin the second favourite bird and the finch the least favourite bird.

3 Likes

From a story-writing perspective, arrays are especially useful for managing a character’s inventory.

3 Likes

Yep. That’s the sort of array where you don’t care where something is in an array, just whether it is or not.

Example of use:
http://pastebin.com/1Lx3CNKs

Though this is actually from before the addition of the [i] syntax, so it’d be even easier now.

3 Likes

You can actually use looping code to declare *temp arrays, but for *create arrays, you’d have to define them manually. The reason one works, but the other doesn’t is due to the fact that all your *create lines must be in the same block of code, and this breaks when you try to do anything else.

1 Like

Thanks, I’ll try that. I think I tried it with *create arrays, but it didn’t work, so I started a different approach.

Are you sure? Last I checked you couldn’t create *temp or *create variable names dynamically (i.e. use another variable to help define their name).

1 Like

Oh!

I fear i already tried to code something like this!

But i was using a string as a vector for my system since choicescript can “read” word’s lenght and “write” by silabic position.

Pretty sure I remember trying out loops with *temp. It’s been quite a while though, so I may either be mistaken, and I was just theorizing it could be done, or it’s been changed. I’ma have to test this out again either way.

Edit: Yup. I was wrong. My bad :stuck_out_tongue:

2 Likes

Thanks for double checking for me. I hadnt gotten around to double checking it myself yet.

I’ve been going with programmatically generating the array initialisation code in Choicescript and then copy+pasting that code from the resulting web page into the text editor.

Honestly, as a programmer myself, I’ve been seriously considering writing a converting program to turn more program like code into CS coding. At the very least, I’ll probably write up a few helper programs to do things like initializing arrays, making copies of variables for checkpoints, repetitive if/elseif/else codes, etc…

4 Likes