Jump to content

Function passing Array persistence question


Recommended Posts

Hi,

 

I have a function passing an array.

 

Here is a shorter version of what I'm doing:

Armor[] Container0
Armor[] Container1
Armor[] Container2
;
Event OnQuestInit()
   Container0 = New Armor[10]
   Container1 = New Armor[10]
   Container2 = New Armor[10]
EndEvent
;
Function ChooseArray(Int Whatever)
   If Whatever == 0 
	ModifyThisArray(Container0)
   ElseIf Whatever == 1
	ModifyThisArray(Container1)
   ElseIf Whatever == 2
	ModifyThisArray(Container2)
   EndIf	
EndFunction
;
Function ModifyThisArray(Armor[] thisContainer)
   thisContainer[0] = myWhatever as Armor
EndFunction

When later on a function reads for example Container0 array, it's empty. I was thinking that when passing an array as argument, it would be a reference to the array and only the reference would be lost when the function ends.

 

Note: If I make 5 functions ModifyThisArrayX modifying ContainerX without passing the array, it works fine. But I would prefer to have only one function and understand why it's not working.

Edited by F4ll0uta
Link to comment
Share on other sites

Not sure what are you trying to accomplish (context would help) but maybe this:

; inserts theArmor to theContainerArray at ElementIndex
Function SetArmorInArrayAt(Armor theArmor, Armor[] theContainerArray, Int ElementIndex)
	If !theArmor || !theContainerArray || ElementIndex < 0 || ElementIndex >= theContainerArray.Length
		Return None
	EndIf
	theContainerArray.Insert(theArmor, ElementIndex)
EndFunction
Link to comment
Share on other sites

I guess my question is,

 

A. I declare and initialize an array (Container0) so that all function have access to it.

B. From a function I pass the array (Container0) as reference named (thisContainer) to another function that will modify his content.

C. Later on, an event called function reads the stored information in the Array Container0.

 

I think I should see the changes made to the reference array (but I'm not sure), but for my the array is blank. It didn't kept the changes.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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