Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Problem getting a percentile

Author
Message
erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 23rd Apr 2012 00:44
Hello,

I am using the stretch sprite command to create a 'level progress bar'

So I wanted to write some code to figure out how far along into the current level you are as a percentage and then use that as a variable in the stretch sprite command right?

So imagine level one ends at 5000 points , which is the potential High_Score
Your current score is 2500 points which we will call CurrentScore

My Code so far is like so



My expectation is that Added# should be .5, then this gets multiplied by 100 to be 50 which is the number I am looking for; however it comes out as 0, and 0 respectively.

Any ideas what I'm doing wrong with this one?

Thanks!
CmdrDoughnut
12
Years of Service
User Offline
Joined: 21st Mar 2012
Location:
Posted: 23rd Apr 2012 00:55
Instead of using "+" try using ";" to combine strings/values in a print command.
WickedX
15
Years of Service
User Offline
Joined: 8th Feb 2009
Location: A Mile High
Posted: 23rd Apr 2012 01:03
In the equation Added# = CurrentScore / High_Score, CurrentScore and High_score are integers. I think therefore it is calculated as an integer before being assigned to Added#. To force the calculation to be float use this.

Millenium7
19
Years of Service
User Offline
Joined: 13th Dec 2004
Location:
Posted: 23rd Apr 2012 01:11
simple yet not quite obvious (even to those of us who've been around a while)
Both variables high_score and currentscore are being created as integers. Integers cannot have decimal places and will always be rounded down, thus when you divide to get 0.5 you end up with 0, which gets multiplied by 100 and is still 0

It's good practise to always declare your variables before settings them. i.e.



This will allow the decimal place
I'm super tired and can't think properly right now but I believe you don't have to declare currentscore/highscore, they can remain as integers. Though Added# definately must be declared as a float, otherwise it will auto declare as an integer because you are adding 2 integers to it

You may yet have some troubles down the track with decimal places i.e.


will probably output "0", because it's still treating the numbers as integers and not floats. To fix that you simply add a .0 to the end of a number

erebusman
13
Years of Service
User Offline
Joined: 23rd Jul 2011
Location: Sacramento, CA
Posted: 23rd Apr 2012 01:46
Thanks guys - both of you nailed it

That did not occur to me - that's quite a little tricky fine point!

Login to post a reply

Server time is: 2024-11-23 21:06:08
Your offset time is: 2024-11-23 21:06:08