[SIMPLE] HOW TO DISPLAY NUMBERS WITH SCRIPT~

Posts

Pages: 1
===================================================================================
This is for RPG MAKER VX
===================================================================================
I am a script noob, and in need of help (Before the 31 of Oct. For a Halloween RPG making contest)
All I want to know, is how to display numbers on screen without using pictures. This should be simple for you scripting pros ...
I am making an ABS and i just want to show:
Red numbers when someone is hurt (-27 something like that)
Green numbers when someone is getting healed (+1-100)

I mean thats all ... im sure this is simple as hell.

So please anyone teach me! sad.gif
Copy this into the script editor... somewhere. It shouldn't matter where.


class Sprite

# Define the colours to use if the number is positive, negative, or zero
POSITIVE_COLOR = Color.new( 64, 255, 64, 255)
NEGATIVE_COLOR = Color.new(255, 64, 64, 255)
ZERO_COLOR = POSITIVE_COLOR

# Define the default Z value of any sprite this method generates
DEFAULT_Z = 10

# Create a sprite object that displays a number with a colour based on its
# sign at the intended x/y and return it to the caller
def Sprite.ShowNumber(number, x, y, z = DEFAULT_Z)

# Get the size of the numbers so the appropriate bitmap can be created
textSize = Bitmap.new(32, 32).text_size(number)

# Prepare the sprite object to return
returnSprite = Sprite.new
returnSprite.bitmap = Bitmap.new(textSize.width, textSize.height)
# Set the X/Y/Z of the sprite to the given values
returnSprite.x = x
returnSprite.y = y
returnSprite.z = z

# Remember the default font color of the sprite's bitmap
oldColor = returnSprite.bitmap.font.color.dup
# Set the font color based on the sign of the number
# If the number is positive
if number > 0
# Set the font color to its positive color
returnSprite.bitmap.font.color = POSITIVE_COLOR
# If the number is at zero (has no sign)
elsif number == 0
# Set the font color to its no sign color
returnSprite.bitmap.font.color = ZERO_COLOR
# Otherwise the number has to be negative
else
# So set the font color to its negative color
returnSprite.bitmap.font.color = NEGATIVE_COLOR
end

# Draw the number onto the sprite bitmap
returnSprite.bitmap.draw_text(0, 0, textSize.width, textSize.height, number)
# Reset the sprite bitmap's font
returnSprite.bitmap.font.color = oldColor

# Return the prepared sprite
return returnSprite
end

end


To use it use the Script function in an event. Call the method and it'll give you a reference to your newly created sprite. The arguments are:
1) The number to show
2) The X coordinate of the upper-left corner
3) The Y coordinate of the upper-left corner
4) The Z coordinate (defaults to 10 which is above the map)

Example:

@sprite = Sprite.ShowNumber(9000, 320, 240)
*event stuff here*
@sprite.x = 100
*more event stuff here*
@sprite.dispose

This'll show the number 9000 in green with the upper left corner at 320x240. After some event stuff the number will move so its x coordinate is at 100. Later on the sprite is removed when @sprite.dispose is called.

If you don't want the negative sign in front of negative numbers (I wasn't sure if you wanted that or not) find this line:

returnSprite.bitmap.draw_text(0, 0, textSize.width, textSize.height, number)

And replace it with:

returnSprite.bitmap.draw_text(0, 0, textSize.width, textSize.height, number.abs)


If you've having trouble with it I can make a simple demo of it.

(Also why is text_size an instance method instead of a class method? I shouldn't have to allocate a bitmap just to use that dang method!
)
Thanks for that post lol i haven't read it all as it seems like alot, I'm gonna try and see if i can understand it now, but a demo would be much appreciated thanks :)
Here's a quick demo. I don't know what you want the number to do exactly so its a pretty awful demo.

Download link. Requires RMVX RTP.

If you need it to do more or something else, ask and I'll see what I can do.
author=GreatRedSpirit link=topic=2287.msg39600#msg39600 date=1224820673
Here's a quick demo. I don't know what you want the number to do exactly so its a pretty awful demo.

Download link. Requires RMVX RTP.

If you need it to do more or something else, ask and I'll see what I can do.

Well I get an error when it trys to get rid of the number:


Ok what I want is pretty simple to explain, but I'm not sure if its simple to do ...

Anyway ... i want to make an ABS. Where when you attack a monster it shows the number of damage you dealt to that monster above their head. I have the variables for the monster's screen relative co-ordinates ... so the position of the number will have to be displayed at these specific co-ords right?
Then it gets a little more complicated ...
I want the amount of damage dealt to be dependent of the hero's attack power(Also stored in a variable). So I want the damage to vary randomly (Damage dealt will be either 5 below character's attack power ... or 5 above Eg. Hero's attack power = 30, the damage he deals will be a random number from 25-35).
Damage dealt to an enemy can either be red or no color at all.

Then when my healer is healing ... The amount she heals will be displayed in green. the amount she heals is dependent on her "Spirit" and the number is too be displayed above the hero's head that she heals ...

I hope you understand what I am trying to accomplish ... Please tell me if it would be too difficult to make or not ...
Oops, that's what I get for doing this when I'm too tired to be coding. I'm gonna have to sleep on this, I shouldn't be doing anything when I'm too tired to properly spell my variable names. (I think most of it can be done with event functions though, calculating damage and whatnot)
author=GreatRedSpirit link=topic=2287.msg39622#msg39622 date=1224824993
Oops, that's what I get for doing this when I'm too tired to be coding. I'm gonna have to sleep on this, I shouldn't be doing anything when I'm too tired to properly spell my variable names. (I think most of it can be done with event functions though, calculating damage and whatnot)

Oh your right. I can calculate the damage with events than, and you can display the value of variables right? Is that possible?
Yeah. Something like $game_variables should get you the variable in any script.

So something like

Sprite.ShowNumber($game_variable, whateverX, whateverY)

should show the value of the first variable.
author=GreatRedSpirit link=topic=2287.msg39625#msg39625 date=1224825592
Yeah. Something like $game_variables should get you the variable in any script.

So something like

Sprite.ShowNumber($game_variable, whateverX, whateverY)

should show the value of the first variable.

Ok and can the x and y values be taken from variables too?
Like:
Sprite.ShowNumber($game_variable, $game_variable, $game_variable)
Yeah, that should work.
author=GreatRedSpirit link=topic=2287.msg39630#msg39630 date=1224827939
Yeah, that should work.

Ok thanks for your help ... this seems like it should work :D ... I will try when i get home :p
Sorry for the double post but thanks alot man!
I finally figured it out :D ...
But here is another question is it possible to have a + in front of the green numbers and a - in front of the red? If not never mind ...

Also how do I display red numbers lol ... I've only managed to make green ones
Replace the original script with this:

class Sprite

# Define the colours to use if the number is positive, negative, or zero
POSITIVE_COLOR = Color.new( 64, 255, 64, 255)
NEGATIVE_COLOR = Color.new(255, 64, 64, 255)
ZERO_COLOR = POSITIVE_COLOR

# Define the default Z value of any sprite this method generates
DEFAULT_Z = 10

# Create a sprite object that displays a number with a colour based on its
# sign at the intended x/y and return it to the caller
def Sprite.ShowNumber(number, x, y, z = DEFAULT_Z)

# Get the size of the numbers so the appropriate bitmap can be created
textSize = Bitmap.new(32, 32).text_size(number)

# Prepare the sprite object to return
returnSprite = Sprite.new
returnSprite.bitmap = Bitmap.new(textSize.width, textSize.height)
# Set the X/Y/Z of the sprite to the given values
returnSprite.x = x
returnSprite.y = y
returnSprite.z = z

# Remember the default font color of the sprite's bitmap
oldColor = returnSprite.bitmap.font.color.dup
# Set the font color based on the sign of the number
# If the number is positive
if number > 0
# Set the font color to its positive color
returnSprite.bitmap.font.color = POSITIVE_COLOR
# Set the string to show to be the number with a positive sign
string = "+#{number}"
# If the number is at zero (has no sign)
elsif number == 0
# Set the font color to its no sign color
returnSprite.bitmap.font.color = ZERO_COLOR
# Set the string to show to be the number
string = number
# Otherwise the number has to be negative
else
# So set the font color to its negative color
returnSprite.bitmap.font.color = NEGATIVE_COLOR
# Set the string to show the number, negative sign is implied
string = number
end

# Draw the number onto the sprite bitmap
returnSprite.bitmap.draw_text(0, 0, textSize.width, textSize.height, string)
# Reset the sprite bitmap's font
returnSprite.bitmap.font.color = oldColor

# Return the prepared sprite
return returnSprite
end

end


For negative numbers, the first number you give to Sprite.ShowNumber should be negative. If its positive, just slap a '-' in front.
Example:
Sprite.ShowNumber(-$game_variables, $game_variables, $game_variables)
OK thanks a lot for your help man! I really appreciate it :D
Found a problem with my script and fixed it. Please use this script instead

(I forgot to dispose of the bitmap I created to get access to the text_size method. Pretty big mistake if this wasn't Ruby)


class Sprite

# Define the colours to use if the number is positive, negative, or zero
POSITIVE_COLOR = Color.new( 64, 255, 64, 255)
NEGATIVE_COLOR = Color.new(255, 64, 64, 255)
ZERO_COLOR = POSITIVE_COLOR

# Define the default Z value of any sprite this method generates
DEFAULT_Z = 10

# Create a sprite object that displays a number with a colour based on its
# sign at the intended x/y and return it to the caller
def Sprite.ShowNumber(number, x, y, z = DEFAULT_Z)

# Get the size of the numbers so the appropriate bitmap can be created
tempBitmap = Bitmap.new(32, 32)
textSize = tempBitmap.text_size(number)
tempBitmap.dispose

# Prepare the sprite object to return
returnSprite = Sprite.new
returnSprite.bitmap = Bitmap.new(textSize.width, textSize.height)
# Set the X/Y/Z of the sprite to the given values
returnSprite.x = x
returnSprite.y = y
returnSprite.z = z

# Remember the default font color of the sprite's bitmap
oldColor = returnSprite.bitmap.font.color.dup
# Set the font color based on the sign of the number
# If the number is positive
if number > 0
# Set the font color to its positive color
returnSprite.bitmap.font.color = POSITIVE_COLOR
# Set the string to show to be the number with a positive sign
string = "+#{number}"
# If the number is at zero (has no sign)
elsif number == 0
# Set the font color to its no sign color
returnSprite.bitmap.font.color = ZERO_COLOR
# Set the string to show to be the number
string = number
# Otherwise the number has to be negative
else
# So set the font color to its negative color
returnSprite.bitmap.font.color = NEGATIVE_COLOR
# Set the string to show the number, negative sign is implied
string = number
end

# Draw the number onto the sprite bitmap
returnSprite.bitmap.draw_text(0, 0, textSize.width, textSize.height, string)
# Reset the sprite bitmap's font
returnSprite.bitmap.font.color = oldColor

# Return the prepared sprite
return returnSprite
end

end
Iget a strage error when i try to run this:
@sprite = Sprite.ShowNumber($game_variables, $game_variables, $game_variables)

By the looks of it ... I'd say nothing is wrong with that code ... but why is it not working? Any help?

Ok what I have seen is that displaying any number i specify works fine. Eg.:
@sprite = Sprite.ShowNumber(21, $game_variables, $game_variables)

But when I want to display the number held in a variable:
@Sprite.ShowNumber($game_variables, $game_variables, $game_variables)

I get this error:

(I put blood over it because its killing me!! ... It says "Syntax Error occurred while running script")

I hope its just a small problem ...
Since I do a bit of programming, I'm suspecting that it has something to do with the variable's value not being a string? (Or the other way around ... i dunno, but I need help asap) :'(
BAH!!
Never mind ... I have found why it wasn't working lol ... ;D
I'm sorry for necro posting, but I think it's necessary ...

GreatRedSpirit the script works perfectly, but when trying to save i get this error:


After I put this "Script" command
@sprite.dispose

The number goes away(Or appears to go away), but my guess Is that it really doesn't ... and so when trying to save Scene_File tries to save the map data, but can not save the sprite?
Didn't think of that, the game is probably trying to save the disposed sprite since it got tied to an event. Try setting the sprite variable to nil after you dispose it.

Example:

@sprite = Sprite.Shownumber(10, 10 10)
*insert event stuff here*
@sprite.dispose
@sprite = nil


That's kind of fustrating. I wish I had a better solution :|
Worked like a charm! :D
I'll confirm after I test it in full into my game, as I just tested it on a test map
Pages: 1