jkub Posted November 11, 2009 Share Posted November 11, 2009 Im making a compact speedometer script for experimentational purposes. I know there are is already a selection of speedometers uploaded but I want to learn a basic one myself. I calculated the speed and other things correctly but my main problem here is that I am almost clueless when it comes to manipulating strings. I have did something quite similar to this before and now instead of a "minute.second" format im gonna need a mph format for example "248" Right now I just get "2.48" Ive tried looking at string.sub and string.format in the lua website but I cant get much out of it. However the only thing I am or seem to be understanding is the number that I put in will change the amount of visible charecters. For example "speedStr = string.format ( "%.2f", speedSub )" that two allows me to get somthing like this 2.48 where if I changed it to 3 I would get something like 2.485(some other number) It says there I can use different letters and only two charecters are for strings. I have no clue what each of them does all though Ive tried all of them to see if I could get lucky... No luck Here is some of the code function calculateSpeedAndHealth() local clientVeh = getPedOccupiedVehicle ( getLocalPlayer() ) local velX, velY, velZ = getElementVelocity ( clientVeh ) local realSpeed = ( velX^2 + velY^2 + velZ^2 )^( 0.5 ) -- local vehHealth = getElementHealth ( clientVeh ) -- speedSub = string.sub ( realSpeed, 1, 4 ) speedStr = string.format ( "%.2f", speedSub ) healthString = string.sub ( vehHealth, 1, 3 ) guiSetText ( speedNumber, "Mph: " ..speedStr ) if ( vehHealth >= 1000 ) then guiSetText ( healthNumber, "Health: 1000" ) else guiSetText ( healthNumber, "Health: " ..healthString ) end if isPedInVehicle ( getLocalPlayer() ) then -- else guiSetVisible ( speedometer, false ) removeEventHandler ( "onClientRender", getRootElement(), calculateSpeedAndHealth ) end end You can also see I used string.sub which from what I understands does ALMOST the same thing as that number I mentioned that changed the ammount of numbers Please help me with this Link to comment
eAi Posted November 11, 2009 Share Posted November 11, 2009 String formatting syntax is well documented all over the place. http://en.wikipedia.org/wiki/Printf#pri ... aceholders would be a reasonable place to start. %.3f means a floating point number (a number with a decimal point) with 3 numbers after the decimal point. %d would be an integer (no decimal places). So maybe you want "%d Miles Per Hour" or something? Link to comment
50p Posted November 11, 2009 Share Posted November 11, 2009 248 2.48 Don't you see any similarities? How about moving decimal point to the right by 2 places? Do you remember basic multiplications by 10, 100, 1000, etc.? Look in there https://community.multitheftauto.com/index.html?p ... tails&id=5 You will find out what I did to display speed. Link to comment
jkub Posted November 11, 2009 Author Share Posted November 11, 2009 I used 50p's method of multiplying by 100 and eAi's "%d" suggestion. It worked Perfectly! This is exactly what I was wanting I have been wondering this for so long. Ill take a look at that page and try to interpret:) Thanks so Much Guys! Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now