Author Topic: Strange Issue with Arrays on Blitzmax  (Read 8288 times)

0 Members and 1 Guest are viewing this topic.

Offline spathi

  • C= 64
  • **
  • Posts: 78
  • Karma: 5
    • View Profile
Strange Issue with Arrays on Blitzmax
« on: November 18, 2011 »
I have a very peculiar issue that I'm running into.  This is, frankly, the most intractable bug I've run into for years.

The code here is VERY simple and fundamental, therefore it can't be a bug in Blitz.  It has to be something that I'm not seeing.

This is an 8x8x8 cubical array.  I am attempting to set each cell of the array with a different value.  However, when I later read the cells, they all come out the same as the last value that I fed in!  In other words, ALL THE CELLS ARE APPARENTLY OVERWRITTEN WITH THE SAME VALUE.

The strangest thing is that the error happened with Blitz's built-in multidimensional array functionality.  So I thought that it might be a bug with that, and I wrote my own array class using a 1d array, with aliasing to make it 3d thus:

Code: [Select]
data_array[x+(8*y)+(64*z)].r=x+y+z
Trouble is that the same error occurs!

I have done my best to pare away everything that is not necessary to isolate the bug, but I still can't see where the problem lies.  Might anyone have any idea?  I am afraid that there may be some sort of bug in the Blitzmax functionality for arrays of types....!?!?!

Initially things weren't packaged away in classes but were all procedural.  Imagine my horror after I wrote my own array class, packaged away in classes, and the same error occurred with different code!

Am I going insane?  Sure feels that way.

Code: [Select]
SuperStrict

'jgrid is an 8x8x8 grid of Tcolor types.  For testing purposes, just looking at the r value-- same problem occurs with g and b so removed them.
Type Tcolor
Field r:Double
End Type

Type Jgrid
Field data_array:tcolor[512]
Field tempcolor:tcolor = New tcolor

Method set(x:Int,y:Int,z:Int,r:Float,g:Float,b:Float)
data_array[x+(8*y)+(64*z)].r=r
End Method

Method get:tcolor(x:Int,y:Int,z:Int)
Local returnvalue: Tcolor = New Tcolor
returnvalue = data_array[x+(8*y)+(64*z)]
Return returnvalue
End Method

Method randomize()
For Local x:Int = 0 To 7
For Local y:Int = 0 To 7
For Local z:Int = 0 To 7

data_array[x+(8*y)+(64*z)].r=x+y+z  'SHOULD THIS NOT ASSIGN EVERY ARRAY ELEMENT WITH A DIFFERENT VALUE!?

Next
Next
Next
End Method
End Type

' Here is the testing driver for the above classes.

Global testjgrid:jgrid=New jgrid
testjgrid.randomize()
Local testcolor:tcolor = New tcolor

testcolor = testjgrid.get (3,3,3)

Print testcolor.r

testcolor = testjgrid.get (7,7,7)

Print testcolor.r
« Last Edit: November 18, 2011 by spathi »

Offline spathi

  • C= 64
  • **
  • Posts: 78
  • Karma: 5
    • View Profile
Re: Strange Issue with Arrays on Blitzmax
« Reply #1 on: November 18, 2011 »
Is it possible that my syntax for arrays of types is somehow wrong?  Is it declaring one tcolor object instead of 512 of them?

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: Strange Issue with Arrays on Blitzmax
« Reply #2 on: November 18, 2011 »
You can either create a "new" method which contains code that will be run when a new type is created. Here you can initiate the array with types of Tcolor so that it knows what and where to reference when used later on. Place the method together with the other methods of your Jgrid type.

Code: [Select]
Method New()
For Local i:Int = 0 To 511
data_array[i] = New Tcolor
Next
End Method

Or you can initiate each type as they are being put into use. In you randomize method add the following line before the data_array is given a value:

Code: [Select]
data_array[x+(8*y)+(64*z)] = New Tcolor

Using one of the above mentioned solutions makes your code seem to work.

Offline spathi

  • C= 64
  • **
  • Posts: 78
  • Karma: 5
    • View Profile
Re: Strange Issue with Arrays on Blitzmax
« Reply #3 on: November 18, 2011 »
Great answer and I was suspecting that this might be the case.

WHY is it the case?

Are you saying that it's essentially treating all references to the objects as references to a single tcolor object? 

I understand the error now.  I was assuming that the objects in the jgrid array were automatically being constructed but I see that this is not the case.  For some reason I thought that it would call the constructor as the objects were needed.

Thanks for your help.  I may or may not have some other questions at some point in the future but I've written other much longer things in Blitz before and never had a problem like this. 

I appreciate it very much and I will be contributing opengl graphics demo code here, I think people might particularly like some of the pseudo-copper stuff I've done.

I'm also curious as to how well you like Blitz for serious projects.  Do you feel that it's powerful enough to do well-made indie titles?
« Last Edit: November 18, 2011 by spathi »

Offline zawran

  • Sponsor
  • Pentium
  • *******
  • Posts: 909
  • Karma: 67
    • View Profile
Re: Strange Issue with Arrays on Blitzmax
« Reply #4 on: November 18, 2011 »
Arrays in blitzmax are not initiated when created which is why you need to either add this with a "new" method or later on.

I have used blitzmax since it was released and have used it for a lot of serious projects. I have not made any commercial releases myself with it, but a good handful of people have and a number of them have made a pretty penny doing so. There is probably 10+ games on the bigfishgames portal that was made with blitzmax. I would say that it is well suited for making indie titles, I can only recommend it as it is really great for getting work done and it is really a stable platform. It doesn't hurt either that is compiles to three platforms which is great for indie games since a lot of people using macs and linux does not have a problem with paying for the games, so there is a good niche market that can be utilized there.

Online Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17413
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Strange Issue with Arrays on Blitzmax
« Reply #5 on: November 18, 2011 »
Quote
I appreciate it very much and I will be contributing opengl graphics demo code here, I think people might particularly like some of the pseudo-copper stuff I've done.

You're correct there spathi.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline spathi

  • C= 64
  • **
  • Posts: 78
  • Karma: 5
    • View Profile
Re: Strange Issue with Arrays on Blitzmax
« Reply #6 on: November 19, 2011 »
What I'm writing right now is a roguelike dungeon shooter with a voxel engine.  Think Gauntlet meets Nethack. 

I'm rather happy with the Blitzmax opengl implementation.  All in all I love the language but it does have some idiosyncracies that you have to get used to.

Online Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17413
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Strange Issue with Arrays on Blitzmax
« Reply #7 on: November 19, 2011 »
Yeah, I played with bmax too and I like it, I don't like the IDE but that's all I didn't like.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline spathi

  • C= 64
  • **
  • Posts: 78
  • Karma: 5
    • View Profile
Re: Strange Issue with Arrays on Blitzmax
« Reply #8 on: November 23, 2011 »
Well, since the IDE is written in Blitz and they provide source...

Also on linux there's a tutorial for getting it running with Gedit so that you can compile and run right from Gedit with F5. 

Offline spathi

  • C= 64
  • **
  • Posts: 78
  • Karma: 5
    • View Profile
Re: Strange Issue with Arrays on Blitzmax
« Reply #9 on: November 30, 2011 »
Try an IDE called Hydramax.  It's free and OK.

[edit - link fixed sw]
« Last Edit: December 03, 2011 by Shockwave »

Online Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17413
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Strange Issue with Arrays on Blitzmax
« Reply #10 on: December 03, 2011 »
Ah, that's by Eikon :) I remember him from the old days of Blitzcoder!

Thanks Spathi!
Shockwave ^ Codigos
Challenge Trophies Won:

Offline spathi

  • C= 64
  • **
  • Posts: 78
  • Karma: 5
    • View Profile
Re: Strange Issue with Arrays on Blitzmax
« Reply #11 on: December 13, 2011 »
I quit using it because of stability issues so you might want to be careful.  The code folding is nice but not necessary and I am sort of a minimalist...