Looking for a way to scale a variable within a specific range

Hi there, hopefully somebody can help on this one - been trying all day and I think I'm at the point where my brain is about to melt 😛


So in our game we are storing the Players X and Y co-ordinates every 60 frames. These are stored into variables 26 and 27 respectively.


What I am in need of is a way of scaling the X co-ordinate (variable 26) as a number within the range 0 to 1024 (it can be a whole number)


And the Y co-ordinate (variable 27) scaled as a number within the range 0 to 768 (again it can be a whole number)


Is there a way of storing these scaled values into a new set of variables with the script variable function? If so, how?


Absolutely happy to pay / donate for the right solution.
Okay, judging by the numbers this is your screen resolution. So you actually want to get the real x y coordinates of the player on the screen in pixels, I guess?


This can be done with event commands:


Control variable -> Game Data -> Character -> Screen X


Or with script calls:


$gamePlayer.screenX()




I know this isn't exactly what you asked but I tried answering the spirit of the question.


You could calculate all that by getting tile coordinates, multiplying them by the width and height and knowing where the display is right now, but why would you bother since you have the function ready?


Please look at this thread to get the list of the equivalent of event commands as script calls:




You'd use:


$gameVariables.value(id) //to return the value of a variable
$gameVariables.setValue(id,value) //to set the value of a variable


Combining those script calls should get you the desired effect.


If I'm wrong:


Why are you trying to "scale" those variables? I could tell you to use the mod operator. This way you'd always get a result between 0 and 1024 or 0 and 762, or you could use simple calls such as Math.max and Math.min to make it always >= 0 and < to your upper limit but you'd likely get unusable result if you were trying to have those still refer to the player coordinates.
Hi there @Astfgl66, thank you for responding.


You're correct in that I want to scale the players X and Y co-ordinates into their relevant X and Y co-ordinates based off a 1024x768 screen resolution. Which I can then use to display a small image to notate the players location on a 1024x768 hand-drawn map (which are displayed using the show picture command)


I've already got the players X and Y tile co-ordinates stored within variables in a map that is 180 tiles wide by 150 tiles high. So I need to scale their X into a range of 0 ~ 1024 and their Y into a range of 0 ~ 768


Thanks again ... any help is appreciated, would you know the exact script I would need for this?
This is just a matter of simple math then.


Your base resolution is 180*150, your target resolution is 1024*768.


Lets say you have point 50,40  in your 180*150 resolution you want to make it into 1024 * 768:


Just multiply by the quotient bewteen your two resolutions:


50 * (1024/180) = 284


40 * (768/150) = 204,4


There you go, you have your new coordinates.
No I think I'm getting there now - feel free to pass me the dunce hat!  However the game runs in 1024x768 constantly, and the map jpeg  i'm displaying is 1024x768 too.


I'm basically asking what calculation I need to get the X and Y pixel co-ordinate on a 1024x768 screen based of the location of the player on a 180 by 150 tile map


Really sorry, if it seems like I'm stupid here, maybe I am 🙂  
@keeno79


Ah I edited my message because even I thought I was coming off as a bit mean, and I wasn't precisely offering you a solution. I have modified it to provide you with a precise answer. I'm sorry if I come as a bit harsh or as looking down on you, I assure you that is not my intention. Must be the language I guess.


I hope the new answer does what you want.
@Astfgl66


Absolutely I didn't think you were being mean, I was being self-deprecating! 🙂  It's ok 🙂


What I don't understand here is how to get a variable to perform that kind of math, there just isn't an option for multiple calculations within one variable control? 


Screen Shot 2016-07-01 at 21.14.42.png


As far as I know (and that's not that much 😛 ) you can only set a variable to something, not do a multi step equation in there. 
Seeing as this was in the javascript forums I didn't realize this could be the problem ^^.


Two ways: via script call:


var x,y
x = $gamePlayer.x * (1024/180)
y = $gamePlayer.y * (768/150)
$gameVariables.setValue(your_variable_id,x)
$gameVariables.setValue(your_variable_id2,y)


Or event commands:

Spoiler
uvXaWs9.png



And as I upload that I realised I made a mistake, the blacked out number of the variable is meant to be 12.



My suggestion is searching for a tutorial on how variables work ^^. It's especially important to do this before thinking about doing any scripting.
@Astfgl66


You are a beautiful human being, thank you for sticking with me. Please let me know your Paypal address so I can send over a token of my appreciation 🙂
I'm going to respectfully decline. This was simple, didn't take much of my time, and I certainly did not do it for the money.


I'm helping people learn because I like it, because I like solving problems and because it helps me learn too.


Not to mention I don't have a paypal account 😛  . Now , if you are some crazy billionaire who would like to give me millions over this, do tell, I'll be happy to arrange matters 🙂  


Joking aside:


Have a beer on me to celebrate having learned something, give it to people more in need than me, or keep it,... Your thanks are enough to show your appreciations, don't worry.


Cheers!

Original thread: https://forums.rpgmakerweb.com/threads/looking-for-a-way-to-scale-a-variable-within-a-specific-range.64579/