Greetings Monks! I seek your enlightenment. How do I make objects interact with each other? For instance, lets say I have one object attack another object? How do I change an objects internal data?
From years of mudding, I'd probably abstract out a little more. Instead of:
$joe->damage($henry->attack); $henry->damage($joe->attack);
I'd use something more like
$henry->attack($joe); $joe->attack($henry); sub attack { my ($self,$target) = @_; ... # compare dex/abilities to see if you hit the target if ( $hit ) { ... # compute damage from the attack $target->damage( $amount, $self ); ... # emit messages about the attack } else { ... # emit messages about a miss } } sub damage { my ($self, $amount, $attacker) = @_; ... # adjust amount for armour, etc. $self->{_hp} -= $amount; # this assumes the object is a hashref # this may not be the case ... see 'perldoc perltoot' if ( $self->{_hp} <= 0 ) { ... # whatever to do for death } }
Of course, I'd personally use some existing mud base (eg, PerlMUD if you're going to do it in perl ... (although I've never used it ... most of my mud coding was in LPC)), an then modify it to suit my needs. (c'mon ... damage in hp only? if you pass a type of damage, you can account for armour differences and/or resistances/weaknesses)
In reply to Re: Perl OO newbie advice - object interaction
by jhourcle
in thread Perl OO newbie advice - object interaction
by yoda54
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |