00:00
00:00
Newgrounds Background Image Theme

lbkush just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

So I'm trying to push values from my equipz array to inventory array, and then replacing equipz values with 0's. But for some reason inventory's values are also replaced with 0's and I am just not sure why. I feel it might be because of the push() method but I am still unsure. I don't think that should be an issue because I feel they are unrelated arrays. Any help would be appreciated, maybe there is a better way to do this so I could avoid this all together. Thanks.
Here's the code I'm using.

var inventory:Array = new Array()

var equipz:Array = new Array(
new Array("Brittle Sword","Weapon",1,10,5,1,1,1,4,1,0,"desc",0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0),
new Array(0,0,0,0,0,0,0,0,0,0,0,0,0))

if(var == 1){
_root.inventory.push(_root.equipz[0])
var = 2
trace(_root.inventory+"new array value")
}
if(var == 2){
for(i = 0;i < equipz[0].length; i ++){
_root.equipz[0][i] = 0
if(i == equipz[0].length - 1){
var = 3;trace(_root.inventory+"inventory altered array value)
}
}
}


O.o?

Response to Array issue 2014-12-25 07:38:58


At 12/25/14 05:51 AM, Inswivnia wrote: So I'm trying to push values from my equipz array to inventory array, and then replacing equipz values with 0's. But for some reason inventory's values are also replaced with 0's and I am just not sure why. I feel it might be because of the push() method but I am still unsure. I don't think that should be an issue because I feel they are unrelated arrays. Any help would be appreciated, maybe there is a better way to do this so I could avoid this all together. Thanks.

Arrays are passed by reference, not by value. A quick search gave this result, which should be of some use to you.

However, I'll take this opportunity to point you in the direction of object orientated programming.

The idea behind it is you'd create a Weapon class with properties and methods relating to all of your weapons, and then through the use of inheritance, create subclasses such as BrittleSword. This seems like a simple introduction to OOP, specifically in AS3.

Good luck.

Response to Array issue 2014-12-26 21:19:16


At 12/25/14 07:38 AM, Sam wrote:
At 12/25/14 05:51 AM, Inswivnia wrote: So I'm trying to push values from my equipz array to inventory array, and then replacing equipz values with 0's. But for some reason inventory's values are also replaced with 0's and I am just not sure why. I feel it might be because of the push() method but I am still unsure. I don't think that should be an issue because I feel they are unrelated arrays. Any help would be appreciated, maybe there is a better way to do this so I could avoid this all together. Thanks.
Arrays are passed by reference, not by value. A quick search gave this result, which should be of some use to you.

However, I'll take this opportunity to point you in the direction of object orientated programming.

The idea behind it is you'd create a Weapon class with properties and methods relating to all of your weapons, and then through the use of inheritance, create subclasses such as BrittleSword. This seems like a simple introduction to OOP, specifically in AS3.

Good luck.

Thank you Sam. I'm gonna learn more about OOP and AS3 really soon. I didn't know that arrays passed references instead of values. This is one of my earlier projects so im still getting used to things and it means a lot to e to have your help!


O.o?

Response to Array issue 2014-12-26 22:35:20


At 12/26/14 09:19 PM, Inswivnia wrote: I didn't know that arrays passed references instead of values.

Everything in ActionScript is passed by reference because everything in ActionScript is a reference. When you're storing arrays, you're really just creating an array and having a reference to it stored in the variable, and it's the same for literally any other object.

The only time you get anything like pass-by-value is with scalars (i.e. int, uint, Number and Boolean) and with strings, which are still passed by reference but instead have new references stored when they are altered, rather than changing the original reference.

Response to Array issue 2014-12-30 04:33:45


At 12/26/14 10:35 PM, Diki wrote:
At 12/26/14 09:19 PM, Inswivnia wrote: I didn't know that arrays passed references instead of values.
Everything in ActionScript is passed by reference because everything in ActionScript is a reference. When you're storing arrays, you're really just creating an array and having a reference to it stored in the variable, and it's the same for literally any other object.

The only time you get anything like pass-by-value is with scalars (i.e. int, uint, Number and Boolean) and with strings, which are still passed by reference but instead have new references stored when they are altered, rather than changing the original reference.

That makes since. My coding teacher talked about something before, he said somthing like: In code debuggers are great but they don't account for errors in programmer logic. I was stumped for the longest time because my code didn't tell me I was wrong. There's a lot more I wanna learn about so thank you Diki and Sam for the tips. I'm going to learn more about coding. So I appreciate you guys pointing me in the right direction.


O.o?