lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 03.12.2013 12:54, K M wrote:

>  player.all_kills[self.name] =  player.all_kills[self.name] or 0
>  player.all_kills[self.name] =  player.all_kills[self.name] + 1
>         end     

The above can be rewritten as:
  player.all_kills[self.name] = ( player.all_kills[self.name] or 0 ) + 1

> So it stores every kill in a table entry named after the creature killed.
> 
> I've tried to display the kill count in various ways, but none work.

Probably because of mistake below:

> The idea is to have
> 
> "orc -".. player.all_kills[orc]
> without hardcoding it for every creature possible in my game.

That should be:

"orc -".. player.all_kills["orc"]

or (equivalent):

"orc -".. player.all_kills.orc

> Heelp?

Try this:
player.all_kills={orc=12, dragon=5, hero=2}

player.all_kills=setmetatable(player.all_kills, {
  __tostring=function(self)
    local buf={}
    for k,v in pairs(self) do
      buf[#buf+1]=k..'-'..v
    end
    return table.concat(buf, ' ')
  end
})

print(player.all_kills)

Works in pure lua, don't know about the version in this game engine.

Regards,
miko