GLBasic forum

Main forum => GLBasic - en => Topic started by: aroldo on 2011-Sep-11

Title: Array of constants and objects
Post by: aroldo on 2011-Sep-11
How can I implement an array of constants like this one in Javascritpt:

this.GAMESTATE ={TEST1:0,TEST2:1,TEST3:2,TITLE:3,DEMO:4,START:5,
                  PLAYING:6,EXPLODING:7,ENDLEVEL:8,GAMEOVER:9
};

And and Array of objects like this on in Javascript;

this.stars = [
         new this.star(), new this.star(), new this.star(), new this.star(), new this.star(), new this.star()
      ];
Title: Re: Array of constants and objects
Post by: ampos on 2011-Sep-12
DIMDATA array$[],"Value 1","Value 2",...
Title: Re: Array of constants and objects
Post by: mentalthink on 2011-Sep-12
Or something more large, but works equal:

array$[0]="Value 1"
array$[1]="Value 2"
...
...
...
array$[8]="Game Over"

Title: Re: Array of constants and objects
Post by: backslider on 2011-Sep-12
Quote from: aroldo on 2011-Sep-11
And and Array of objects like this on in Javascript;

this.stars = [
         new this.star(), new this.star(), new this.star(), new this.star(), new this.star(), new this.star()
      ];

For this you should use:
Code (glbasic) Select

//the star "class"
TYPE TStar
    //Attributes and Functions
ENDTYPE

LOCAL stars[] AS TStar //declare the star array

//push 100 stars into the array
FOR i=0 TO 99
    LOCAL star AS TStar //temp star
    //set the position of the star or so...

    DIMPUSH stars[], star //put the temp star into the array
NEXT
Title: Re: Array of constants and objects
Post by: matchy on 2011-Sep-12
Someone give backslider some chocolate now!!!
Title: Re: Array of constants and objects
Post by: doimus on 2011-Sep-12
It would be nice to have some sort of enumerted array support in GLBasic.

Something like:
Code (glbasic) Select
DIMENUM arr[], FIRST, SECOND, THIRD

arr[FIRST] = 42

//..is the same as...

arr[0] = 42



Currently, this can be done with constants/globals but it gets really messy very quickly due to global nature of those values and lots of space needed for pointless definitions:

Code (glbasic) Select
CONSTANT FIRST = 0
CONSTANT SECOND = 1
CONSTANT THIRD = 2
//.... few years in the future ...
CONSTANT BILLIONTH =  lulz

Title: Re: Array of constants and objects
Post by: okee on 2011-Sep-12
re backsliders code, just for the record the LOCAL star AS TStar
would be declared 100 times in the loop

Quote
//the star "class"
TYPE TStar
    //Attributes and Functions
ENDTYPE

LOCAL stars[] AS TStar //declare the star array

//push 100 stars into the array
LOCAL star AS TStar //temp star moved out of the loop
FOR i=0 TO 99
   
    //set the position of the star or so...

    DIMPUSH stars[], star //put the temp star into the array
NEXT
Title: Re: Array of constants and objects
Post by: hardyx on 2011-Sep-14
Quote from: doimus on 2011-Sep-12
It would be nice to have some sort of enumerted array support in GLBasic.

Something like:
Code (glbasic) Select
DIMENUM arr[], FIRST, SECOND, THIRD

arr[FIRST] = 42


Or something like this, as used in Visual Basic:

Code (glbasic) Select

ENUM Fruits
     Apple = 1,
     Orange,
     Banana
ENDENUM

DIM fr AS Fruits
fr = Orange


This could create CONSTANTs instructions with the values. When you don't assign a value to the constant, takes the previous value +1.
Title: Re: Array of constants and objects
Post by: aroldo on 2011-Sep-15
Thanks Guys, :nw:

TYPE name worked fine!