Add 1(numeric) or more to all values in array

rad.noire

Veteran
Veteran
Joined
Mar 19, 2017
Messages
34
Reaction score
6
First Language
English
Primarily Uses
RMVXA
hi, i think it's a basic question, but still

so here, i have a variable array
V[67]
as you can see, i want to add all the values in variable array V[67] with + 1

i make a parallel event to check:
if V[67][0] = 1?
and if yes, it self switch A= on, if not, will not do anything

but not just one event,
there are another event like
if V[67][1] = 1?
if V[67][2] = 1?

and so on.

and at one point, i want to make all values in variable array V[67] to become 1 since the default is 0
i tried to make it with the default system control variables with constant value = 1
and when i tried to make $game_values[67][0] = 0 the output is,
error var.JPG

and as far as i know, this is because the game only know V[67] as single variable but not array.

Long story short,
1. anyone know how to add a numeric value to all values in variable array?
2. anyone know how to use loop in script call?

thank you

Edit: i tried to use this script call
my_array = [0]
for i in 1..(30)
my_array << 1
end
$game_variables[67] = my_array

well i'm just messing around with it, and still doesn't work out
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
In your edit you are creating an array with the first element set to 0. You then proceed to push 29 1s into the array (the first element is still 0). You could fix that problem by using an empty array and then starting the loop from 0 instead of 1. Here's another way of doing it:
Code:
# Create an array with 30 elements all equal to 0
$game_variables[67] = Array.new(30, 0)
# add one to all elements in the array
$game_variables[67].map! {|x| x + 1}
Also note that your if statements need two equals not one.
so:
Code:
if $game_variables[67][0] == 1
 # do something
else
  #do something else
end
Also one final note if I remember correctly; defining a variable inside a script call will mean that variable will not be found outside of the script call. which will give you undefined errors. This can be avoided by simply using $game_variables to hold whatever information you want.
 

dbchest

Beast Master
Veteran
Joined
Oct 1, 2013
Messages
434
Reaction score
300
First Language
English
Primarily Uses
RMMV
in my opinion, the best way to approach an objective like this is to extend the default functionality of the class you want to modify from within the .js file itself (create additional functions within the class to perform the work). after the additional functionality has been programmed into the class, performing the function via script call becomes trivial.

example
Code:
Game_Variables.prototype.setValueAll = function(value) {
    for (var i = 0; i < this._data.length; i++) {
        this.setValue(i + 1, value);
    }
};

a script call to perform this task then becomes routine.
Code:
$gameVariables.setValueAll(1);
i recommend grabbing your favorite drink and sitting down in front of the cpu and staring at the example above until it makes PERFECT sense. i can only say from experience that learning to code js for yourself is a very rewarding experience, but it takes real time, real effort trying to solve problems. you can do it though.
 
Last edited:

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
@dbchest I disagree with that approach for this particular issue. What he wants is to store an array in the $game_variables array and change only that array (not the entire $game_variables array). Also note that this question was for Ruby not JavaScript.
 

dbchest

Beast Master
Veteran
Joined
Oct 1, 2013
Messages
434
Reaction score
300
First Language
English
Primarily Uses
RMMV
@Sarlecc
i am referring to the approach; i did not mean to imply that the EXAMPLE i posted above was the exact code needed to solve the problem; if it was interpreted that way you have my apologies. i did not take note that this was for RGSS application however, but in the end that doesn't really have an effect either as the theory is the same. RGSS or .js, extending the functionality of the class where the data you want to modify exists IS the best way to add new functionality to that class. it also makes script calls to perform those functions at any time extremely simple.

do we agree on that, at least?
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
Generally yes, however in certain cases I feel it is unneeded (for example only saving me a few characters of typing) :).
For example the code I posted could also be done this way:
Code:
class Game_Variables

def incArrayAll (variableId, i = 1)
    @data[variableId].map! {|x| x + i}
end

end
However you would still need to set the $game_variable id to an array first and then call that method which is around the same length as doing it the other way. Which is why I feel that it would be unnecessary to add onto the Game_Variable class in this case.
Anyway I wanted avoid confusion for others who might read (sorry if it sounded harsh as that wasn't my intent). ;)
 

dbchest

Beast Master
Veteran
Joined
Oct 1, 2013
Messages
434
Reaction score
300
First Language
English
Primarily Uses
RMMV
in my experience, creating a foundation for extended functionality will typically save you work in the long run, however, you will of course encounter some scenarios where this may not be the case (our current situation), but i think it is important to note that providing that foundation still yields more pros to cons, even in these scenarios, and i have taught myself to try hard to follow best practice when writing my code, both for myself and the individuals using it.

some pros include:
legibility, greater control, room for the unknown, easier to re-connect with after a hiatus.

some cons include:
you may end up writing one or two more lines of code. :p

i think we can agree to disagree on this one, but you're definitely, not wrong!
 

rad.noire

Veteran
Veteran
Joined
Mar 19, 2017
Messages
34
Reaction score
6
First Language
English
Primarily Uses
RMVXA
i do agree with @dbchest , it would be good for a long run if i used your method, but with a level of knowledge i have now, i think i will go with @Sarlecc , since i create a simple task and a simple method is what i need right now. But really, thanks both of you. It's great help and new knowledge for me. Thank you.
 

dbchest

Beast Master
Veteran
Joined
Oct 1, 2013
Messages
434
Reaction score
300
First Language
English
Primarily Uses
RMMV
either way, i am sure @Sarlecc is as pleased as i am to know that your problem is solved!
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
454
Reaction score
209
First Language
English
Primarily Uses
RMMV
Yep! :)
Also was looking further in the docs and there is yet another method that could be used to do this (which is also extremely easy to read):
Code:
#fills the entire array with the value

$game_variables[id].fill(value)

#note* though that this is an overwrite not an increment
#meaning that if you fill an array that has 10 elements with 1
#then fill it again with 1 all 10 elements will equal 1
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Potato The game! Few concepts for characters to my game.
Well, well, well, well....Lemme get a look at this fanart.
The Google Play rating for one of my games finally rose above 4.0 after getting review-bombed from a $0 sale. Now the game's sales are doing much better when discounted, so that's nice.
I'm getting close to releasing a demo. Working on formatting my Games in Development post.
I always see a few people asking for C# in next engine and I always thought: Do I not know enough about that language to tell it will affect extensibility and customizability to the negative? Watching a video from SumRndmDde today I feel confirmed.

Forum statistics

Threads
98,226
Messages
950,523
Members
129,527
Latest member
ShadyPlar
Top