New account registration is temporarily disabled.

RMXP: SWAP VARIABLE VALUES (SOLVED)

Posts

Pages: 1
NeverSilent
Got any Dexreth amulets?
6299
Seeing how I am completely inexperienced when it comes to scripting, I need to ask your help with a probably very simple operation: is there a simple script command that allows me to swap the stored values of a larger batch of variables in RPG Maker XP?

An example: I want to swap variables 21 to 30 with variables 41 to 50. So after the operation, variable 21 should have the value that was previously stored in variable 41 and vice versa; variable 22 would become what 42 was before and vice versa; etc. etc.

I know that doing this is possible purely via event commands. But since it would require an enormous amount of those and I would be using this option often, I thought it wouldn't hurt to find an alternative and avoid tedious work and the risk of lags.
If anyone would be able to help me here, I'd be very grateful.
Hi NeverSilent,

Try this:


def variable_swap(start,end,destination)
var_start = start
var_end = end
dest = destination
for i in var_start..var_end
clipboard = $game_variables[dest]
$game_variables[dest] = $game_variables[i]
$game_variables[i] = clipboard
dest += 1
end
end

This will work if all of the variables are consecutive.

I'm not sure where you'd be able to paste that, maybe inside the default Interpreter scripts. Then you'd be able to run the code just by typing, say, "variable_swap(21,30,41)" into the Script event command window.
Marrend
Guardian of the Description Thread
21806
I'd go with something even simpler. Like...

def var_sawp(var1, var2)
  temp1 = var1
  temp2 = var2

  var1 = temp2
  var2 = temp1
end

With this, I'm pretty sure you can toss just about anything into "var1" and "var2", and have the values switched. I'm pretty sure, anyway. If $game_variables[21] and $game_variables[30] (your first example) are passed into this, and they are not flipped, let me know.

The event commands to test this might look something like...
MESSAGE BOX:
\v[21]
\v[30]

SCRIPT:
var_sawp($game_variables[21], $game_variables[30])

MESSAGE BOX:
\v[21]
\v[30]

As for where the script physically goes, I dunno. Somewhere under the "Materials" section, like you're "supposed" to?

*Edit: Crap, I didn't catch that you wanted to flip a range of variables, for some reason. What I did was a single swap. Though, it can still work within a loop. It just doesn't loop on it's own. You're probably better off with Zachary_Braun's code.

Way to fail with reading, Sataro. Way. To Fail.

*Edit2: Also, I must terribly confused. Why else would I mention putting scripts "under the Materials section" when RPG Maker XP does not have a "Materials" section!
SunflowerGames
The most beautiful user on RMN!
13323
You might be able to event it. You'll need 4 variable to swap 2 variables.

Variable 1 = 30

Variable 2 = 10

Variable 3 = 0

Variable 4 = 0

Event:

Set Variable 3 = Variable 1 and Set Variable 4 = Variable 2.

Then multiple Variable 1 and 2 by 0.

Then set Variable 1 = Variable 4 and set Variable 2 = Variable 3.

Don't know 100% if this will work :)
NeverSilent
Got any Dexreth amulets?
6299
Hey, thanks a lot, you all. I decided to try out Zachary_Braun's code and tried inserting it into the script editor in various different places. But of course, on every attempt all I managed to produce was a "sytax error".
So, could somebody be so nice to explain to me where exactly I should put that code and how to arrange it? (As far as I know, the spacing in scripts is Kind of important?) Sorry for being so clueless.

@Marrend: Thanks for the effort, even if your suggestion is not exactly what I was looking for. I really appreciate it.

@kory_toombs: Thanks for the idea. In fact, it's what I initially wanted to do as well. (It's even possible to swap 2 variable values using just 3 variables, the 3rd one being sort of a temporary store.) However, since that will also only swap 2 variables at once and I'll probably need to do it often and with large ranges of variables, that would mean having to use use dozens of event commands per operation - and they have to be different every time.
Try this more convenient version below, with the Class added. Now you can Insert it as its own script, and just change the Class name, trying to see which one is the magic name. It's currently labeled Game_Events. I know it has to be one of those Classes around there, somewhere, for you to be able to access it by typing its name into the Script box.


Class Game_Events


def variable_swap(start,end,destination)
var_start = start
var_end = end
dest = destination
for i in var_start..var_end
clipboard = $game_variables[dest]
$game_variables[dest] = $game_variables[i]
$game_variables[i] = clipboard
dest += 1
end
end

end


Spacing is only important for some other programming languages. Ruby will tolerate this unformatted-looking code (the looks of which I'd be happy to improve, if I knew the "[code ]" name here on this site).
Using the script editor, add the following as a new script just above the 'Main' script.

class Game_Variables
  attr_reader :data
end


Once you've done that, you can use a 'Script...' event command and enter something like the following:

v = $game_variables.data
v[21..30], v[41..50] = v[41..50], v[21..30]


Which would swap variables 21 through 30 and 41 through 50 inclusive.
NeverSilent
Got any Dexreth amulets?
6299
Zachary_Braun, thank you so much! It took me a bit of experimenting, but it works perfectly now. The class needed is the "Interpreter" class, and I had to replace the word "end" in the first and third line of the code, because the editor interpreted those as a command (blue text). Now that that's done, it does its job with no problems.
You helped me a lot. Thanks again!

Edit: Oh dear, sorry pete_mw. I hadn't seen your post before I wrote this reply. Thanks for the help, though, I'll try out that script as well. You guys are awesome!
BTW, Marrend, I hate to be the bearer of bad news, but your function won't do anything at all, no matter what values you pass to it.

Your problem is that if you call:

var_swap($game_variables[32], $game_variables[33]


Then your function doesn't know that it's getting the 32nd and 33rd game variables. All it gets are their values. It can do whatever it wants with its own local variables, but local variables are only meaningful within the context of the function that defines them.
Jeroen_Sol
Nothing reveals Humanity so well as the games it plays. A game of betrayal, where the most suspicious person is brutally murdered? How savage.
3885
author=kory_toombs
You might be able to event it. You'll need 4 variable to swap 2 variables.

Variable 1 = 30

Variable 2 = 10

Variable 3 = 0

Variable 4 = 0

Event:

Set Variable 3 = Variable 1 and Set Variable 4 = Variable 2.

Then multiple Variable 1 and 2 by 0.

Then set Variable 1 = Variable 4 and set Variable 2 = Variable 3.

Don't know 100% if this will work :)

For two variables, three is enough.

Variable 1 = x
Variable 2 = y
Variable 3 = 0

Variable 3 = Variable 1
Variable 1 = Variable 2
Variable 2 = Variable 3
Variable 3 = 0

However, for large numbers of variables, I wouldn't have any idea how to swap values efficiently.
author=NeverSilent
...I had to replace the word "end" in the first and third line of the code, because the editor interpreted those as a command (blue text).



I totally didn't catch that, sorry about that. But I'm glad it works now. If you ever need it, Pete's concise code should work, too. He knows his stuff. He helped me make my own text engine once.
NeverSilent
Got any Dexreth amulets?
6299
Sorry for the late reply. Zachary, you really don't have to apologize for trying to help me - I'm glad already you even made such an effort on my behalf.

I ultimately decided to use pete_mw's script because it is simpler and more flexible, but of course I will credit you both in any game I use this function in. Thanks again, guys.
author=Marrend
def var_sawp(var1, var2)
  temp1 = var1
  temp2 = var2

  var1 = temp2
  var2 = temp1
end

Prettier:

def var_sawp(var1, var2)
  var1, var2 = var2, var1
end

EDIT : My bad, sorry, pete_mw has already proposed this syntax...

author=pete_mw
v = $game_variables.data
v[21..30], v[41..50] = v[41..50], v[21..30]
Pages: 1