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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.