Initialising arrays (and programming language implementation)


#1

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


#2

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…


#3

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


#4

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.


#5

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?


#6

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.


#7

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


#8

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


#9

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.


#10

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.


#11

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


#12

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).


#13

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.


#14

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:


#15

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.


#16

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…