Talk:Arrays (Papyrus)

Revision as of 15:41, 30 August 2012 by imported>JaxFirehart (Confirming that arrays are passed by value)

I appreciate that this page has been created because I never really understood this Arrays thing... but I still barely get it. I think that this page needs a rewrite and it needs to be dumbed down.

First, when you write something "for begginers" don't give them a long lecture about how computer memory works. I know that you wanted people to understand the whole concept but it's really hard for newbies. And if you do something like this please, simply try to avoid sentences like "...since you'd need to construct identifiers at runtime by concatenating the descripting name with the changing index". Sometimes I just didn't know what it was about. You're talking about arrays and then suddenly: "Arrays are, memory-wise, just a bunch of bytes stacked together, continuous in memory (virtual memory, that is, physical memory can get messy). From now on, I'll just refer to it as memory."

Second, when you get to the technical part of the page, scripts and all, could you make it more orderly? Like, start with how you define an array, then tell how to define its value, then how do some other stuff... I mean, get from the simplest stuff to the most complicated. Right now I think there's everything in this article but I still don't understand it. Try to incorporate new things gradually. I know that this isn't a tutorial but still, many people are going to learn from this. Domius 14:16, 17 February 2012 (EST)

For beginners who didn't major in CS

No offense meant to the person who wrote the For Beginners section, but I agree it's still far too technical for beginners. Here's my attempt.

Arrays are how programmers store lists of related things. Compare a normal person's grocery list:

Bread
Milk
Eggs
Bananas

Some people are a little more organized:

Grocery List
------------
1. Bread
2. Milk
3. Eggs
4. Bananas

Programmers have to be even more obsessive because computes are dumb and need every little detail written down.

Defining arrays

First the computer needs to know what kind of things will be in the list, that it will be a list, the name of the list.

FoodItem[] GroceryList

Declaring arrays

But many times we need to also tell the computer to make enough storage space to hold the number of items we plan to store.

FoodItem[] GroceryList = new Fooditem[4]

Setting individual values

Then the items are stored in the numbered locations, but it's easier for the computer to start counting at zero instead of one, and has to be reminded of which list every single time.

GroceryList[0] = "Bread"
GroceryList[1] = "Milk"
GroceryList[2] = "Eggs"
GroceryList[3] = "Bananas"

Checking the length

Finding out how many items are in the list is easy. (Actually it's how many slots are available, even if some are blank.)

GroceryList.length

Getting individual values

Getting the values back isn't bad as long as you remember that the computer starts counting at zero.

FoodItem TheThirdItem = GroceryList[2]

Creating functions that expect arrays

When you need to tell the computer to expect a list, you put the square brackets after the name of the type of items the list will hold.

Function BuyThese(FoodItem[] ShoppingListForToday)

Sending whole arrays to a function

But to do something with an entire list you would just use it's name.

BuyThese(GroceryList)

Examples of working with every item in the array

The real power of lists only appears when you need to do the same thing with every item in the list. We can loop through all of the items in the list by their index position.

Function BuyThese(FoodItem[] ShoppingListForToday)
   int itemNumber = 0
   while itemNumber < ShoppingListForToday.Length
      FindTheItemAndPutItInYourCart(ShoppingListForToday[itemNumber])
      itemNumber += 1
   endwhile
   GoThroughCheckoutAndPay()
EndFunction

Making it a little more like the game's context for tracking items will make it more complex. So programmers usually shorten those names and might even give the list a name that makes more sense based on how it's used just to compensate.

Function BuyThese(FoodItem[] item)           ; item is a list of fooditems
   int totalCost = 0                         ; we'll want to know this at the end
   int n = 0                                 ; start at the beginning
   while n < list.Length                     ; and go to the end of the list
      if TheStore.GetItemCount(item[n]) > 0  ; if item is in stock
         TheStore.RemoveItem(item[n], 1)     ; take one from the shelf
         TheCart.AddItem(item[n], 1)         ; put it in our cart
         totalCost += item[n].GetGoldValue() ; record the price
         item[n] = None                      ; mark it off of our list
      endif
      n += 1                                 ; go on to the next item
   endwhile
   GoThroughCheckoutAndPay()                 ; hopefully we'll have enough money
EndFunction

Other considerations

But passing that list into the BuyThese function really is like handing someone a list. If code inside that function changes or removes some of the items, then they are changed everywhere that list is referenced. We aren't making copies of the list, just letting different parts of the code see it (and some may call it different things, but it's the same list). So after we've called "BuyThese(GroceryList)" we'll be able to see if anything wasn't available.

   int i = 0
   while i < GroceryList.Length
      if GroceryList[i]  ; it wasn't changed to None
         Debug.Notification("We weren't able to buy the " + GroceryList[i].GetName())
      endif
      i += 1
   endwhile

Some special limitations are that each list can only have up to 128 items, you must use a literal number with the "new" command, and that you can't have an array of arrays.

--Cdcooley 23:24, 25 May 2012 (EDT)

I just wrote a function that passes in an array of forms and populates it with the currently equipped gear of the specified actor. When attempting to access the array outside of the function, it is still empty. To me this implies that arrays are passed by value and any changes do not stick outside of the function. I am a CS major and understand how arrays work, so I am fairly sure that this is not a misunderstanding.
Form[] property playerEquipArray auto;

Function GetEquipment(Actor akActor, Form[] akEquipList, int aiArraySize)

	int count = 0;
	Form tempSlot;
	
	tempSlot = akActor.GetWornForm(1);  This is an skse function that gets the worn form if given a slot
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)  ;Ensures that something was found and that it is not already in the array
		akEquipList[count] = tempSlot;  Assigns currently worn form to the current slot in the array
		count += 1;  Increments the array index
	endif
	tempSlot = akActor.GetWornForm(2);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	tempSlot = akActor.GetWornForm(4);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	tempSlot = akActor.GetWornForm(8);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	tempSlot = akActor.GetWornForm(16);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	tempSlot = akActor.GetWornForm(32);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	tempSlot = akActor.GetWornForm(64);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	tempSlot = akActor.GetWornForm(128);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	tempSlot = akActor.GetWornForm(256);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	tempSlot = akActor.GetWornForm(512);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	tempSlot = akActor.GetWornForm(4096);	
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)
		akEquipList[count] = tempSlot;
		count += 1;
	endif
	while (count < aiArraySize)
		akEquipList[count] = None;
		count += 1;
	endWhile

endFunction

Event OnActivate(ObjectReference akActionRef)
	GetEquipment(Game.GetPlayer(), playerEquipArray, 11);  Player is definitely wearing something when tested
	debug.messagebox(playerEquipArray[0].GetName());  Outputs no text
endEvent

--JaxFirehart (talk) 16:16, 30 August 2012 (EDT)

I modified this script as thus:
Form[] property playerEquipArray auto;

Form[] Function GetEquipment(Actor akActor)

	int count = 0;
	Form[] akEquipList = new Form[11];
	Form tempSlot;
	
	tempSlot = akActor.GetWornForm(1);  This is an skse function that gets the worn form if given a slot
	if (tempSlot != None) && (akEquipList.find(tempSlot) < 0)  ;Ensures that something was found and that it is not already in the array
		akEquipList[count] = tempSlot;  Assigns currently worn form to the current slot in the array
		count += 1;  Increments the array index
	endif

	;SNIP

	while (count < 11)
		akEquipList[count] = None;
		count += 1;
	endWhile

endFunction

Event OnActivate(ObjectReference akActionRef)
	playerEquipArray = GetEquipment(Game.GetPlayer());  Player is definitely wearing something when tested
	debug.messagebox(playerEquipArray[0].GetName());  Outputs "Shrowded Cowl" as expected
endEvent
It now works as expected, this confirms to me that arrays are passed by value unless someone can explain how I am wrong

--JaxFirehart (talk) 16:41, 30 August 2012 (EDT)

Return to "Arrays (Papyrus)" page.