Page 1 of 1

[SOLVED] Array in callback Procedure

Posted: Sun Jul 18, 2021 7:20 pm
by eck49
Is there a way of accessing an array of structures in a callback procedure so that the procedure knows all about the array?

What I'd like to be able to do is this:

Code: Select all

Declare DLC()

Structure One
  a1.i
  b1.i
  etc.i
EndStructure
Define.One Dim Two(14,12,3)
Define.One Dim Three(7,22,9)

Structure Four
  pxxxr.i
  q4.i
  r4.i
EndStructure  
Define Five.Four

; and in the course of the run
Five\pxxxr = @Two() ;or, with different results, it might be @Three()
; and at some point, with gadget 44
SetGadgetData(44, *Five)			;so DLC can access Five\q4, Five\r4 and Five\pxxxr
BindGadgetEvent(44, @DLC()) ;with an eventtype

;and here it comes
Procedure DLC()
  
  Protected.i *DLC_Six.Four
  Protected.One DLC_Seven   ;this can't be exactly right, I don't need a fresh array
  
  *DLC_Six = GetGadgetData(EventGadget())
  ;so now   *DLC_Six\pxxxr contains the address of the array Two
  @DLC_Seven = *DLC_Six\pxxxr          ;bad syntax
  Debug ArraySize(DLC_DSeven(),2)   ;would be nice to get the answer '12' 
  
EndProcedure

Re: Array in callback Procedure

Posted: Sun Jul 18, 2021 8:45 pm
by GPI
The only methode (without reading undocumented pb-internal-data) that I found is a array in a structure:

Code: Select all

Structure One
  a1.i
  b1.i
  etc.i
EndStructure

Structure MyArray
  Array one.one(0,0,0)
EndStructure

two.MyArray
Dim two\one(1,2,4)

three.MyArray
Dim three\one(5,6,7) 

Procedure Check(*p.MyArray)
  Debug Hex(*p)+" - size:"
  Debug "  "+ArraySize(*p\one(), 1)
  Debug "  "+ArraySize(*p\one(), 2)
  Debug "  "+ArraySize(*p\one(), 3)
EndProcedure

check(two)
check(three)

  

Re: Array in callback Procedure

Posted: Sun Jul 18, 2021 8:55 pm
by infratec
You can try this:

Code: Select all

Structure One
  a1.i
  b1.i
  etc.i
EndStructure

Structure OneHelp
  Array OneArray.One(0, 0, 0)
EndStructure

Structure Four
  *pxxxr
  q4.i
  r4.i
EndStructure


Procedure DLC()
  
  Protected *DLC_Six.Four
  Protected *DLC_Seven.OneHelp
  
  
  *DLC_Six = GetGadgetData(44)
  *DLC_Seven = *DLC_Six\pxxxr
  
  Debug ArraySize(*DLC_Seven\OneArray(), 2)
  
EndProcedure


Define Five.Four
Define Two.OneHelp
Define Three.OneHelp

Dim Two\OneArray(14, 12, 3)
Dim Three\OneArray(7, 22, 9)




OpenWindow(0, 0, 0, 200, 300, "")
TextGadget(44, 0, 0, 100, 20, "")

Five\pxxxr = @Two
SetGadgetData(44, @Five)

DLC()

Five\pxxxr = @Three
SetGadgetData(44, @Five)

DLC()

Re: Array in callback Procedure

Posted: Sun Jul 18, 2021 11:15 pm
by eck49
@infratec and @GPI

Thank you both. This works, in that I get the answers 12 then 4 in Debug output:

Code: Select all

Declare DLC()

Structure One
  a1.i
  b1.i
  etc.i
EndStructure

Structure OneX
  Array OneA.One(0,0,0)
EndStructure  

Two.OneX
Dim Two\OneA(14,12,3)

Three.OneX
Dim Three\OneA(7,22,9)

Structure Four
  pxxxr.i
  q4.i
  r4.i
EndStructure  
Define Five.Four

OpenWindow(5,0,0,100,100,"TEST",#PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_SizeGadget|#PB_Window_MaximizeGadget|#PB_Window_MinimizeGadget)
ButtonGadget(44,10,10,40,40,"Hi")
SetGadgetData(44, @Five)			;so DLC can access Five\q4, Five\r4 and Five\pxxxr
BindGadgetEvent(44, @DLC())

Five\pxxxr = @Two ;or, with different results, it might be @Three()
Two\OneA(1,2,3)\a1 = 4

Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow

Procedure DLC()
  
  Structure OneHelp
    Array OneB.One(0, 0, 0)
  EndStructure
  
  Protected.i *DLC_Six.Four
  Protected *DLC_Seven.OneHelp
  
  *DLC_Six = GetGadgetData(EventGadget())
  ;so now   *DLC_Six\pxxxr contains the address of the array structure Two
  *DLC_Seven = *DLC_Six\pxxxr
  Debug ArraySize(*DLC_Seven\OneB(), 2) 
  Debug *DLC_Seven\OneB(1,2,3)\a1
  
EndProcedure
It is a shame the resulting code looks rather strange, but I've had to write code in the past which has looked a lot worse!