September 21, 2018, 02:02:27 AM

Author Topic: Problem using image arrays with "Var" in BlitzmaxNG  (Read 326 times)

Offline Rooster

  • Sr. Member
  • ****
  • Posts: 355
  • I don't know what this does...
Problem using image arrays with "Var" in BlitzmaxNG
« on: April 16, 2018, 08:43:16 PM »
In vanilla Bmax I could make a empty image array, pass it to a function using "Var", and load images into the original variable.
Something like this,
Code: BlitzMax
  1. Global Img:TImage []
  2.  
  3. Graphics 800,600,0
  4.  
  5. LoadImages (Img:TImage [])
  6.  
  7. Function LoadImages (i:TImage [] Var)
  8.   i = i [..2]
  9.   i [0] = LoadImage ("image1.png")
  10.   i [1] = LoadImage ("image2.png")
  11. End Function
  12.  
  13. If( Img [0] = Null)
  14.   Print "fail"
  15. Else
  16.   Print "success"
  17. EndIf
However in NG The global Img just comes back empty. If I make Img not an array it works just fine.
I've noticed that the same applies to TSound variables.

I know that I can use a empty array as a argument with "Var",
Code: BlitzMax
  1. Global text:String  []
  2.  
  3. Write  (text:String  [])
  4.  
  5. Function Write (i:String  [] Var)
  6.   i = i [..2]
  7.   i [0] = "textone"
  8.   i [1] = "texttwo"
  9. End Function
  10.  
  11. If( text [0] = Null)
  12.   Print "fail"
  13. Else
  14.   Print "success"
  15. EndIf

Did something change about how "Var" works in NG, is it a bug?
Was I just exploiting something in non-strict on vanilla?

Offline col

  • Sr. Member
  • ****
  • Posts: 397
Re: Problem using image arrays with "Var" in BlitzmaxNG
« Reply #1 on: April 17, 2018, 07:03:15 AM »
This...
Code: [Select]
Write  (text:String  [])doesn't compiler for me in the legacy 'Max or NG.

Code: [Select]
Write (text)will compile ok

But yes.. your original point with 'Var' looks like a possible bug in NG. It would be worth reporting the find via the GitHub repository as it may get missed if just only posted here.
Any bugs in my code are proof of its hand-coded nature.
https://github.com/davecamp

Offline Derron

  • Hero Member
  • *****
  • Posts: 1263
Re: Problem using image arrays with "Var" in BlitzmaxNG
« Reply #2 on: April 17, 2018, 07:37:36 AM »
_meanwhile_ you could wrap your "arr:bla[]" into a custom type - and then remove the need of "var". Real "objects" (so not "string, int, float..") are passed as reference already, so you might adjust their properties within a function.

Means:
Code: BlitzMax
  1. Type TAssets
  2.  Field images:TImage[]
  3. End Type
  4. Global assets:TAssets = new TAssets
  5.  
  6. LoadImages(assets)
  7.  

With "var" you are enabled to assign a new "variable/object" to the passed parameter - but with passed by referenced parameters you aren't. But you can still access/manipulate their properties - which is the "Field" in that case.


What also works is this:
Code: BlitzMax
  1. Function LoadImages:TImage[] (i:TImage[] Var)
  2.         i = i[..2]
  3.         i[0] = CreateImage(100,100)
  4.         i[1] = CreateImage(200,100)
  5.         Return i
  6. End Function
  7.  
  8. 'until it is fixed, use this way: instead of just "LoadImages(Img)"
  9. Img = LoadImages(Img)
  10.  



@ your code

Code: BlitzMax
  1. SuperStrict
  2. Framework Brl.StandardIO
  3. Import Brl.GLMax2D
  4. 'Import Brl.Image
  5.  
  6. Global Img:TImage[]
  7.  
  8. Graphics 800,600,0
  9.  
  10. LoadImages (Img:TImage [])
  11.  
  12. Function LoadImages (i:TImage [] Var)
  13.   i = i [..2]
  14.   i [0] = LoadImage ("image1.png")
  15.   i [1] = LoadImage ("image2.png")
  16. End Function
  17.  
  18. If( Img [0] = Null)
  19.   Print "fail"
  20. Else
  21.   Print "success"
  22. EndIf
  23.  

outputs here:
Code: [Select]
[100%] Linking:var_param
Executing:var_param
success

Process complete
- so in release mode it finds something "at img[0]" (aka is "TImage") while the index is already an access "out of bounds" (in debug this is catched) - that is pretty odd.

I also replaced that global with a function using a local - to check if it is based on "global passed" versus "local passed".



I created an issue for this thing:
https://github.com/bmx-ng/bcc/issues/323
(and maybe in connection to this)
https://github.com/bmx-ng/bcc/issues/324

There I also extended your sample code (to not use external media) and also annotated, that using "string[] var" works - which might help narrowing things down.


Offline Rooster

  • Sr. Member
  • ****
  • Posts: 355
  • I don't know what this does...
Re: Problem using image arrays with "Var" in BlitzmaxNG
« Reply #3 on: April 17, 2018, 08:04:50 PM »
Okay so it was a bug in NG.
Thanks for reporting it for me Derron. :)
I also didn't think about removing external media from my example code, thanks for bringing that to my attention.

@col
That seems really weird. It doesn't work for me in legacy too, but it works in NG. What version are you using?