lua-users home
lua-l archive

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


On 3 December 2013 22:54, K M <katarzynamosz@gmail.com> wrote:
> I am making a module for T-Engine 4. The engine uses Lua with a smattering
> of C, while the modules are pure Lua.
>
> The source code for the module is here:
> https://github.com/Zireael07/The-Veins-of-the-Earth
>
> The list of issues is also available on git.
> Any contributors would be welcome.
> ***
> And now for the meat of this mail:
>
> I haven't updated the side branches in quite some time, but the thing I am
> having most problems with is a kill count.
>
> Every time a creature dies, this snippet is executed:
>
> if src and src.player then
>                 player.all_kills = player.all_kills or {}
>                 player.all_kills[self.name] = player.all_kills[self.name] or
> 0
>                 player.all_kills[self.name] = player.all_kills[self.name] +
> 1
>         end
>
> 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.
>
> The idea is to have
>
> "orc -".. player.all_kills[orc]
> without hardcoding it for every creature possible in my game.
>
> Heelp?

I'm not familiar with the engine, but I'd imagine you can do something
like this:
```
for name, kill_count in pairs(player.all_kills) do
    -- Replace print with the appropriate GUI display function
    print(name .. " - " .. kill_count)
end
```

If you wanted to display 0 for all things the player hasn't killed
yet, you'd need to get a list of every enemy in the game; possibly
from the engine (though I have no idea how you'd go about this).

Assuming you can get that list and it's in the format `["Enemy Name"]
= X` (X can be any non-nil value), you can use this to print the
player's kill counts for everything:
```
for name, _ in pairs(enemy_list) do
    print(name .. " - " .. (player.all_kills[name] or 0))
end
```

On a side note, you can default to 0 and increment the kill count on
one line by replacing this:
```
player.all_kills[self.name] = player.all_kills[self.name] or 0
player.all_kills[self.name] = player.all_kills[self.name] + 1
```

With this:
```
player.all_kills[self.name] = (player.all_kills[self.name] or 0) + 1
```

If using two lines was an intentional choice, feel free to ignore this.

Regards,
Choonster