• Dear Cerberus X User!

    As we prepare to transition the forum ownership from Mike to Phil (TripleHead GmbH), we need your explicit consent to transfer your user data in accordance with our amended Terms and Rules in order to be compliant with data protection laws.

    Important: If you accept the amended Terms and Rules, you agree to the transfer of your user data to the future forum owner!

    Please read the new Terms and Rules below, check the box to agree, and click "Accept" to continue enjoying your Cerberus X Forum experience. The deadline for consent is April 5, 2024.

    Do not accept the amended Terms and Rules if you do not wish your personal data to be transferred to the future forum owner!

    Accepting ensures:

    - Continued access to your account with a short break for the actual transfer.

    - Retention of your data under the same terms.

    Without consent:

    - You don't have further access to your forum user account.

    - Your account and personal data will be deleted after April 5, 2024.

    - Public posts remain, but usernames indicating real identity will be anonymized. If you disagree with a fictitious name you have the option to contact us so we can find a name that is acceptable to you.

    We hope to keep you in our community and see you on the forum soon!

    All the best

    Your Cerberus X Team

can someone help 2d obj array

Dubbsta

Active member
Joined
Jul 13, 2017
Messages
208
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
 
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.
 
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
 
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
 
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.
 
ohhhhhhh... i got it. now to figure this out. thanks Holzchopz
 
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:
ok thanks dawlane wasnt sure what a key guide was. still a little green myself
 
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.
 
Back
Top Bottom