4 Replies - 5726 Views - Last Post: 21 June 2012 - 03:08 PM Rate Topic: -----

#1 Ladiesplease  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-April 12

Code Problem

Posted 11 April 2012 - 12:59 AM

Hello i have gotten this code of internet my the use BlizzABS and you could say im a 'noob' in ruby but i really want
to learn it :) So i wounder if anyone can help me with some usefull comments on the code. I find it an easy way to learn
and so i can go back and read commets about the code in the future when i need it.
"This code is about 50 Lines"

 
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# START Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

# word displayed for EXP
EXP_WORD = 'EXP'

#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# END Configuration
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

$exphud_exp_in_hud = 1.01

#==============================================================================
# Hud
#==============================================================================

class Hud
  
  alias create_positions_expinhud_later create_positions
  def create_positions
    create_positions_expinhud_later
    @exp_x, @exp_y = 4, 49 + @hud_height - @original_height
    @hud_height += 16
    @hot_y += 16
    @left_y += 16
  end
  
  alias draw_basic_expinhud_later draw_basic
  def draw_basic
    draw_basic_expinhud_later
    color = case BlizzABS::Config::HUD_TYPE
    when 0 then Color.new(255, 255, 255, 192)
    when 1 then Color.new(0, 0, 0, 0)
    end
    self.bitmap.fill_rect(@exp_x+32, @exp_y+3, 116, 14, color) if color.is_a?(Color)
    self.bitmap.fill_rect(@exp_x, @exp_y, 32, 20, Color.new(0, 0, 0, 128))
    self.bitmap.font.color = system_color
    self.bitmap.draw_text_full(@exp_x, @exp_y, 80, 20, EXP_WORD)
    self.bitmap.font.color = normal_color
  end
  
  alias draw_empty_expinhud_later draw_empty
  def draw_empty
    draw_empty_expinhud_later
    self.bitmap.gradient_bar_hud(@exp_x + 32, @exp_y + 3, 114, 0, 'hud_white_bar')
    self.bitmap.font.color = disabled_color
    self.bitmap.draw_text_full(@exp_x + 54, @exp_y, 84, 20, '0', 2)
    @exp = nil
  end
  
  def draw_exp
    @exp = actor.exp
    rate = (actor.next_exp != 0 ? actor.now_exp.to_f / actor.next_exp : 1)
    self.bitmap.gradient_bar_hud(@exp_x + 32, @exp_y + 3, 114, rate, 'hud_white_bar')
    self.bitmap.font.color = normal_color
    self.bitmap.draw_text_full(@exp_x + 54, @exp_y, 84, 20, actor.next_rest_exp_s, 2)
  end
  
  alias upd_expinhud_later update
  def update
    $game_temp.hud_refresh ? draw_exp : test_exp if actor != nil
    upd_expinhud_later
  end
  
  def test_exp
    draw_exp if actor.exp != @exp
  end

end



Is This A Good Question/Topic? 0
  • +

Replies To: Code Problem

#2 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Code Problem

Posted 11 April 2012 - 09:58 AM

Hello, welcome to the forum.

Quote

Hello i have gotten this code of internet my the use BlizzABS

Could you put that in other words? I'm having trouble parsing it.

Also, could you tell us what the code is supposed to do and append a link to its source. Knowing a piece of codes function beforehand makes for a better read.

After reading what blizz-ABS is..

Quote

Using this script in your game will allow you to carry out battles on the map. Examples on commercial games for an ABS are The Legend of Zelda (various Nintendo consoles), Secret of Mana (SNES), Diablo 2 (PC), etc. Active Battle System also means real-time battle on the map.
This ABS has a many advanced features to help you and allow you creating high quality games, is easy to use and is very flexible.

...it sounds really cool, but I've never encountered it before. blizz-ABS is a library. That means it's made up of functions which you can call from ruby and have it do lots of stuff for you. For beginners to ruby, it's sometimes easier to get started doing things with out the use of 3rd party libraries like this one so you can get the hang of functions, classes, and using objects.


I've been meaning to pick up some game design tricks so maybe I'll be able to help you out as I rebuild an OS on vSphere.

Do you know how to compile and run that code? It looks like it might be the kind of snippet that you're expected to insert into an already working game.

See where it says class Hud that's an important bit of it. It means that it's a class, and to use the class in your code, it would look something like this.

my_HUD = HUD.new

my_HUD.create_positions_expinhud_later()  # this will make the word "EXP" appear somewhere on screen


Without knowing more, it's hard to say what that would do exactly.


This bit of code is more interseting.
alias create_positions_expinhud_later create_positions


That line implies that there is already a function called 'create_positions' and an alias for it should be made named 'create_positions_expinhud_later'. After that line, (I *THINK*) the script overwrites the original function that was named 'create_positions'. This allows the script to be dropped in somewhere, and overwrite all function calls to 'create_positions' which is one of the reasons why Ruby is such a damned versatile thing.

So have you gotten anything to run yet? It looks like it's a GUI type of thing. I wonder if it can be used with WebGL.
Was This Post Helpful? 0
  • +
  • -

#3 Ladiesplease  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-April 12

Re: Code Problem

Posted 12 April 2012 - 12:28 AM

I dont really know what all of the code does bot i know that the sqript in general adds an EXP bar to the HUD the hud is located in the top right corner my game made in RPG MAKER XP.

Here is a picture of the game
Posted Image

I alsow know that this script expands my HUD in order to make the xp bar fitt without messing up the apperance.

I dont really know more about it :/
Was This Post Helpful? 0
  • +
  • -

#4 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: Code Problem

Posted 12 April 2012 - 08:31 AM

well.. for beginners in Ruby, you're going to struggle quite a bit jumping into to this RPG Maker "Framework". You might find this online book to bring you luck on your journey. As far as your code goes, at this forum we will be unlikely able to help very much because your question is specific to a framework (RPG Maker). You might have better luck on an RPG Maker forum, because I have no idea what these function calls are like "create_positions" and so on.

I can point out some stuff that might help you in an immediate sense:

Quote

21 def create_positions
22 create_positions_expinhud_later
23 @exp_x, @exp_y = 4, 49 + @hud_height - @original_height
24 @hud_height += 16
25 @hot_y += 16
26 @left_y += 16
27 end



Defining functions:
def create_positions
   # code goes here
end



That's how ruby defines a function. Somewhere else in the code, the function will be called, and everything within that function will run. A function call looks like this

create_positions


or it looks like this
create_positions()


It depends on what kind of mood the programmer is in at the time of writing the call.

This stuff you can play with:
23	    @exp_x, @exp_y = 4, 49 + @hud_height - @original_height



That line can (and arguably should) be broken up into
@exp_x = 4
@exp_y = 49 + @hud_height - @original_height



You probably already know what that does in ruby, but in your framework, it likely sets the coordinates of your EXP bar within that boundary box. Change 4 to 40 and see what moves. Do that with the rest of the code and you'll be in business programming ruby in no time. Oh, @exp_x is an instance variable. It's a kind of super powered variable, I believe that book I linked to exposes their nature articulately.



You can probably manipulate the color of that EXP bar too if you'd like.

Quote

29 alias draw_basic_expinhud_later draw_basic
30 def draw_basic
31 draw_basic_expinhud_later
32 color = case BlizzABS::Config::HUD_TYPE
33 when 0 then Color.new(255, 255, 255, 192)
34 when 1 then Color.new(0, 0, 0, 0)
35 end


This line specifies a white color, which is somewhat transparent. It's probably the color of the bar, but only you can test this at the moment.

33	      when 0 then Color.new(255, 0, 0, 192)


That *should* change the bar red, because the way colors work in your case is that they are specified as (HOW RED?, HOW GREEN?, HOW BLUE?, and finally HOW SEE-THROUGH-ABLE?). You can go up to 255 for a very deep color, and as low as 0 for a very shy color.
Was This Post Helpful? 0
  • +
  • -

#5 Gazuu  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 18
  • Joined: 20-June 12

Re: Code Problem

Posted 21 June 2012 - 03:08 PM

What NotarySojac already suggested regarding the above code snippets seems plausible to me, my thought about that are similar.
Since you asked for some comments on the code, I will try to share some of my thoughts for that code, though I'm really not entirely sure about it myself and can only give some hints about what it could possibly be doing.
So how about the draw_exp method in line 52:
 def draw_exp
    @exp = actor.exp


As far as I know, in the RGSS (the ruby framework for the rpgmaker xp) the Actor class represents the playable character(s), and actor is an instance of that class, representing your character in your game. You can access that character's current exp value via the exp variable.
Since you say you are a "ruby noob", I will mention this now(just skip if you're already familiar with attr_accessor): with actor.exp, your are actually not referring to the exp variable of that class, but to a method, which returns the value of that exp variable.
So, within the Actor class, there will most likely be something like this:
Class Actor
  attr_accessor :exp
  # and lots more code...
end


attr_accessor is a ruby method, which itself creates 2 methods within the class (for every argument, so in this case, only for :exp), a getter and a setter method.
so with actor.exp (exp being the getter method) you can read the value of the @exp variable (it is a member variable within the Actor class) and with actor.exp= (exp= being the setter method) you can change its value.

Now that was a rather long and in-depth explanation for "he sets @exp to the exp value of the actor" ;)
So what else we got:
    rate = (actor.next_exp != 0 ? actor.now_exp.to_f / actor.next_exp : 1)


The variable rate will determine hoch much of the exp bar is going to get filled up.
I don't know the name for it right now, but that stuff with the "?" and the ":" is just another way of writing an if-statement.
First, check if actor.next_exp has any other value than 0. If that's true, rate will get the value calculated before the ":", if it's not true (actor.next_exp is 0), rate will be "1", since that's what is behind the ":" .
I guess actor.next_exp is the exp value needed for the next lvl up...so if that value is 0, rate = 1.
Actually, I can't really make any sense of that, because if my assumption was correct, that would mean when actor.next_exp is 0, you level up and get a new value for actor.next_exp anyway...so, I'm sorry, don't know what's behind the curtains there.
Well then, if actor.next_exp is != 0, you actually calculate the rate.
by calling the .to_f funtion with actor.now_exp, that value is converted to a float (so for example, instead of 256 it's 256.0), which is important, since we are dividing here and obviously want to have more precision. So at least on of the values of the division needs to be a float, only then ruby knows "ok, the result also has to be a float".
So in the end, we divide the current exp value by the value needed for the next lvl up. which, I assume, shall return a value between 0.0 and 0.9(9999... whatever ;) )
If we didn't tell ruby we want to have a float there, ruby would just always calculate 0.
So now we have the rate variable to determine how much of the exp bar is going to be actually drawn as "filled"
    self.bitmap.gradient_bar_hud(@exp_x + 32, @exp_y + 3, 114, rate, 'hud_white_bar')


gradient_bar_hud() seems like a custom function for drawing something onto the bitmap of the Hud class. After seeing your screenshot, one can easily tell that this is the method which is used for filling all the bars you got in your hud with some color.
@exp+32 and @exp+3 determine th x and y coordinates, where the method will start drawing something. hard to tell what the 114 does... as already suggested, try changing that value and see what happens. Now rate tells the method "only draw to that percentage of the bar", as in, with an value of 0.5, only draw half of the bar. 'hud_white_bar' is probably the source bitmap.
How about we have a little crashcourse about how the RGSS handles the character/event animation at this point? Should fit in nicely here.
I'm sure you are familiar how the character sets for RPGMaker XP look like, you have a 4x4 grid kind of image, with different directions and animation cycles.
So in general, what the RGSS does when you move your character is, it takes that image, and copies one of those rectangles, of which your charset is made of, onto the screen. (which are commonly 16 rectangles in total)
That's possible because with the blit() method (block image transfer), you can take an image, tell the blit method where to copy it, and what part of it to copy there.
So what I think is, gradient_bar_hud itself has the blit method somewhere inside.
It probably does "something" with the given values 8@exp_x, rate etc...) and then passes it on to the blit method, where 'hud_white_bar' should be the name of the source image, some white-ish bar thingy that is.

The thing is, if you want to do that with the blit method, there are 7 arguments in total you have to provide:
The x and y values for "where to start drawing", the x and y values for "where to start copying form the source", and the width and height for "how far, horizontally and vertically, should the rectangle that I'm about to copy, be?", and then of course, the source "from what image am I going to copy?"
But as you can see, gradient_bar_hud only takes 5 arguments (that number of arguments will never work with blit).
My guess is, the missing two values are created within gradient_bar_hud() (since for thos bars, some values will always be the same and don't need to be recalculated everytime) and from there passed to blit(), together with all arguments given to gradient_bar_hud().
  self.bitmap.font.color = normal_color
  self.bitmap.draw_text_full(@exp_x + 54, @exp_y, 84, 20, actor.next_rest_exp_s, 2)
end


Nothing much to explain here for me really.
the color of the font used in that hud was set to normal_color, which is some color (something like [123, 234, 111, 0]) which was defined who-knows-where.
draw_text_full draws...some text... onto the Hud bitmap, at the given coordinates. actor.next_rest_exp_s seems to be a function which returns some exp value, but as a string.

But maybe I jost got lazy, because this post here got way bigger than I anticipated o
Abyway, I hope you could learn something from all that.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1