Skip to content

Mudlet: How to count multiple objects in room?

Would someone be willing to please help me figure out how to do a unique action depending on how many of one kind of an object are in a room based on GMCP?

For example, if there is one monolith in a room, I want to send myself a note to get a second monolith. If there are two monoliths in a room then I want to send myself a note that everything is fine.

Currently, I have the following which gives me the info message below twice. 

for _, v in pairs(gmcp.Char.Items.List.items) do

if v.name == "a monolith sigil" then

info("A monolith is here!")

end

end


I tried doing something like if tonumber(v.name) > 1 then info ("More than one here") but... Mudlet didn't like that so much.

Comments

  • Use a random variable to add one to at each monolith. Just reset it on room change.
  • edited February 2015
    local monoCount = 0

    for i, v in ipairs(gmcp.Char.Items.List.items) do
      if v.name=="a monolith sigil" then
        monoCount = monoCount + 1
      end
    end

    if monoCount==0 then
      info("no monoliths")
    elseif monoCount==1 then
      info("one mono")
    else
      info(monoCount .. " monoliths")
    end

    Notes: I prefer using ipairs whenever possible. Since that gmcp.Char.Items.List.items table is indexed, not a dictionary style table, no reason not to use ipairs. Also, info isn't a function that exists by default, so I'm assuming you've got a custom function called info. If not, echo it. =P
    image
  • Thank you so much! That works great! Yeah, I made a custom function so that I didn't have to add code each time to make the echo more noticeable.

    Thanks again.


Sign In or Register to comment.