COUNTING SWITCHES?

Posts

Pages: 1
JayjeAthravein
Old-School GAM-MAKster DOUBLE DONATOR SUPREME!
1945
I'm trying to increase a certain variable's value by 1 every time a certain range of variables is turned on. but i get a ridiculous number every time i use this scriptlet:

keys = (0001..0004).to_a

keys.each { |id|
$game_variables[0001] += 1 if
$game_switches[id] == true}


any clue where i went wrong? any thoughts would be appreciated?
Craze
why would i heal when i could equip a morningstar
15170
It's checking literally every single real number that it can reach with whatever RGSS3's float max is, so 1.0, 1.1, 1.01, 1.001, etc.

keys = [1, 2, 3, 4]; i = 0
keys.each { |id|
i += 1 if $game_switches[id]}
$game_variables[1] += i

(That should fit in the little script box within events I assume you're using from the formatting in the original post.)

EDIT: should mention that if boolean assumes the "== true"; saves time/looks nicer imo! Works for any form of boolean, whether it's currently a variable or a switch or a method (like if $game_party.alive_members.empty?).

EDIT2: I hope this post doesn't scare you away from num..num! It's useful with for loops and whatnot when you're checking for integers (var += n when i == 5, for example)... but here it's screwing you over. =)
JayjeAthravein
Old-School GAM-MAKster DOUBLE DONATOR SUPREME!
1945
author=Craze
It's checking literally every single real number that it can reach with whatever RGSS3's float max is, so 1.0, 1.1, 1.01, 1.001, etc.

keys = [1, 2, 3, 4]; i = 0
keys.each { |id|
i += 1 if $game_switches[id]}
$game_variables[1] += i

(That should fit in the little script box within events I assume you're using from the formatting in the original post.)

edit: i'm using rmxp(RGSS)

EDIT: should mention that if boolean assumes the "== true"; saves time/looks nicer imo! Works for any form of boolean, whether it's currently a variable or a switch or a method (like if $game_party.alive_members.empty?).

EDIT2: I hope this post doesn't scare you away from num..num! It's useful with for loops and whatnot when you're checking for integers (var += n when i == 5, for example)... but here it's screwing you over. =)

thanks! but what if i was looking to use more than just 4 switches?
say like 20 or fifty?
EDIT:I'm using RGSS
Craze
why would i heal when i could equip a morningstar
15170
Set your keys like you did in the original post, and then run a bit of code like this:

temp_array = []
for key in keys do temp_array.push(key.to_i) end
temp_array.uniq!
keys = temp_array

This will turn each number into an integer, and then kill all extra entries in the array. You'd do the switch-checking block after this.
JayjeAthravein
Old-School GAM-MAKster DOUBLE DONATOR SUPREME!
1945
so together it would look like this?

keys = (0001..0004).to_a

keys.each { |id| 

        $game_variables[0001] += 1 if  

        $game_switches[id] == true}


temp_array = []
for key in keys do temp_array.push(key.to_i) end
temp_array.uniq!
keys = temp_array


JayjeAthravein
Old-School GAM-MAKster DOUBLE DONATOR SUPREME!
1945
i tried what you said and got an error. did i miss a step?
As far as I can see, the correct code would look like this:

keys = (0001..0004).to_a
temp_array = []
for key in keys do temp_array.push(key.to_i) end
temp_array.uniq!
keys = temp_array


keys.each { |id| 

        $game_variables[0001] += 1 if  

        $game_switches[id] == true}
http://www.ruby-doc.org/core-1.9.3/Range.html

Why do you need an array of id values, if you use Range to construct that array? wouldn't it be faster, cleaner, and more efficient to directly loop in range? I tested this code below in RGSS1 (If I am not mistaken, you also use this version (rpg maker XP))
for id in 0..3 # change this value accordingly.
  if $game_switches[id] == true
    # no need for triple zero.
    $game_variables[1] += 1
  end
end
If you need to check different ID values, you should just create that array manually. Constructing an array of range is just pointless.
# Construct array manually
keys = [1,7,156] # ID values we need to check

for id in keys # loop in the keys array
  if $game_switches[id] == true
    # no need for triple zero.
    $game_variables[1] += 1
  end
end
The (0..3).to_a should give you an array with 4 elements, no floating numbers. So the "full" code should be this ( ONLY if you want to push additional values to the array. If you don't want this "feature", use the first approach I posted)
keys = (0..3).to_a
keys.push(7) # add number 7 to the keys. Now its size is 5.

for id in keys # loop in the keys array
  if $game_switches[id] == true
    # no need for triple zero.
    $game_variables[1] += 1
  end
end

Edit: I dislike putting 0's before actual numbers, so the 1 in $game_variables does the same thing as 0001. Sorry if you already knew that ^^
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
You could even eliminate another few lines since there's only one statement within the if and just do
"$game_variables[1] += 1 if $game_switches[id]"
Yeah I knew that, I just prefer the "python like syntax" :D sorry for that, you should definitely use the shortened version :)
Trihan
"It's more like a big ball of wibbly wobbly...timey wimey...stuff."
3359
I'm probably just overly concerned with making as short a script as possible, but I can also see the merits of breaking things up for improved readability. :)
JayjeAthravein
Old-School GAM-MAKster DOUBLE DONATOR SUPREME!
1945
author=Scalytank
http://www.ruby-doc.org/core-1.9.3/Range.htmlWhy do you need an array of id values, if you use Range to construct that array? wouldn't it be faster, cleaner, and more efficient to directly loop in range? I tested this code below in RGSS1 (If I am not mistaken, you also use this version


I need a variable that counts a certain array of switches and when it reaches a determined number, it turns on another switch. it would be easier than using a bunch of condition forks. ;(
Use this version, if the switches you need to count are next to each other (like from 0 to 20)
$game_variables[1] = 0 # Reset the value
for id in 0..20 # change this value accordingly.
  $game_variables[1] += 1 if $game_switches[id] == true
end
desired_value = 8 # The number of switches you need, to advance variable 1000.
$game_variables[1000] += 1 if $game_variables[1] >= desired_value

Use this version, if the switches you need to count are NOT next to each other (like, switch 2, switch 17, switch 145 etc...)
$game_variables[1] = 0 # Reset the value
keys = [2,17,145] # List numbers like this
for id in keys # this will loop throught the keys array
  $game_variables[1] += 1 if $game_switches[id] == true
end
desired_value = 2 # The number of switches you need, to advance variable 1000.
$game_variables[1000] += 1 if $game_variables[1] >= desired_value

IF you only want to turn on switches, not to count this, replace this line
$game_variables[1000] += 1
with this
$game_switches[1000] = true
JayjeAthravein
Old-School GAM-MAKster DOUBLE DONATOR SUPREME!
1945
THANKS SCALYTANK. THAT REALLY DID THE TRICK! I had a feeling it was a quick fix, just couldn't figure it out.
Pages: 1