Help - Search - Members - Calendar
Full Version: Variable Shuffle [RESOLVED] thanks to Night5h4d3
RPG RPG Revolution Forums > Game Engines > RPG Maker XP Discussion
Gnarizard
SYNOPSIS:
I was trying to shuffle the contents of a range of variables, but couldn't figure out how, so I asked for help.
Night5h4d3 took the call, and progress was made toward the goal.
After posting back and forth, the problem was resolved, and my variables were successfully scrambled about.

END RESULT:
CODE
temp1 = []
temp2 = []
for i in 1..100
  temp1[i-1] = $game_variables[i]
end
temp2 = temp1.sort_by {rand}
for i in 0..temp2.size
  $game_variables[i+1] = temp2[i]
end

USAGE:
In your event, use the "Script..." command, copy/paste the above code,
then change "1..100" to whatever range of variables you want to shuffle.
(also, if you use this, you should probably credit Night5h4d3, it's only fair)

THANKS to Night5h4d3
for taking such a strange and badly-explained request, and for being patient with me until the problem was resolved.

ORIGINAL POST

Okay, so basically I want to store a value in a variable based on a random number of another variable...

For example...
I set Var 999 to be a random number between 1-100...
I want to check to see if Var 201 = 0, and if so, check to see if the Variable that Var 999 returned = 0, and if NOT,
then I want it to be set to whatever value is stored in the Variable that Var 999 returns,
and set the Variable that Var 999 returned to 0 (so it can't be used twice)...

So if 999 returns 73...
IF Var 201 = 0, then:
IF Var 073 DOES NOT = 0, then:
Var 201 = Var 073.

As you can see, the problem is getting a random variable between 1-100.
Also "Var 201" would also go up and up and up until I do this same process to Vars 201-300,
which is why I can't just do like 100 case branches, because then I'd have to do like 10,000 of them.

See how I'm having trouble? lol

I'm hoping for event, but if there's a script for that, I'd be more than willing to use it.
Night5h4d3
if your trying to do this with events (IE: not in-script) then use a call script with this:

CODE
if $game_variables[999] != 0
$game_variables[201] = $game_variables[$game_variables[999]]
end


edit - if you want to go from 201 to whatever you can use this:

CODE
for i in 201..300
if $game_variables[999] != 0
$game_variables[i] = $game_variables[$game_variables[999]]
end


you can replace 201 with the first variable to use, and 300 with the last.
Gnarizard
Actually, now that I think about it, is there any way to add all the variables from 1-100 into an array, shuffle them, and output them back into the original game variables?

That's really all I need to do.

Sorry. lol
Night5h4d3
I'm not sure what you mean by that, the list of variables is already stored in an array. Do you mean take say, variables 1, 2, 3 and 4, shuffling them (which would result in something like 3, 1, 2, 4) and then transferring them back to their position? (like the value for variable 3 becomes the value for 1, 1 becomes 2, 2 becomes 3, 4 stays 4?)
Gnarizard
QUOTE (Night5h4d3 @ Oct 20 2010, 10:00 AM) *
I'm not sure what you mean by that, the list of variables is already stored in an array. Do you mean take say, variables 1, 2, 3 and 4, shuffling them (which would result in something like 3, 1, 2, 4) and then transferring them back to their position? (like the value for variable 3 becomes the value for 1, 1 becomes 2, 2 becomes 3, 4 stays 4?)


Hey, yeah, that's exactly right! happy.gif
Sorry I'm so bad at explaining things, but that's absolutely what I'm looking to do!
Night5h4d3
I cant guarantee this will work, but try:

CODE
temp_arry = []
for i in 1..100
  temp_arry[i] = $game_variables[i]
end
temp_arry.sort {rand}
for i in 0..temp_arry.size-1
  $game_variables[i+1] = temp_arry[i]
end
Gnarizard
Doesn't seem to do anything... sad.gif
Night5h4d3
can you try this code and take a screenshot of what it returns?

CODE
temp_arry = []
for i in 1..100
  temp_arry[i] = $game_variables[i]
end
temp_arry.sort {rand}
print(temp_arry)
Gnarizard


It appears to return a string of each value of each variable from 1-60 (I'm only using 60 now)
(which I have set to be the value of each, so Var 1 is 1, Var 2 is 2, etc....)
Thus resulting in 1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17..... but without space in between.

If I had to guess, I'd say that the "temp_arry.sort {rand}" line wasn't working properly.
Night5h4d3
Okay, I think I've finally got it.. try this:
CODE
temp1 = []
temp2 = []
for i in 1..100
  temp1[i] = $game_variables[i]
end
temp2 = temp1.sort_by { rand }
for i in 0..temp2.size-1
  $game_variables[i+1] = temp2[i]
end
temp1 = temp2 = nil
Gnarizard
VERY close!

I changed this:
CODE
for i in 1..100

into this:
CODE
for i in 1..60

to shuffle 60 variables instead of 100...

I get all my values shuffled to different variables just fine,
but 1 number ends up in Var.61, and somewhere in the mix there is a 0,
which is not the value of any of my variables (1-60).

EDIT:
I think I fixed it!
No I Didn't
I fiddled with it for a while and got this to work:
CODE
temp1 = []
temp2 = []
for i in 0..2
  temp1[i] = $game_variables[i+1]
end
temp2 = temp1.sort_by { rand }
for i in 0..temp2.size-1
  $game_variables[i+1] = temp2[i]
end
temp1 = temp2 = nil


I changed
CODE
  temp1[i] = $game_variables[i]

into this:
CODE
  temp1[i] = $game_variables[i+1]

which seems to have gotten rid of that stray 0
(I'm guessing it was reading a string that came BEFORE the defined variables in-game)

and I changed:
CODE
for i in 1..100

into this:
CODE
for i in 0..99

which gets me the proper variables I guess, since variable 1 is actually variable 0 or something.

Yay for fiddling with things I don't understand!

NOTE: Please tell me if there's something horribly wrong with the code I came up with.
Night5h4d3
okay, i realized what I was doing wrong. normal arrays go from 0 to N, but with $game_variables it goes from 1 to N, which was leading to the extra number. This should work too:

CODE
temp1 = []
temp2 = []
for i in 1..100
  temp1[i-1] = $game_variables[i]
end
temp2 = temp1.sort_by {rand}
for i in 0..temp2.size
  $game_variables[i+1] = temp2[i]
end


It might be me, but with your variation, variable 100 (after the shuffle) kept returning 100 for me (3 times in a row)
Gnarizard
You're right, I wonder why it does that...
Oh well, your new code works great and is much more elegant than my code-monkeying. lol
Thank you ever so much!
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.