adding a prop on all objects in a rooms contents

0 views
Skip to first unread message

Kenneth Burnett

unread,
Apr 3, 2014, 12:31:22 AM4/3/14
to moo-...@googlegroups.com
Hi,

I have a container with a bunch of objects in it. all of the objects have property of weight. i am trying to add all of these up to make the container weight reflect all of the objects inside weight plus its own weight_empty.

i have tried

for x in (this.contents)
this.weight = this.weight_empty + x.weight;
player:tell(x.name, "," ,x.weight);
endfor
player:tell(this.name, "," ,this.weight);

I put in the player tells to give me an idea of what it is doing. so far not having any luck. any advice would be helpful.

Thanks,

kenny

Katelyn Gigante

unread,
Apr 3, 2014, 1:02:55 AM4/3/14
to Kenneth Burnett, moo-...@googlegroups.com
This is something that should be done with a verb, not a property.  It's a dynamic number, and setting it manually like that is a really bad practice.

Make a verb ROOT:weight with the following code: [Where ROOT is the same place .weight is defined]
  weight = this.weight;
  for thing in (this.contents)
    weight = weight + thing:weight();
  endfor
  return weight;

This way, .weight is always the weight of the empty object.  You should never refer to it in code, unless you need to know how much it would be if empty. 

- Katelyn
Thursday, 3 April 2014 3:31 PM
--
You received this message because you are subscribed to the Google Groups "MOO Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to MOO-talk+u...@googlegroups.com.
To post to this group, send email to MOO-...@googlegroups.com.
Visit this group at http://groups.google.com/group/MOO-talk.
For more options, visit https://groups.google.com/d/optout.

--
Sent using Postbox:
http://www.getpostbox.com

Littlefield, Tyler

unread,
Apr 3, 2014, 1:46:17 AM4/3/14
to Katelyn Gigante, Kenneth Burnett, moo-...@googlegroups.com
On 4/3/2014 1:02 AM, Katelyn Gigante wrote:
This is something that should be done with a verb, not a property.  It's a dynamic number, and setting it manually like that is a really bad practice.

Make a verb ROOT:weight with the following code: [Where ROOT is the same place .weight is defined]
  weight = this.weight;
  for thing in (this.contents)
    weight = weight + thing:weight();
  endfor
  return weight;

I'm not totally sure I agree. If you can unequivocally state that:
1) The weight of the container will be updated when it's state changes
and 2) That every state change will update the weight of the container, then you would be fine updating the weight property with the one-off calculation like that.

I'm picturing a scenario in which the player's weight of all objects is calculated when they move, maybe as a penalty to MP or something based on encumbrance. With that logic, every move would require that a lot of loops are ran--especially if it's possible to carry more than one container, or even nest containers. That could get pretty expensive in terms of ticks depending on game logic and where the weight is actually applied.
-- 
Take care,
Ty
http://tds-solutions.net
He that will not reason is a bigot; he that cannot reason is a fool; he that dares not reason is a slave.

Josh Benner

unread,
Apr 3, 2014, 9:30:09 AM4/3/14
to Katelyn Gigante, ty...@tysdomain.com, moo-...@googlegroups.com, Kenneth Burnett
I've done a lot of that type of stuff with MOO, and the actual performance penalty is often smaller than I imagine. I've learned to profile things by execution time rather than ticks (which are sometimes useful, but not as a performance profile). I recommend modifying your eval command(s) to spit out milliseconds in addition to ticks (and keep in mind that time will include compile time [depending on your eval implementation], which is often not insignificant compared to relatively quick code).

Furthermore: beware premature optimization. Implement first the way that makes the most sense from the perspectives of API design/usability/durability/elegance first -- then measure, and if it performs poorly, optimize.
-- 
Josh Benner

part1.04080200.08090107@tysdomain

Shan Hollen

unread,
Apr 4, 2014, 7:18:17 PM4/4/14
to MOO-...@googlegroups.com
Ever consider using enterfunc and exitfunc?

Kenneth Burnett

unread,
Apr 4, 2014, 8:16:37 PM4/4/14
to Shan Hollen, moo-...@googlegroups.com
I was able to get it to work using what tyler sent. I have a verb that i use for moving from one room to another. so i was able to implement it using that. thanks to everyone that has sent the good suggestions.
Reply all
Reply to author
Forward
0 new messages