Lua and ENet upgrades

Summary: In the next release of Knights I will be upgrading the versions of ENet (a networking library) and Lua that are used by Knights. I will also be removing some custom patches to Lua that were being used previously. This will have the following effects:

  • Users will have to upgrade their game client (because the new ENet version is incompatible with the old one).
  • The Lua code for mods might need to be updated.

Lua Change

I have updated the version of Lua used to the latest version, 5.4.6 (previously it was 5.1.4).

The previous versions of Knights also included a custom Lua patch (written by me) which added a new “&” operator to the language. This was used for merging tables, for example one could write code like the following:

i_wand_of_destruction = kts.ItemType(
  basic_wand & {
    melee_damage = 1000,
    melee_stun_time = rng_time_range(2, 3),
    melee_tile_damage = 1000
  }
)

This would make an ItemType that had all of the properties from the “basic_wand” table, together with the new properties shown.

Adding the “&” operator to Lua seemed like a good idea at the time — but it does cause problems, because it means I can’t easily upgrade to newer versions of Lua (as I would have to port my patch across to the new codebase every time).

So, from now on I am removing this patch, and the “&” table-merge operator will no longer be available.

Instead I have written a Lua function that does the same thing:

function table_merge(a, b)
    local result = {}
    for k, v in pairs(a) do
        result[k] = v
    end
    for k, v in pairs(b) do
        result[k] = v
    end
    return result
end

This function is available as “kts.table_merge”. Therefore the above “wand of destruction” code must now be written like this instead:

i_wand_of_destruction = kts.ItemType(
  kts.table_merge(
    basic_wand,
    {
      melee_damage = 1000,
      melee_stun_time = rng_time_range(2, 3),
      melee_tile_damage = 1000
    }
  }
)

This does make the code a little bit uglier I suppose, but it is not a big problem and I think this is a small price to pay, in exchange for making it much easier to do Lua upgrades in the future.

Of course, any mods that were using “&”, must now be modified (no pun intended) to use “kts.table_merge” instead, but this shouldn’t be too difficult.

ENet change

I have also taken the opportunity to upgrade ENet from version 1.2.5 to the latest available version (on Windows this will be 1.3.17, as this is the latest VCPKG package available; on Linux it will be whatever is installed on your local system, which will hopefully be reasonably up to date).

The main issue here is that ENet 1.3.x is incompatible with 1.2.x, so users running the older versions of Knights will not be able to connect to the new Knights server. They won’t even get any message telling them they need to upgrade; the connection will just fail completely.

To mitigate this, I’ve arranged for the “Connect to Server” screen to show a message like the following when players try to connect:

Screenshot showing error message asking the player to upgrade to the latest version of Knights.

Hopefully this will be enough to let players know what they have to do, and it won’t be a huge issue.

Leave a Reply

Your email address will not be published. Required fields are marked *