Array Cloning and recursion

The place to discuss scripting and game modifications for X³: Terran Conflict and X³: Albion Prelude.

Moderators: Moderators for English X Forum, Scripting / Modding Moderators

Post Reply
HarryCx2
Posts: 3
Joined: Mon, 20. Dec 04, 15:10
x2

Array Cloning and recursion

Post by HarryCx2 » Sun, 11. Aug 13, 12:54

I'm probably way out of date here, but having been writing a few scripts and getting fed up with the <clone array> function not working as it suggests it should, I enhanced it.
This little script will recurse through an array cloning each array-type member it finds, resulting in a properly cloned array, fully independent of the original.

Just copy/paste into Exscriptor

The scriptname I used for this is hc.array.clone


* ------------------------------
* Clone the Source
$Count = size of array $Source
skip if $Count > 0
return null

dec $Count =
$Source.Array = clone array $Source : index 0 ... $Count
* ------------------------------
* Clone the $Source.Array: This will create disconnected copies of the non-array typemembers of the Original Source
* All we need to process then are the array-type members
$New.Array = clone array $Source.Array : index 0 ... $Count
* ------------------------------
inc $Count =
* ------------------------------

$Index = -1
while $Index < ( $Count - 1 )
inc $Index =

$This.Item = $Source.Array[$Index]
* ------------------------------
* Recurse for array-type members
if is datatyp[ $This.Item ] == {DATATYPE_ARRAY}
$New.Item = [THIS]->call script 'hc.array.clone' : Source=$This.Item
$New.Array[$Index] = $New.Item
end
* ------------------------------
end
return $New.Array

I hope this is useful to someone

Post Reply

Return to “X³: Terran Conflict / Albion Prelude - Scripts and Modding”