Defining an array of fixed values

Started by TerranRich, Tue 20/01/2009 05:17:55

Previous topic - Next topic

TerranRich

Pretty basic. How would one define an array as a set of specific values... something like "int values[5] = {1, 2, 3, 4, 5};". How would that be coded? I tried searching and came up with nothing relevant. Thanks!
Status: Trying to come up with some ideas...

Gilbert

I think, unfortunately, assignment for array elements is not possible during declaration.

Either initialise the array element by element or in a while loop if there is a certain pattern (when it is needed, in game_start() maybe).

Other way round (especially when you need to initialise a lareg array to some set values) is to read from an external file and set the elements one by one by using the data in the file.

Ryan Timothy B

Are you asking how
values[1]=1
and
values[2]=2
and so on?

I would do it like this:
Code: ags

while (i<5) {
i++;
value[i]=i;
}


If that isn't what you're asking, I'm afraid I don't understand your question.

TerranRich

For example, in JavaScript, you can set an array as such:

Code: ags

var names = new Array("Alice", "Bob", "Carol", "Daria", "Eric");


This sets names to an array of 5 elements, and sets the values of each element in that array at once.

I was asking if there was a way to do it in AGS, but apparently there isn't.

More specifically, I have a list of values I want to assign to an array for faces of cards in a card game... something like:

Code: ags

String array[13] = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"];


See what I mean?
Status: Trying to come up with some ideas...

Lt. Smash

unfortunately this is not possible in AGS.
If its a very huge array, I would suggest using an external file.
Otherwise you have to
Code: ags

arr[0] = "Alice";
arr[1] = "Bob";
arr[2] = "Carol";
arr[3] = "Daria";
arr[4] = "Eric";
...

You could try and ask in the wishlist (or start a new thread) for an implementation of this in the next AGS version.

Gilbert

In fact, I found the original (global and room) messages (which, seems that everybodies underused them) in old versions VERY useful for this kind of purposes.

For example, in my game, I used them to hold game messages (like warning messages, hints, etc.) and for array declarations. The good thing about them is that the editor provides an interface for you to enter the text and you don't need to use an external file or type them all out into your script.

Unfortunately, since the rewrite of the Editor introduced in V3, it seems that the message system is far less flexible (I cannot even find how to write global messages, if it is still even possible), say I can only add room messages one by one following thegiven numbering. In the past, I could for example, allocate messages 10-20 as hints, and messages 100-120 as warning messages, so I can easily expand each of the lists if required.

TerranRich

#6
I guess what I could do is use another programming language (outside of AGS) to generate the code to initialize the array values, then copy and paste it into the AGS editor. You like that idea? Clever, no? ;)

For those who are curious, here is how I am doing it. First off, I have a struct of a type OhnoCard with the following attributes: String color, String faceValue, bool isDealt, int spriteNum. The colors are "P" (purple), "A" (aqua), "O" (orange), "G" (green), and "W" (wild card). The faceValues are "0" through "9", "R" (Reverse), "J" (Jump), "P" (Pick 2), "W" (Wild), and "P4" (Wild Pick 4).

1. Use Excel to come up with a spreadsheet, each column corresponding to a struct variable. Each row will be a card. I will then fill it out as such:

P 0 false 1
P 0 false 1
P 1 false 2
P 1 false 2
P 2 false 3
...
O R false 39
O J false 40
O P false 41
(etc.)

2. Save the spreadsheet as a CSV file.

3. Use your favorite programming language to read the CSV file, parse it, and generate values such as:

card[0].color = "P";
card[0].faceValue = "0";
card[0].isDealt = false;
card[0].spriteNum = 1;
card[1].color = "P";
card[1].faceValue = "0";
card[1].isDealt = false;
card[1].spriteNum = 1;
card[2].color = "P";
card[2].faceValue = "1";
card[2].isDealt = false;
card[2].spriteNum = 2;
(etc.)

(Keep in mind that there are two of each of the numerical cards in a standard Uno deck... dunno about the action cards such as Reverse, Skip, etc.)

4. Copy and paste the AGS code into the appropriate place.

Hopefully this helps anybody else who wishes to initialize a large array with external data, but do not want that external data to be available to the player, and even editable.
Status: Trying to come up with some ideas...

Khris

Or:
Code: ags
String array[13];

void init_array() {
  String s = "A23456789XJQK";
  int i;
  while(i<13) {
    array[i] = s.Chars[i];
    i++;
  }
  array[9] = "10";
}

TerranRich

#8
Ah-hah! Why hadn't I thought of that! Very brillant idea, KhrisMUC. Good thinking.

I may do it that way instead then. ;)

EDIT:
On second thought... it might just be easier to do it the CSV/parse/copy/paste way instead. :)
Status: Trying to come up with some ideas...

SMF spam blocked by CleanTalk