Combining X and Y Coordinates into one variable

Hey all,

I'm attempting to code a hookshot into my game. I'm currently getting a variable for the player's location X and the player's location y, as hookshot location x and hookshot location y. I will be using terrain ID and get location info for most of the coding, but that does not take into account of events that may be in the way. Because of this I am trying to implement a comparison of the event's location x and y and compare them.

I first tried comparing the event and hookshot's x and y variables, but it will not work properly unless the two are merged. For example, if the event's x and the hookshot's x = each other the code would not run properly, meaning there is an entire row and an entire column where the code won't run, even though I only want it for the one location.

Because of this, I would like to try to merge the two variables--simply adding them together won't solve anything because say for example you have x=1 and y=4, it would be the same as x=4 and y=1. Can anyone assist me with merging the variables? (Or maybe you've got another idea that would work?)

Here are the lines of code I am having issues with:
$gameVariables.value(11) == $gameVariables.value(9) + $gameVariables.value(10)
$gameVariables.value(12) == $gameMap.event(9).x + $gameMap.event(9).y
$gameVariables.value(11) != $gameVariables.value(12)

upload_2017-10-13_23-6-18.png


Thanks in advance for the help! 🙂
Is there a way to convert them into a string and compare that string? I don't know much about java scripting, but that shouldn't be too difficult I would think. Your string might be "x34y45" and then you could check both the axes with one variable. So in short convert both the x and y location numbers into a text string and combine them that way using a separator.
Yeah, that's what I meant 🙂 I guess I forgot to use the word "string", though. I had read that a simple + was supposed to merge two strings together, but it wasn't working.
Forgive my ignorance if I"m just not getting it, but is this what you need to do?
concat()
If you do want to combine them, you would need to set the variable via the Script command for control variable and for example save them as an array [X,Y]. Then you'd also use the Script part of the conditional branch to check them in one go

OR

. I had read that a simple + was supposed to merge two strings together, but it wasn't working.

It's because the values of $gameVariables are numbers by default and not strings, so using $gameVariables.value(v1) + $gameVariables.value(v2) would actually merge them mathematically, so basically add them via mathematical methods.. If you want to combine them as string, do this
String($gameVariables.value(v1)) + String($gameVariables.value(v2)) so that the game converts them to string first before combining them

Note: Change v1 and v2 to the ID of the variables that you need
If you do want to combine them, you would need to set the variable via the Script command for control variable and for example save them as an array [X,Y]. Then you'd also use the Script part of the conditional branch to check them in one go

OR



It's because the values of $gameVariables are numbers by default and not strings, so using $gameVariables.value(v1) + $gameVariables.value(v2) would actually merge them mathematically, so basically add them via mathematical methods.. If you want to combine them as string, do this
String($gameVariables.value(v1)) + String($gameVariables.value(v2)) so that the game converts them to string first before combining them

Note: Change v1 and v2 to the ID of the variables that you need


Yes, I think an array is probably what I need, actually. I was just thinking about it and the same issue I mentioned earlier could come about with combining them as strings. Example: x=1, y=12 = 112 would be the same as x=11, y=2 = 112.

Do you have any tips for setting up arrays? I can always google about them too 🙂
You could do

$gameVariables.set(ID,[Variable for X, Variable for Y]) so for your post,

$gameVariables.set(11,[$gameVariables.value(9), $gameVariables.value(10)])

or you could always just put a comma or something else in the string combination

String($gameVariables.value(9)) + "," + String($gameVariables.value(10))

That way, x=1,y=12 will be 1,12 while x=11,y=2 will be 11,2 so they won't be the same anymore
You could do

$gameVariables.set(ID,[Variable for X, Variable for Y]) so for your post,

$gameVariables.set(1,[$gameVariables.value(9), $gameVariables.value(10)])

or you could always just put a comma or something else in the string combination

String($gameVariables.value(9)) + "," + String($gameVariables.value(10))

That way, x=1,y=12 will be 1,12 while x=11,y=2 will be 11,2 so they won't be the same anymore

Cool, I will try that! Thank you!!!
You could do

$gameVariables.set(ID,[Variable for X, Variable for Y]) so for your post,

$gameVariables.set(11,[$gameVariables.value(9), $gameVariables.value(10)])

or you could always just put a comma or something else in the string combination

String($gameVariables.value(9)) + "," + String($gameVariables.value(10))

That way, x=1,y=12 will be 1,12 while x=11,y=2 will be 11,2 so they won't be the same anymore

It is working! However, I have come into another crossroads. I am trying to write a script to check for all the events in one call, but am trouble with the switch comparison. Can you help with my syntax?

((String($gameVariables.value(9)) + "," + String($gameVariables.value(10)) != String($gameMap.event(9).x) + "," + String($gameMap.event(9).y)) || ($gameSwitches.Value(35) === true))

This is a conditional script i am calling. I am trying to have it say if the variables 9 and 10 do not equal the event location OR if switch number 35 is true, it will run the rest of the code.

Thanks in advance! 🙂
Nvm!!!! I figured out what was causing the problem! Apparently the syntax does not allow a capital V in value, and when i switched it to a lowercase it is now working correctly 🙂

Original thread: https://forums.rpgmakerweb.com/threads/combining-x-and-y-coordinates-into-one-variable.85696/