can someone help 2d obj array

Dubbsta

Active Member
Joined
Jul 13, 2017
trying to draw a map of blocks but i get a memory access violation

Code:
Strict

Import mojo





Class Block



 Field x:Int,y:Int

 Field size:Int



        Method New(x:Int,y:Int,size:Int)

           Self.x = x

           Self.y = y

           Self.size = size

         End



          Method draw:Void()
          End

End







Class Grass Extends Block



             Method New(x:Int,y:Int,size:Int)

                 Super.New(x,y,size)

             End



             Method draw:Void()

             SetColor(0,255,0)

                 DrawRect(x,y,size,size)

             End

End









Class test Extends App

 Field grass:Block

 Field map:Block[][]

 Field width:Int = 2

 Field height:Int = 2





              Method OnCreate:Int()

              SetUpdateRate(60)

                  map = [[grass,grass],
                             [grass,grass]]



                     For Local i:Int = 0 Until height
                         For Local j:Int = 0 Until width

                            map[i][j] = New Grass(grass.x * 40,grass.y * 40,40)  '<---memory access violation here

                          End

                     End
                     Return 0

               End









                 Method OnUpdate:Int()

                     Return 0

                 End









                  Method OnRender:Int()
                   Cls 0,0,0
 
                      For Local i:Int = 0 Until height
                           For Local j:Int = 0 Until width
                                 If map[i][j] = grass
                                      grass.draw()
                             End
                        End

                   End
                   Return 0

                   End





End





Function Main:Int()

              New test()
               Return 0
End
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
OK look closely at:
Code:
New Grass(grass.x * 40,grass.y * 40,40)
The object grass doesn't exist yet.
Try:
Code:
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:
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.
 

MikeHart

Administrator
Joined
Jun 19, 2017
Location
Germany
Arrays in CX can always be a pain in the rear end.

Code:
Strict

Import mojo
Class Block
    Field x:Int,y:Int
    Field size:Int

    Method New(x:Int,y:Int,size:Int)
        Self.x = x
        Self.y = y
        Self.size = size
    End

    Method draw:Void()
    End
End

Class Grass Extends Block
    Method New(x:Int,y:Int,size:Int)
        Super.New(x,y,size)
    End

    Method draw:Void()
        SetColor(0,255,0)
        DrawRect(x,y,size,size)
    End
End

Class test Extends App
    'Field grass:Grass
    Field map:Grass[][]
    Field width:Int = 10
    Field height:Int = 5
    Field angle:Float = 0.0

    Method OnCreate:Int()
        SetUpdateRate(60)
        'map = [[grass,grass], [grass,grass]]
        map = CreateMap(width, height)
        'For Local i:Int = 0 Until height
            'For Local j:Int = 0 Until width
        For Local i:Int = 0 Until width
            For Local j:Int = 0 Until height
                'map[i][j] = New Grass(grass.x * 40,grass.y * 40,40)  '<---memory access violation here
                map[i][j] = New Grass(i * 40,j * 40,35)
            End
        End
        Return 0
    End
   
    Method OnUpdate:Int()
        angle += 0.1
        Return 0
    End

    Method OnRender:Int()
        Cls 0,0,50
        'For Local i:Int = 0 Until height
            'For Local j:Int = 0 Until width
        For Local i:Int = 0 Until width
            For Local j:Int = 0 Until height
                'If map[i][j] = grass
                If map[i][j] <> Null
                    'grass.draw()
                    map[i][j].draw()
                End
            End
        End
       
        SetColor 128,128,128
        PushMatrix
        Translate DeviceWidth()/2,DeviceHeight()/2
        Scale 2.0,2.0
        Rotate 360.0-angle
        DrawText "Cerberus X - Mojo template", 0, 0, .5, .5
        PopMatrix
        Return 0
    End
End

Function Main:Int()
    New test()
    Return 0
End


'***************************************
Function CreateMap:Grass[][]( columns:Int, rows:Int)
    Local a:Grass[][] = New Grass[columns][]
    For Local c:Int = 0 Until (columns)
        a[c] = New Grass[rows]
    Next
    Return a       
End
 

Dubbsta

Active Member
Joined
Jul 13, 2017
i was trying for something more like this but it seems the gblock object only gets created because blocks are all green


Code:
Strict

Import mojo




Class Block
    Field x:Int,y:Int
    Field size:Int
    Method New(x:Int,y:Int,size:Int)
        Self.x = x
        Self.y = y
        Self.size = size
    End
    Method draw:Void()
    End
End
Class GreenBlock Extends Block
    Method New(x:Int,y:Int,size:Int)
        Super.New(x,y,size)
    End
    Method draw:Void()
        SetColor(0,255,0)
        DrawRect(x,y,size,size)
    End
End



Class RedBlock Extends Block
    Method New(x:Int,y:Int,size:Int)
        Super.New(x,y,size)
    End
    Method draw:Void()
        SetColor(255,0,0)
        DrawRect(x,y,size,size)
    End
End


Class test Extends App
    Field gblock:Block
    Field rblock:Block
    Field map:Block[][]
    Field width:Int = 2
    Field height:Int = 3


       Method OnCreate:Int()
        SetUpdateRate(60)
           map = [[rblock,gblock],
                     [rblock,gblock],
                     [gblock,gblock]]
        For Local i:Int = 0 Until height
           For Local j:Int = 0 Until width
      
               If  map[i][j] = gblock
                       map[i][j] = New GreenBlock(j * 46,i * 46,40) 
               Elseif map[i][j] = rblock
                          map[i][j] = New RedBlock(j * 46,i * 46,40)
               End
            End
        End
        Return 0
    End
  
  
    Method OnUpdate:Int()
  

        Return 0
    End
    Method OnRender:Int()
        Cls 0,0,0
        For Local i:Int = 0 Until height
            For Local j:Int = 0 Until width            
               If map[i][j] <> Null
                    map[i][j].draw()              
                End
            End
        End

        Return 0
    End
End


Function Main:Int()
    New test()
    Return 0
End
 

Holzchopf

Moderator
Joined
Jul 31, 2017
Location
Bern, Switzerland
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.
 

dawlane

Active Member
CX Code Contributor
Joined
Jun 21, 2017
The problem would be how would you populate the map array with another type of block without a block key guide?
As I stated earlier.
Code:
Strict
Import mojo

Const GREENBLK:=1
Const REDBLK:=2

Class Block
    Field x:Int,y:Int
    Field size:Int
    Method New(x:Int,y:Int,size:Int)
        Self.x = x
        Self.y = y
        Self.size = size
    End
    Method draw:Void()
    End
End

Class GreenBlock Extends Block
    Method New(x:Int,y:Int,size:Int)
        Super.New(x,y,size)
    End
    Method draw:Void()
        SetColor(0,255,0)
        DrawRect(x,y,size,size)
    End
End
 
Class RedBlock Extends Block
    Method New(x:Int,y:Int,size:Int)
        Super.New(x,y,size)
    End
    Method draw:Void()
        SetColor(255,0,0)
        DrawRect(x,y,size,size)
    End
End
Class test Extends App
    Field map:Block[][]
    Field width:Int = 2
    Field height:Int = 3
       Method OnCreate:Int()
        SetUpdateRate(60)
        Local keymap:Int[][] = [[REDBLK,GREENBLK],
                                 [REDBLK,GREENBLK],
                                 [GREENBLK,GREENBLK]]
        For Local i:Int = 0 Until height
            map=map.Resize(map.Length()+1)
            For Local j:Int = 0 Until width
                map[i]=map[i].Resize(map[i].Length()+1)
              If  keymap[i][j] = GREENBLK
                  map[i][j] = New GreenBlock(j * 46,i * 46,40)
              Elseif keymap[i][j] = REDBLK
                map[i][j] = New RedBlock(j * 46,i * 46,40)
              End
            End
        End
        Return 0
    End
    Method OnUpdate:Int()
        Return 0
    End
    Method OnRender:Int()
        Cls 0,0,0
        For Local i:Int = 0 Until height
            For Local j:Int = 0 Until width          
               If map[i][j] <> Null
                    map[i][j].draw()            
                End
            End
        End
        Return 0
    End
End
Function Main:Int()
    New test()
    Return 0
End
EDIT: I bet the next question you are going to ask is how do you identify what a block is.
 
Last edited:

Dubbsta

Active Member
Joined
Jul 13, 2017
ok thanks dawlane wasnt sure what a key guide was. still a little green myself
 

Gerry Quinn

Active Member
Joined
Jun 24, 2017
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.
 
Top Bottom