Difference between revisions of "Talk:Arrays (Papyrus)"
imported>Domius (Created page with "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 ne...") |
imported>Cdcooley (→For beginners who didn't major in CS: new section) |
||
Line 4: | Line 4: | ||
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. [[User:Domius|Domius]] 14:16, 17 February 2012 (EST) | 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. [[User:Domius|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: | |||
<source lang="text"> | |||
Bread | |||
Milk | |||
Eggs | |||
Bananas | |||
</source> | |||
Some people are a little more organized: | |||
<source lang="text"> | |||
Grocery List | |||
------------ | |||
1. Bread | |||
2. Milk | |||
3. Eggs | |||
4. Bananas | |||
</source> | |||
Programmers have to be even more obsessive because computes are dumb and need every little detail written down. | |||
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. | |||
<source lang="papyrus"> | |||
FoodItem[] GroceryList | |||
</source> | |||
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. | |||
<source lang="papyrus"> | |||
FoodItem[] GroceryList = new Fooditem[4] | |||
</source> | |||
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. | |||
<source lang="papyrus"> | |||
GroceryList[0] = "Bread" | |||
GroceryList[1] = "Milk" | |||
GroceryList[2] = "Eggs" | |||
GroceryList[3] = "Bananas" | |||
</source> | |||
Finding out how many items are in the list is easy. (Actually it's how many slots are available, even if some are blank.) | |||
<source lang="papyrus"> | |||
GroceryList.length | |||
</source> | |||
Getting the values back isn't bad as long as you remember that the computer starts counting at zero. | |||
<source lang="papyrus"> | |||
FoodItem TheThirdItem = GroceryList[2] | |||
</source> | |||
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. | |||
<source lang="papyrus"> | |||
Function BuyThese(FoodItem[] ShoppingListForToday) | |||
</source> | |||
But to do something with an entire list you would just use it's name. | |||
<source lang="papyrus"> | |||
BuyThese(GroceryList) | |||
</source> | |||
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. | |||
<source lang="papyrus"> | |||
Function BuyThese(FoodItem[] ShoppingListForToday) | |||
int itemNumber = 0 | |||
while itemNumber < ShoppingListForToday.Length | |||
FindTheItemAndPutItInYourCart(ShoppingListForToday[itemNumber]) | |||
itemNumber += 1 | |||
endwhile | |||
GoThroughCheckoutAndPay() | |||
EndFunction | |||
</source> | |||
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. | |||
<source lang="papyrus"> | |||
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 | |||
</source> | |||
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. | |||
<source lang="papyrus"> | |||
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 | |||
</source> | |||
Two special limitations are that each list can only have up to 128 items and that you can't have an array of arrays. | |||
--[[User:Cdcooley|Cdcooley]] 23:07, 25 May 2012 (EDT) |
Revision as of 22:07, 25 May 2012
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.
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
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]
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"
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 the values back isn't bad as long as you remember that the computer starts counting at zero.
FoodItem TheThirdItem = GroceryList[2]
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)
But to do something with an entire list you would just use it's name.
BuyThese(GroceryList)
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
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
Two special limitations are that each list can only have up to 128 items and that you can't have an array of arrays.
--Cdcooley 23:07, 25 May 2012 (EDT)