Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: Zexion on February 13, 2014, 03:49:47 am

Title: [XP]How is this line read?
Post by: Zexion on February 13, 2014, 03:49:47 am
I'm trying to make a damage formula for my abs, and I'm looking at the default RMXP formula.

atk = [attacker.atk - self.pdef / 2, 0].max
self.damage = atk * (20 + attacker.str) / 20


I have no idea what the output of the first line would even look like? Why is it in [] brackets. and what is .max lol
Title: Re: [XP]How is this line read?
Post by: LiTTleDRAgo on February 13, 2014, 04:30:55 am

variable = [a, b, c, d, ....].max         # gets the biggest value
variable = [a, b, c, d, ....].min         # gets the smallest value


example :

atk = [attacker.atk - self.pdef / 2, 0].max


means :


# define atk variable
atk = attacker.atk - self.pdef / 2
# change variable into 0 if atk is negative (below 0)
atk = 0 if atk < 0
Title: Re: [XP]How is this line read?
Post by: Zexion on February 13, 2014, 01:02:08 pm
Thanks Drago :p
It's good to know
Title: Re: [XP]How is this line read?
Post by: Ryex on February 13, 2014, 01:26:50 pm
it's worth pointing out that "max" and "min" are methods of the enumerable (http://ruby-doc.org/core-1.8.6/Enumerable.html) module which is included in the array class. They will work with far more than just 2 values

max and min assumes that the objects in the enumerable implement Comparable (http://www.ruby-doc.org/core-1.8.6/Comparable.html)

you can also pass max/min a block that works just like sort
array.max { |a, b| a.length <=> b.length }


the <=> (spaceship) operator is special comparison in that instead of returning true or false if one object is bigger than another it returns 1, 0, or -1 if the first object is bigger, equal to, or smaller than the second respectively.  you do not need to use the <=> in the block you pass its just a convince. The block simply needs to return 1 if a>b, 0 if a==b, or -1 if a<b.
Title: Re: [XP]How is this line read?
Post by: Zexion on February 17, 2014, 05:36:58 pm
I didn't even see this post ryex o.o thanks for the info. Never hurts to learn multiple methods of accomplishing something :D