Joined
Sep 9, 2015
Messages
297
Reaction score
29
First Language
English
Primarily Uses
Hi! :D

Got a quick question about arrays in a definition for example:

def test_onevalue += 2value2 += 3@my_array_test = Array.new@my_array_test2 = Array.new@my_array_test.push(value)@my_array_test2.push(value2)msgbox(@my_array_test.size)msgbox(@my_array_test.size2)endDoes instance arrays gets cleared after the definition is called? I'm calling the same definition three times and it keeps coming back the same array size. Shouldn't the array supposed to get longer as I keep calling the definition?
 

Shaz

Global Moderators
Global Mod
Joined
Mar 2, 2012
Messages
41,861
Reaction score
14,510
First Language
English
Primarily Uses
RMMV
I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.

No, because you are instantiating a new object each time you call it

@my_array_test = Array.new@my_array_test2 = Array.new
This is overwriting the old array with a new array each time.

Also what is value?  That is an instance variable, so will not keep increasing. I'm a bit surprised it doesn't throw an error, but += must revert to = if the object doesn't exist.
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,965
Reaction score
3,068
First Language
French
Primarily Uses
RMMV
I find actually really weird to use Array.new....

why not just using 

"[ ]" this far more clear and more easier in my opinion :/
 

Engr. Adiktuzmiko

Chemical Engineer, Game Developer, Using BlinkBoy'
Veteran
Joined
May 15, 2012
Messages
14,682
Reaction score
3,005
First Language
Tagalog
Primarily Uses
RMVXA
It doesn't throw you an error? value is a local and never defined beforehand so it should be sending some kinda error.

And yeah, it's resetting because you're making a new array every time that method is called.

If you want the array to keep increasing, you'd want to do something like

Code:
class Test    def initialize    @my_array_test = Array.new    @my_array_test2 = Array.new  end    def test_one    value = 2    value2 = 3    @my_array_test.push(value)    @my_array_test2.push(value2)    msgbox(@my_array_test.size)    msgbox(@my_array_test.size2)  endend
or if you want value to be kept and increase over calls of test_one

Code:
class Test    def initialize    @my_array_test = Array.new    @my_array_test2 = Array.new    @value = 0    @value2 = 0  end    def test_one    @value += 2    @value2 += 3    @my_array_test.push(@value)    @my_array_test2.push(@value2)    msgbox(@my_array_test.size)    msgbox(@my_array_test.size2)  endend
 
Last edited by a moderator:
Joined
Sep 9, 2015
Messages
297
Reaction score
29
First Language
English
Primarily Uses
I'm so sorry guys! I didnt find this till now but thank yall for yalls inputs. XD

Solved and close down plz and ty all /bow XD
 

Latest Threads

Latest Profile Posts

Kind of relieved that I had medibang installed when I need to edit Sprite I was about to download gimp but I remember I had medibang installed lol
Voting for the first round of the Map Madness Tournament is now live! Vote for your favorite in each matchup!

Finally started working on Grim Trigger episode 2 today :LZScat: it's half comic/half visual novel!

GfY3Rwj.png
Is it interesting to get more backstory on villains? A key moment in their past, their childhood, or maybe something that shows they've always been that way? To see or not to see?
"Everything tastes like chicken until it's chicken, then it doesn't taste like chicken."
Context: chicken samosas do not taste like chicken. I thought it was veggie samosas.
Just another ordinary evening.

Forum statistics

Threads
110,366
Messages
1,052,632
Members
143,401
Latest member
Nazgramaria
Top