Jump to content

Returning an array from a user func.


Recommended Posts

Sorry, the manual is a bit unclear & i can't find an example of this anywhere...

Say i just want to return the result of a function as a couple of array keys:

msgbox(0, Blub[0] & Blub[1])

Func blub()
$crapa = "yay"
$crapb = "kewl"

Return $crapa;insert stuff to make it 2 array vars
Return $crapb
EndFunc
Link to comment
Share on other sites

ah i see, thank you :-D

yeah it was just this confusing sentence: "Arrays can be passed to functions (and returned from them) by simply using the array name without any brackets. Note that function declarations cannot appear inside other function declarations."

Edited by Smorg
Link to comment
Share on other sites

I added comments for my late example

; Declare and assign the array
Global $array[2]
$array[0] = 1
$array[1] = 2

; execute function to pass an array
$return = _function($array)

; Show returned results
If IsArray($return) Then
    For $i = 0 To UBound($return) -1
        MsgBox(0, $i, $return[$i])
    Next
EndIf

; ByRef will prevent a copy of the array being made for function use.
Func _function(ByRef $parameter)
    Return $parameter
EndFunc

:D

Link to comment
Share on other sites

yay works... good examples. This is what I was trying to do: (part of a very big include, returns nearest pixel color to an origin, rather than standard linear pixel search, centered over the middle of that color mass.)

;==========================================
;Radial pixel searching algorhythm ~Smorg
;Will perform a circular search pattern from an oragin ($x,$y)
;Returns 0 if none present, else 1 for success.
;==========================================
Func PixelFocusB()
;------------------User Variables----------------
$Radius = 1             ;starting radius from the scan oragin
$x = 400                      ;search origin
$y = 300                      ;search origin
;------------------------------------------------
Dim $XYBlock[2]
Dim $MaxRadius[4]
Dim $FinalCoord[2]
Dim $Corner = 0
Dim $Pixel = 0

$MaxRadius[0] = Sqrt(($x-$windowXmin)^2 + ($y-$windowYmin)^2);origin to upper left
$MaxRadius[1] = Sqrt(($x-$windowXmax)^2 + ($y-$windowYmin)^2);origin to upper right
$MaxRadius[2] = Sqrt(($x-$windowXmin)^2 + ($y-$windowYmax)^2);origin to lower left
$MaxRadius[3] = Sqrt(($x-$windowXmax)^2 + ($y-$windowYmax)^2);origin to lower right
$Corner = _ArrayMaxIndex($MaxRadius, 1, 0)

Do
  For $PiCount = 0 to (3.1415926*2) step $Resolution*((3.1415926*2)/(($Radius*2)*3.1415926))
  $XYBlock[0] = Round($x + ($Radius * (cos($PiCount))))
  $XYBlock[1] = Round($y + ($Radius * (sin($PiCount))))
    If $XYBlock[0] > $windowXmin AND $XYBlock[0] < $windowXmax AND $XYBlock[1] > $windowYmin AND $XYBlock[1] < $windowYmax Then
    $Pixel = pixelgetcolor($XYBlock[0], $XYBlock[1])
      For $MC = 1 to $MonstersColor[0]
        If $Pixel = $MonstersColor[$MC] Then
        Dim $XYBlock = MonsterCenter($XYBlock[0], $XYBlock[1], $MC)
        MouseMove($XYBlock[0], $XYBlock[1], $D2_FastIntMDelay)
        Return 1
        EndIf
      Next
    EndIf
  Next
  $Radius = $Radius + $Resolution
Until $Radius > $MaxRadius[$Corner]
Return 0
EndFunc

;==========================================================
;Will center the mouse over a selected block ~Smorg
;Final coordinates are returned as $XYBlock[0] & [1]
;==========================================================
Func MonsterCenter($x, $y, $color)
$CenteringResolution = 2      ;The resolution to use when scanning for the middle of the monster block
For $Left = $x to $windowXmin Step -($CenteringResolution)
$pixel = pixelgetcolor($Left, $y) 
If $pixel <> $MonstersColor[$color] Then ExitLoop
Next

For $Right = $x to $windowXmax Step $CenteringResolution
$pixel = pixelgetcolor($Right, $y)
If $pixel <> $MonstersColor[$color] Then ExitLoop
Next

For $Top = $y to $windowYmin Step -($CenteringResolution)
$pixel = pixelgetcolor($x, $Top)
If $pixel <> $MonstersColor[$color] Then ExitLoop
Next

For $Bottom = $y to $windowYmax Step $CenteringResolution
$pixel = pixelgetcolor($x, $Bottom)
If $pixel <> $MonstersColor[$color] Then ExitLoop
Next

$XYBlock[0] = $Left + (($Right - $Left)/2)
$XYBlock[1] = $Top + (($Bottom - $Top)/2)
Return $XYBlock
EndFunc
Edited by Smorg
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...