Simplifying some logic

Rello

GERGtheNERD
Veteran
Joined
May 15, 2012
Messages
207
Reaction score
13
First Language
Profanity
Primarily Uses
RMVXA
Hey all,

I solved my previous problem and now I was just wondering if someone can help me simplify some logic I'm using.

I'm using

if new_value2 > @actor.param(2) draw_picture("uparrow", 204-43, dy + 22 * 2 + 8) end if new_value2 < @actor.param(2) draw_picture("down arrow", 204-43, dy + 22 * 2 + 8) end if new_value2 == @actor.param(2) draw_picture("equalarrow", 204-43, dy + 22 * 2 + 8) endbut I'm sure there's a way to simplify this. Doing it this way would give me over 81 lines for all the things I'm displaying =_=;
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
To keep it fairly readable to a newer scripter, you could do it like this:

if new_value2 > @actor.param(2) draw_picture("uparrow", 204-43, dy + 22 * 2 + 8)elsif new_value2 < @actor.param(2) draw_picture("down arrow", 204-43, dy + 22 * 2 + 8)else draw_picture("equalarrow", 204-43, dy + 22 * 2 + 8)endOr if you want to minimize the number of lines, do it like this:
Code:
picture = new_value2 > @actor.param(2) ? "uparrow" : new_value2 < @actor.param(2) ? "down arrow" : "equalarrow"draw_picture(picture, 204-43, dy + 22 * 2 + 8)
And you should really convert those numbers - do the calculation yourself, where possible, and put the result into the script, rather than having it do the calculation every time.204-43 is always going to be 161, so replace all instances of 204-43 with 161 instead.

dy + 22 * 2 + 8 is always going to be dy + 52, do replace all instances of dy + 22 * 2 + 8 with dy + 52 instead.

According to the normal order of operations, dy + 22 * 2 + 8 is NOT going to be calculated from left to right. The 22 * 2 will be done first, giving 44. Then dy and 8 will be added. And the order doesn't matter when you're adding, so you can do 44 + 8 to give 52.

That would make your last command be this:

Code:
draw_picture(name, 161, dy + 52)
 
Last edited by a moderator:

Tsukihime

Veteran
Veteran
Joined
Jun 30, 2012
Messages
8,564
Reaction score
3,846
First Language
English
Instead of copy pasting and hardcoding all your param ID's just create a method that takes a param ID as an argument.
 
Last edited by a moderator:

Rello

GERGtheNERD
Veteran
Joined
May 15, 2012
Messages
207
Reaction score
13
First Language
Profanity
Primarily Uses
RMVXA
@Shaz, I do it that way fo quick edits and then I simplify when I'm done with the visual work. It's just something I do. :\

@Tsuki, I wish I knew how- or even knew what that means... ^^;;
 

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
param2 = @actor.param(2)bitmap = 'uparrow'bitmap = 'downarrow' if new_value2 < param2bitmap = 'equalarrow' if new_value2 == param2draw_picture(bitmap, 161, dy + 52)It's a good idea to make it calculate the parameter once instead of calling the param(2) method multiple times, since you would end up executing the following method without good reason:

#-------------------------------------------------------------------------- # * Get Parameter #-------------------------------------------------------------------------- def param(param_id) value = param_base(param_id) + param_plus(param_id) value *= param_rate(param_id) * param_buff_rate(param_id) [[value, param_max(param_id)].min, param_min(param_id)].max.to_i endAlso:

but I'm sure there's a way to simplify this. Doing it this way would give me over 81 lines for all the things I'm displaying =_=;
You could probably use an iterator, what are you trying to do?
 

Rello

GERGtheNERD
Veteran
Joined
May 15, 2012
Messages
207
Reaction score
13
First Language
Profanity
Primarily Uses
RMVXA
Breaking Owl, I'm completely re-doing the parameter comparison on EquipStatus. I don't like the pre-defined automated stuff by the core system or YEA, so I'm just redoing it all to better suit what and how I want to show this. Right now, this is basically me hard coding the arguments for every stat to respective show arrows.

def draw_new_param_rello(dx, dy, param_id) @actor = $game_party.menu_actor self.contents.font.size = 20 new_value0 = @temp_actor.param(0) new_value1 = @temp_actor.param(1) new_value2 = @temp_actor.param(2) new_value3 = @temp_actor.param(3) new_value4 = @temp_actor.param(4) new_value5 = @temp_actor.param(5) new_value6 = @temp_actor.param(6) new_value7 = @temp_actor.param(7) draw_number(-2, dy + 22 * 0, contents.width-4, new_value0, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 1, contents.width-4, new_value1, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 2, contents.width-4, new_value2, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 3, contents.width-4, new_value3, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 4, contents.width-4, new_value4, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 5, contents.width-4, new_value5, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 6, contents.width-4, new_value6, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 7, contents.width-4, new_value7, 4, 2, Color.new(255,255,255,100)) reset_font_settings #using param(2) (strength) as a test for the displays since it's quickly done by changing weapons    param2 = @actor.param(2)    bitmap = 'uparrow'    bitmap = 'down arrow'  if new_value2 <  param2    bitmap = 'equalarrow' if new_value2 == param2    draw_picture(bitmap, 161, dy + 52)endI know this is horribly primitive work, and a definite eyesore, but I'm just doing what I know how to do.
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
40,098
Reaction score
13,704
First Language
English
Primarily Uses
RMMV
Oh, then you could do something like this:

Code:
def get_arrow(new_value, old_value)  return "uparrow" if new_value > old_value  return "downarrow" if new_value < old_value  return "equalarrow"end
And in the method where you show stuff:
Code:
draw_picture(get_arrow(new_value2, @actor.param(2)), 204-43, dy + 22 * 2 + 8)
 

Rello

GERGtheNERD
Veteran
Joined
May 15, 2012
Messages
207
Reaction score
13
First Language
Profanity
Primarily Uses
RMVXA
Would I have to explicity define that for every stat new/old? or could I do something like:

def get_arrow(@temp_actor.param(param_Id), @actor.param(param_id) return "uparrow" if @temp_actor.param(param_Id > @actor.param(param_id return "downarrow" if @temp_actor.param(param_Ide < @actor.param(param_id return "equalarrow"endand it would automatically know which things to compare?
 
Last edited by a moderator:

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
But isn't that method getting the parameter id as an argument? Why do you want to draw all the parameters each time the method is intended to draw one?

def draw_new_param_rello(dx, dy, param_id)You see each parameter goes from 0 to 7?

new_value0 = @temp_actor.param(0) new_value1 = @temp_actor.param(1) new_value2 = @temp_actor.param(2) new_value3 = @temp_actor.param(3) new_value4 = @temp_actor.param(4) new_value5 = @temp_actor.param(5) new_value6 = @temp_actor.param(6) new_value7 = @temp_actor.param(7)You can use that to your advantage since you use that numbers here;

draw_number(-2, dy + 22 * 0, contents.width-4, new_value0, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 1, contents.width-4, new_value1, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 2, contents.width-4, new_value2, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 3, contents.width-4, new_value3, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 4, contents.width-4, new_value4, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 5, contents.width-4, new_value5, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 6, contents.width-4, new_value6, 4, 2, Color.new(255,255,255,100)) draw_number(-2, dy + 22 * 7, contents.width-4, new_value7, 4, 2, Color.new(255,255,255,100))So you can just say, param_id:

Code:
 def draw_new_param_rello(dx, dy, param_id)    @actor = $game_party.menu_actor    self.contents.font.size = 20    new_value = @temp_actor.param(param_id)    draw_number(-2, dy + 22 * param_id, contents.width-4, new_value, 4, 2, Color.new(255,255,255,100))    reset_font_settings    #using param(2) (strength) as a test for the displays since it's quickly done by changing weapons    param2 = @actor.param(param_id)    bitmap = 'uparrow'    bitmap = 'down arrow'  if new_value2 <  param2    bitmap = 'equalarrow' if new_value2 == param2    draw_picture(bitmap, 161, dy + 52)  end
 
Last edited by a moderator:

Rello

GERGtheNERD
Veteran
Joined
May 15, 2012
Messages
207
Reaction score
13
First Language
Profanity
Primarily Uses
RMVXA
If it legimitely that simple? *facepalm*

Witht what you posted, I'm only seeing the HP (param0) being displayed. Unless I was supposed to do more than a copy and paste...
 
Last edited by a moderator:

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
Check where draw_new_param_rello is called, maybe you're calling it once instead of replacing the draw_new_param call in draw_item (Window_EquipStatus).
 

Rello

GERGtheNERD
Veteran
Joined
May 15, 2012
Messages
207
Reaction score
13
First Language
Profanity
Primarily Uses
RMVXA
I did a direct replace, but the previous call was for a mass (new_param.group) or something like that. I didn't like how it was all bundled under one thing that I couldn't personally control
 

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
Try iterating in your method:

Code:
   def draw_new_param_rello(dx, dy, param_id)    @actor = $game_party.menu_actor    (0..7).each { |param_id|       self.contents.font.size = 20      new_value = @temp_actor.param(param_id)      draw_number(-2, dy + 22 * param_id, contents.width-4, new_value, 4, 2, Color.new(255,255,255,100))      reset_font_settings    #using param(2) (strength) as a test for the displays since it's quickly done by changing weapons      param2 = @actor.param(param_id)      bitmap = 'uparrow'      bitmap = 'down arrow'  if new_value2 <  param2      bitmap = 'equalarrow' if new_value2 == param2      draw_picture(bitmap, 161, dy + 52)    }  end
 

Rello

GERGtheNERD
Veteran
Joined
May 15, 2012
Messages
207
Reaction score
13
First Language
Profanity
Primarily Uses
RMVXA
What is "iterating" exactly? Sorry for newbie questions.
 

Venka

Veteran
Veteran
Joined
Jun 20, 2012
Messages
945
Reaction score
365
First Language
English
Primarily Uses
def draw_new_param_rello(dx, dy, param_id) @actor = $game_party.menu_actor.param contents.font.size = 20 new_value = @temp_actor.param(param_id) draw_number(dx, dy + 22 * param_id, contents.width-4, new_value, 4, 2, Color.new(255,255,255,100)) reset_font_settings bitamp = 'uparrow' bitamp = 'uparrow' if new_value < @actor.param(param_id) bitamp = 'uparrow' if new_value == @actor.param(param_id) draw_picture(bitmap, dx + 158, dy + 22 * param_id) endadded in the dx to the def cause it wasn't used before

to go through the list you'll want to do something like this

def draw_params for i in 0..7 draw_new_param_rello(-2, 0, i) end endthat says to start posting the stats at -2 (on x axis) and at the top of the window on the y axis. The for statement says going through numbers 0 to 7 (this is the iteration part). When we're on the 0 number.. the i = 0 and run the draw_new_param_rello with 0 as the param_id. Then run through with i = 1, then do it again and this time i = 2, etc till 7

also I changed your bitmap thing to scale it's place with the param id as well. I can't play test since I don't have all your code or resources ;) so you might need to adjust some of the numbers.
 
Last edited by a moderator:

♥SOURCE♥

Too sexy for your party.
Veteran
Joined
Mar 14, 2012
Messages
693
Reaction score
411
Primarily Uses
http://en.wikipedia.org/wiki/Iterator

http://en.wikipedia.org/wiki/Iteration#Computing

(0..7) is a range, any number from 0 to 7 (inclusive). So iterating in that case is doing something each time the "current number" changes, it will execute the code between brackets one time for 0, one time for 1, one time for 2, one time for 3, until it gets to 7 and stops since that is the end of the given range.

As you can see, iterators are extremely useful.

Here you have some Ruby resources.

http://code.tutsplus.com/tutorials/ruby-for-newbies-iterators-and-blocks--net-17089

https://thenewcircle.com/static/bookshelf/ruby_tutorial/iterators.html
 

Rello

GERGtheNERD
Veteran
Joined
May 15, 2012
Messages
207
Reaction score
13
First Language
Profanity
Primarily Uses
RMVXA
Thanks, everybody. I genuinely learned quite a lot today. :]
 
Last edited by a moderator:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

How many parameters is 'too many'??
Yay, now back in action Happy Christmas time, coming back!






Back in action to develop the indie game that has been long overdue... Final Fallacy. A game that keeps on giving! The development never ends as the developer thinks to be the smart cookie by coming back and beginning by saying... "Oh bother, this indie game has been long overdue..." How could one resist such? No-one c
So I was playing with filters and this looked interesting...

Versus the normal look...

Kind of gives a very different feel. :LZSexcite:
To whom ever person or persons who re-did the DS/DS+ asset packs for MV (as in, they are all 48x48, and not just x2 the pixel scale) .... THANK-YOU!!!!!!!!! XwwwwX

Forum statistics

Threads
105,847
Messages
1,016,968
Members
137,561
Latest member
JaCrispy85
Top