Photo arrays?

Monkey Forums/Monkey Programming/Photo arrays?

ewancordiner(Posted 2017) [#1]
Hi I am programming a clicker style game (think Cookie Clickers) and I want to be able to upgrade the product that I am clicking. To do this I think it would be most efficient if I had an array so that it would make it easier to save, however is there a way to do all of these items in an array with the images or set them as variables and then put them in. Sorry if this is difficult to understand, hope you can help :)


Xaron(Posted 2017) [#2]
So basically you want to use an array of images, right?

Something like:
Global photos:Image[]

photos = New Image[5]
photos[0] = LoadImage( "img1.png" )
photos[1] = LoadImage( "img2.png" )
photos[2] = LoadImage( "img3.png" )
photos[3] = LoadImage( "img4.png" )
photos[4] = LoadImage( "img5.png" )



dubbsta(Posted 2017) [#3]
@Xaron how/why is the "new" used there? what does that do?
edit: and do you have to use "new" when you call the index?


Simonsuuri(Posted 2017) [#4]
as much as i understand
photos = New Image[5]

means basicly
"empty photos array" = New "ImageArray"["lenght of array"]
(so at this point there is Image array with length of 5 with "empty images")

i ques you can also use (might propably just work with Int/String/Float arrays tho)
 
photos = photos.Resize(5)
or 
photos = Array.Resize(5)  
or "wild one" (works with Ints/String/Floats) 
photos = [LoadImage("img1.png"), LoadImage("img2.png"), LoadImage("img3.png"), LoadImage("img4.png"),LoadImage("img5.png")]



dubbsta(Posted 2017) [#5]
I get that part, but wondering about "new" what's the difference with and with out the"new" part. I understand it in a constructor method but how does it apply in a different situation like this, is it similar to constructors but how?


ewancordiner(Posted 2017) [#6]
Thankyou, I see what you mean. I'm thinking about adding upgrades into my game so that when the user has enough money they can upgrade to the next product which would get them more per click, and I wanted to implement some sort of saving into the game so that if the user went off the game and then came back on then they would be able to still have the same product if you get me? I thought that this would be the most efficient method of adding it to a save state.


Jesse(Posted 2017) [#7]
"new" practically means "allocate memory for"
in this case:
photos = New Image[5]

this tells the program to allocate memory for an array of five elements.

when you "New" an object instance, you allocate memory for the instance of the object.


dubbsta(Posted 2017) [#8]
so new means you are making a new slot so each new image would be referenced individually, where as without new you could only reference the same image? ive been sort of confused on this and would really love to understand this fully.