Tiny Thor (made with Monkey X)
Develop on Windows or OSX and deploy easily
Crypt of the Necrodancer (made with Monkey X)
New Star Soccer - BAFTA Winner 2013!!! (made with Monkey X )
Ted - The code editor of Cerberus X
Avalon Legends (made with Monkey X)
Race Time (made with Monkey X)

can someone help 2d obj array

Dubbsta Nov 2, 2018

  1. Dubbsta

    Dubbsta Member

    Messages:
    97
    Likes Received:
    20
    Trophy Points:
    8
    trying to draw a map of blocks but i get a memory access violation

    Code (Cerberus X):
    1.  
    2. Strict
    3.  
    4. Import mojo
    5.  
    6.  
    7.  
    8.  
    9.  
    10. Class Block
    11.  
    12.  
    13.  
    14.  Field x:Int,y:Int
    15.  
    16.  Field size:Int
    17.  
    18.  
    19.  
    20.         Method New(x:Int,y:Int,size:Int)
    21.  
    22.            Self.x = x
    23.  
    24.            Self.y = y
    25.  
    26.            Self.size = size
    27.  
    28.          End
    29.  
    30.  
    31.  
    32.           Method draw:Void()
    33.           End
    34.  
    35. End
    36.  
    37.  
    38.  
    39.  
    40.  
    41.  
    42.  
    43. Class Grass Extends Block
    44.  
    45.  
    46.  
    47.              Method New(x:Int,y:Int,size:Int)
    48.  
    49.                  Super.New(x,y,size)
    50.  
    51.              End
    52.  
    53.  
    54.  
    55.              Method draw:Void()
    56.  
    57.              SetColor(0,255,0)
    58.  
    59.                  DrawRect(x,y,size,size)
    60.  
    61.              End
    62.  
    63. End
    64.  
    65.  
    66.  
    67.  
    68.  
    69.  
    70.  
    71.  
    72.  
    73. Class test Extends App
    74.  
    75.  Field grass:Block
    76.  
    77.  Field map:Block[][]
    78.  
    79.  Field width:Int = 2
    80.  
    81.  Field height:Int = 2
    82.  
    83.  
    84.  
    85.  
    86.  
    87.               Method OnCreate:Int()
    88.  
    89.               SetUpdateRate(60)
    90.  
    91.                   map = [[grass,grass],
    92.                              [grass,grass]]
    93.  
    94.  
    95.  
    96.                      For Local i:Int = 0 Until height
    97.                          For Local j:Int = 0 Until width
    98.  
    99.                             map[i][j] = New Grass(grass.x * 40,grass.y * 40,40)  '<---memory access violation here
    100.  
    101.                           End
    102.  
    103.                      End
    104.                      Return 0
    105.  
    106.                End
    107.  
    108.  
    109.  
    110.  
    111.  
    112.  
    113.  
    114.  
    115.  
    116.                  Method OnUpdate:Int()
    117.  
    118.                      Return 0
    119.  
    120.                  End
    121.  
    122.  
    123.  
    124.  
    125.  
    126.  
    127.  
    128.  
    129.  
    130.                   Method OnRender:Int()
    131.                    Cls 0,0,0
    132.  
    133.                       For Local i:Int = 0 Until height
    134.                            For Local j:Int = 0 Until width
    135.                                  If map[i][j] = grass
    136.                                       grass.draw()
    137.                              End
    138.                         End
    139.  
    140.                    End
    141.                    Return 0
    142.  
    143.                    End
    144.  
    145.  
    146.  
    147.  
    148.  
    149. End
    150.  
    151.  
    152.  
    153.  
    154.  
    155. Function Main:Int()
    156.  
    157.               New test()
    158.                Return 0
    159. End
    160.  
    161.  
     
  2. dawlane

    dawlane Active Member CX Code Contributor

    Messages:
    385
    Likes Received:
    161
    Trophy Points:
    43
    OK look closely at:
    Code (Cerberus X):
    1. New Grass(grass.x * 40,grass.y * 40,40)
    The object grass doesn't exist yet.
    Try:
    Code (Cerberus X):
    1. map[i][j] = New Grass(j * 40, i * 40,40)
    The problem would be how would you populate the map array with another type of block without a block key guide?

    The next problem will be
    Code (Cerberus X):
    1. If map[i][j] = grass
    For a start grass is defined as type Block and not type Grass and is set to Null.
    Second even if you could detect it that way, each object would be different and would never evaluate.

    The only way that I can think of would be to use the reflection module to get class names, but that is pretty advanced and can throw up other problems.

    You should reconsider defining the array as a integer identifying numbers for each type of block and using a stack container to store the objects and arrays of objects.
     
    Dubbsta likes this.
  3. MikeHart

    MikeHart Administrator Staff Member Administrator Moderator

    Messages:
    1,237
    Likes Received:
    300
    Trophy Points:
    83
    Arrays in CX can always be a pain in the rear end.

    Code (Cerberus X):
    1. Strict
    2.  
    3. Import mojo
    4. Class Block
    5.     Field x:Int,y:Int
    6.     Field size:Int
    7.  
    8.     Method New(x:Int,y:Int,size:Int)
    9.         Self.x = x
    10.         Self.y = y
    11.         Self.size = size
    12.     End
    13.  
    14.     Method draw:Void()
    15.     End
    16. End
    17.  
    18. Class Grass Extends Block
    19.     Method New(x:Int,y:Int,size:Int)
    20.         Super.New(x,y,size)
    21.     End
    22.  
    23.     Method draw:Void()
    24.         SetColor(0,255,0)
    25.         DrawRect(x,y,size,size)
    26.     End
    27. End
    28.  
    29. Class test Extends App
    30.     'Field grass:Grass
    31.     Field map:Grass[][]
    32.     Field width:Int = 10
    33.     Field height:Int = 5
    34.     Field angle:Float = 0.0
    35.  
    36.     Method OnCreate:Int()
    37.         SetUpdateRate(60)
    38.         'map = [[grass,grass], [grass,grass]]
    39.         map = CreateMap(width, height)
    40.         'For Local i:Int = 0 Until height
    41.             'For Local j:Int = 0 Until width
    42.         For Local i:Int = 0 Until width
    43.             For Local j:Int = 0 Until height
    44.                 'map[i][j] = New Grass(grass.x * 40,grass.y * 40,40)  '<---memory access violation here
    45.                 map[i][j] = New Grass(i * 40,j * 40,35)
    46.             End
    47.         End
    48.         Return 0
    49.     End
    50.    
    51.     Method OnUpdate:Int()
    52.         angle += 0.1
    53.         Return 0
    54.     End
    55.  
    56.     Method OnRender:Int()
    57.         Cls 0,0,50
    58.         'For Local i:Int = 0 Until height
    59.             'For Local j:Int = 0 Until width
    60.         For Local i:Int = 0 Until width
    61.             For Local j:Int = 0 Until height
    62.                 'If map[i][j] = grass
    63.                 If map[i][j] <> Null
    64.                     'grass.draw()
    65.                     map[i][j].draw()
    66.                 End
    67.             End
    68.         End
    69.        
    70.         SetColor 128,128,128
    71.         PushMatrix
    72.         Translate DeviceWidth()/2,DeviceHeight()/2
    73.         Scale 2.0,2.0
    74.         Rotate 360.0-angle
    75.         DrawText "Cerberus X - Mojo template", 0, 0, .5, .5
    76.         PopMatrix
    77.         Return 0
    78.     End
    79. End
    80.  
    81. Function Main:Int()
    82.     New test()
    83.     Return 0
    84. End
    85.  
    86.  
    87. '***************************************
    88. Function CreateMap:Grass[][]( columns:Int, rows:Int)
    89.     Local a:Grass[][] = New Grass[columns][]
    90.     For Local c:Int = 0 Until (columns)
    91.         a[c] = New Grass[rows]
    92.     Next
    93.     Return a      
    94. End
     
    Dubbsta likes this.
  4. Dubbsta

    Dubbsta Member

    Messages:
    97
    Likes Received:
    20
    Trophy Points:
    8
    thats great mike thanks.
     
  5. Dubbsta

    Dubbsta Member

    Messages:
    97
    Likes Received:
    20
    Trophy Points:
    8
    i was trying for something more like this but it seems the gblock object only gets created because blocks are all green


    Code (Cerberus X):
    1.  
    2.  
    3. Strict
    4.  
    5. Import mojo
    6.  
    7.  
    8.  
    9.  
    10. Class Block
    11.     Field x:Int,y:Int
    12.     Field size:Int
    13.     Method New(x:Int,y:Int,size:Int)
    14.         Self.x = x
    15.         Self.y = y
    16.         Self.size = size
    17.     End
    18.     Method draw:Void()
    19.     End
    20. End
    21. Class GreenBlock Extends Block
    22.     Method New(x:Int,y:Int,size:Int)
    23.         Super.New(x,y,size)
    24.     End
    25.     Method draw:Void()
    26.         SetColor(0,255,0)
    27.         DrawRect(x,y,size,size)
    28.     End
    29. End
    30.  
    31.  
    32.  
    33. Class RedBlock Extends Block
    34.     Method New(x:Int,y:Int,size:Int)
    35.         Super.New(x,y,size)
    36.     End
    37.     Method draw:Void()
    38.         SetColor(255,0,0)
    39.         DrawRect(x,y,size,size)
    40.     End
    41. End
    42.  
    43.  
    44. Class test Extends App
    45.     Field gblock:Block
    46.     Field rblock:Block
    47.     Field map:Block[][]
    48.     Field width:Int = 2
    49.     Field height:Int = 3
    50.  
    51.  
    52.        Method OnCreate:Int()
    53.         SetUpdateRate(60)
    54.            map = [[rblock,gblock],
    55.                      [rblock,gblock],
    56.                      [gblock,gblock]]
    57.         For Local i:Int = 0 Until height
    58.            For Local j:Int = 0 Until width
    59.      
    60.                If  map[i][j] = gblock
    61.                        map[i][j] = New GreenBlock(j * 46,i * 46,40)
    62.                Elseif map[i][j] = rblock
    63.                           map[i][j] = New RedBlock(j * 46,i * 46,40)
    64.                End
    65.             End
    66.         End
    67.         Return 0
    68.     End
    69.  
    70.  
    71.     Method OnUpdate:Int()
    72.  
    73.  
    74.         Return 0
    75.     End
    76.     Method OnRender:Int()
    77.         Cls 0,0,0
    78.         For Local i:Int = 0 Until height
    79.             For Local j:Int = 0 Until width            
    80.                If map[i][j] <> Null
    81.                     map[i][j].draw()              
    82.                 End
    83.             End
    84.         End
    85.  
    86.         Return 0
    87.     End
    88. End
    89.  
    90.  
    91. Function Main:Int()
    92.     New test()
    93.     Return 0
    94. End
     
  6. Holzchopf

    Holzchopf Moderator Staff Member Moderator CX Code Contributor 3rd Party Module Dev

    Messages:
    263
    Likes Received:
    117
    Trophy Points:
    43
    no no. Both rblock and rblock are un-initialized fields, meaning both are Null. So the map you init is [[Null,Null],[Null,Null],[Null,Null]] and your first If-statement always evaluates to true, because If Null = Null is always true. Thus, you create for all your map elements a new GreenBlock.
     
    Dubbsta likes this.
  7. Dubbsta

    Dubbsta Member

    Messages:
    97
    Likes Received:
    20
    Trophy Points:
    8
    ohhhhhhh... i got it. now to figure this out. thanks Holzchopz
     
  8. dawlane

    dawlane Active Member CX Code Contributor

    Messages:
    385
    Likes Received:
    161
    Trophy Points:
    43
    As I stated earlier.
    Code (Cerberus X):
    1. Strict
    2. Import mojo
    3.  
    4. Const GREENBLK:=1
    5. Const REDBLK:=2
    6.  
    7. Class Block
    8.     Field x:Int,y:Int
    9.     Field size:Int
    10.     Method New(x:Int,y:Int,size:Int)
    11.         Self.x = x
    12.         Self.y = y
    13.         Self.size = size
    14.     End
    15.     Method draw:Void()
    16.     End
    17. End
    18.  
    19. Class GreenBlock Extends Block
    20.     Method New(x:Int,y:Int,size:Int)
    21.         Super.New(x,y,size)
    22.     End
    23.     Method draw:Void()
    24.         SetColor(0,255,0)
    25.         DrawRect(x,y,size,size)
    26.     End
    27. End
    28.  
    29. Class RedBlock Extends Block
    30.     Method New(x:Int,y:Int,size:Int)
    31.         Super.New(x,y,size)
    32.     End
    33.     Method draw:Void()
    34.         SetColor(255,0,0)
    35.         DrawRect(x,y,size,size)
    36.     End
    37. End
    38. Class test Extends App
    39.     Field map:Block[][]
    40.     Field width:Int = 2
    41.     Field height:Int = 3
    42.        Method OnCreate:Int()
    43.         SetUpdateRate(60)
    44.         Local keymap:Int[][] = [[REDBLK,GREENBLK],
    45.                                  [REDBLK,GREENBLK],
    46.                                  [GREENBLK,GREENBLK]]
    47.         For Local i:Int = 0 Until height
    48.             map=map.Resize(map.Length()+1)
    49.             For Local j:Int = 0 Until width
    50.                 map[i]=map[i].Resize(map[i].Length()+1)
    51.               If  keymap[i][j] = GREENBLK
    52.                   map[i][j] = New GreenBlock(j * 46,i * 46,40)
    53.               Elseif keymap[i][j] = REDBLK
    54.                 map[i][j] = New RedBlock(j * 46,i * 46,40)
    55.               End
    56.             End
    57.         End
    58.         Return 0
    59.     End
    60.     Method OnUpdate:Int()
    61.         Return 0
    62.     End
    63.     Method OnRender:Int()
    64.         Cls 0,0,0
    65.         For Local i:Int = 0 Until height
    66.             For Local j:Int = 0 Until width          
    67.                If map[i][j] <> Null
    68.                     map[i][j].draw()            
    69.                 End
    70.             End
    71.         End
    72.         Return 0
    73.     End
    74. End
    75. Function Main:Int()
    76.     New test()
    77.     Return 0
    78. End
    79.  
    EDIT: I bet the next question you are going to ask is how do you identify what a block is.
     
    Last edited: Nov 3, 2018
    Dubbsta likes this.
  9. Dubbsta

    Dubbsta Member

    Messages:
    97
    Likes Received:
    20
    Trophy Points:
    8
    ok thanks dawlane wasnt sure what a key guide was. still a little green myself
     
  10. Gerry Quinn

    Gerry Quinn Active Member

    Messages:
    108
    Likes Received:
    41
    Trophy Points:
    28
    Really your array should either be of ints (with value GRASS, WALL or whatever) or tile objects that have a field saying what kind of terrain they are.

    It doesn't pay to put too much of your game logic into the class structure.

    Given that they are most likely all the same size (or your array will have problems anyway), it's generally best to have the tile size as an independent value.
     
    Dubbsta likes this.