Problem using image arrays with "Var" in BlitzmaxNG

Started by Rooster, April 16, 2018, 20:43:16

Previous topic - Next topic

Rooster

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) Select

Global Img:TImage []

Graphics 800,600,0

LoadImages (Img:TImage [])

Function LoadImages (i:TImage [] Var)
  i = i [..2]
  i [0] = LoadImage ("image1.png")
  i [1] = LoadImage ("image2.png")
End Function

If( Img [0] = Null)
  Print "fail"
Else
  Print "success"
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) Select

Global text:String  []

Write  (text:String  [])

Function Write (i:String  [] Var)
  i = i [..2]
  i [0] = "textone"
  i [1] = "texttwo"
End Function

If( text [0] = Null)
  Print "fail"
Else
  Print "success"
EndIf


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

col

This...
Write  (text:String  [])
doesn't compiler for me in the legacy 'Max or NG.

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.
https://github.com/davecamp

"When you observe the world through social media, you lose your faith in it."

Derron

_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) Select

Type TAssets
Field images:TImage[]
End Type
Global assets:TAssets = new TAssets

LoadImages(assets)


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) Select

Function LoadImages:TImage[] (i:TImage[] Var)
i = i[..2]
i[0] = CreateImage(100,100)
i[1] = CreateImage(200,100)
Return i
End Function

'until it is fixed, use this way: instead of just "LoadImages(Img)"
Img = LoadImages(Img)




@ your code

Code (BlitzMax) Select

SuperStrict
Framework Brl.StandardIO
Import Brl.GLMax2D
'Import Brl.Image

Global Img:TImage[]

Graphics 800,600,0

LoadImages (Img:TImage [])

Function LoadImages (i:TImage [] Var)
  i = i [..2]
  i [0] = LoadImage ("image1.png")
  i [1] = LoadImage ("image2.png")
End Function

If( Img [0] = Null)
  Print "fail"
Else
  Print "success"
EndIf


outputs here:

[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.


Rooster

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?