Array of constants and objects

Previous topic - Next topic

aroldo

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()
      ];
[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2

ampos

DIMDATA array$[],"Value 1","Value 2",...
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

mentalthink

Or something more large, but works equal:

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


backslider

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

matchy

Someone give backslider some chocolate now!!!

doimus

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


okee

#6
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
Android: Samsung Galaxy S2 -  ZTE Blade (Orange San Francisco) - Ainol Novo 7 Aurora 2
IOS: 2 x Ipod Touch (1G)

hardyx

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

aroldo

Thanks Guys, :nw:

TYPE name worked fine!
[a http://apd-games.com/][img http://apd-games.com/images/APDGames135.png][/a]
MacBook Pro OS X El Capitan
XCode Version 7
iPhone 6 running  iOS 9
iPad Mini running  iOS 7.1
Galaxy S5
Dell Latitude Windows 8 Enterprise
Palm Pre, Palm Pre2