ID:554576
 
(See the best response by Jp.)
I know you can have objects that contain other objects inside of objects, but how can I pull the contents of the container inside the main container? I want to calculate the weight of the contents of a nested container but I'm not sure how to even call the contents of a nested container...
Best response
Oh man, Castle of the Winds! That game was awesome, and props and encouragement if you're working on a game like it.

As to your question: Calculating the weight of something and all of its contents is a recursive function. Looks something like this:

- Get my weight
- Get the weight of all my contents
- Return that

The part you're getting stuck on is 'get the weight of all my contents', but that's really just the same as the outer function. In code, it'd look something like this:

obj
var/weight = 0

proc/calculateWeight(obj/o)
. += o.weight
for (var/obj/c in o) . += calculateWeight(c)


You might want to look up the . var to understand that code.
aaah I see, thanks a lot!

Yeah I'm working on Castle of the Winds Online, an MORPG remake of the original game. The server is online almost 24 hours a day if you'd like to check out the alpha some time. I'm working on it right now though so it's not online at the moment.
and by the way, if you're interested in helping with development, I'm currently seeking coders and designers. I can't pay anything but it's for a good cause. :D
Doing this as a global proc is bad design. I'd actually make this an atom proc, with weight an atom var, and then do this:

atom/var/weight = 0

atom/proc/TotalWeight()
. = weight
for(var/atom/A in src) . += A.TotalWeight()

With pixel movement, this also means that a turf will count mobs and objs that are partially on it.

You could also do this under /atom/movable instead of /atom. The only benefit of doing it this way is that a turf can calculate the weight of its contents easily.
I actually don't need to count objs that are partially on the turf because I don't allow users to move one pixel at a time. It uses an old, one-tile-at-a-time kind of movement.

I'm setting the weight as a player mob variable and the TotalWeight proc in also under /mob/. Would that be ok?
lol I guess I'll take the lack of response as a yes.
In response to Leptoon (#5)
That should be fine so long as you are only considering mobs. You can only consider the weights of a mob and the mobs inside it.
In response to Kaiochao (#7)
Actually if I pass an argument on the proc, I can consider the weights of objects (or anything else) as well as mobs with the same proc. That's how I have it set up and it seems to be working.
Defining the proc under /atom or /atom/movable also allows it to be used for objs or mobs. Since the proc determines the total weight of the container and its contents, the container is a natural choice for a src object. Defining the weight var under that same type basically guarantees it'll be there, and you won't run into any undefined var errors at runtime.