GLBasic forum

Main forum => GLBasic - en => Topic started by: r0ber7 on 2012-Oct-26

Title: Passing an array of TYPEs to a function
Post by: r0ber7 on 2012-Oct-26
Hey.

The compiler just told me the weirdest thing. It said this:

Code (glbasic) Select

  given: DGArray<s_object>
  wants: DGArray<s_object>
"level_process.gbas"(172) error : wrong argument type : quicksort, arg no: 1


I'm doing this:

Function:

Code (glbasic) Select

FUNCTION quicksort : arr[] AS s_object, start, end
...
ENDFUNCTION


Function call:

Code (glbasic) Select

FOR p=0 TO 8
quicksort(s_objects[p], 0, s_objects_n[p]-1)
NEXT


s_objects[] is a two dimensional array of type s_object. It stores tiles for my levels. So if I wanted the x position of a tile, I would access s_objects[5][3].x for example. Any ideas? I've tried juggling around with brackets but that just gives me syntax errors.

The function is basically made to do just one thing, so I could hardcode the s_objects[][] array in there, but I don't like that solution, it's sloppy coding. :P Help!
Title: Re: Passing an array of TYPEs to a function
Post by: MrTAToad on 2012-Oct-27
It looks like you are passing a single element from an array whilst your function wants the complete array.

So you could use :

Code (glbasic) Select
quicksort(s_objects[],0,s_objects_n[p]-1)

Or change the function to :

Code (glbasic) Select
FUNCTION quicksort : arr AS s_object, start, end
Title: Re: Passing an array of TYPEs to a function
Post by: r0ber7 on 2012-Oct-27
Quote from: MrTAToad on 2012-Oct-27
It looks like you are passing a single element from an array whilst your function wants the complete array.

So you could use :

Code (glbasic) Select
quicksort(s_objects[],0,s_objects_n[p]-1)

Or change the function to :

Code (glbasic) Select
FUNCTION quicksort : arr AS s_object, start, end

s_objects is two dimensional, s_objects[0] is not an element but an array inside an array. Changing the function I cannot do, since it needs the array as an argument. If I do your first solution, the compiler complains.
Title: Re: Passing an array of TYPEs to a function
Post by: MrTAToad on 2012-Oct-27
If it's a two dimensional array, then it's going to be an interesting problem.  Multi-dimensional arrays wont be accepted as a function parameter, which means you would need the second array inside the type.
Title: Re: Passing an array of TYPEs to a function
Post by: r0ber7 on 2012-Oct-28
Quote from: MrTAToad on 2012-Oct-27
Multi-dimensional arrays wont be accepted as a function parameter

Ahh, I see! I'll work around it by hardcoding it in, passing the index p as a function parameter. It's a global array anyway, and it's either this solution or rewrite a LOT of code. :P

Thanks for the help, INC knowledge, 1. ;)