Dark Bit Factory & Gravity

PROGRAMMING => Other languages => Blitz => Topic started by: spathi on November 18, 2011

Title: Strange Issue with Arrays on Blitzmax
Post by: spathi 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
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: spathi 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?
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: zawran 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.
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: spathi 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?
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: zawran 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.
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: Shockwave 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.
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: spathi 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.
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: Shockwave 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.
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: spathi 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. 
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: spathi on November 30, 2011
Try an IDE called Hydramax. (http://www.blitzbasic.com/Community/posts.php?topic=68092)  It's free and OK.

[edit - link fixed sw]
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: Shockwave on December 03, 2011
Ah, that's by Eikon :) I remember him from the old days of Blitzcoder!

Thanks Spathi!
Title: Re: Strange Issue with Arrays on Blitzmax
Post by: spathi 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...