[XP]How is this line read?

Started by Zexion, February 13, 2014, 03:49:47 am

Previous topic - Next topic

Zexion

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

LiTTleDRAgo

February 13, 2014, 04:30:55 am #1 Last Edit: February 13, 2014, 01:34:50 pm by LiTTleDRAgo

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

Zexion


Ryex

it's worth pointing out that "max" and "min" are methods of the enumerable 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

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.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Zexion

I didn't even see this post ryex o.o thanks for the info. Never hurts to learn multiple methods of accomplishing something :D