Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Town as plain Lua table #4764

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Conversation

ranisalt
Copy link
Member

@ranisalt ranisalt commented Jul 3, 2024

Pull Request Prelude

Changes Proposed

The Town metatable cannot be used to mutate towns, so it can be pushed as a table for a lighterweight implementation and to cleanup some functions in luascript

EPuncker
EPuncker previously approved these changes Jul 3, 2024
@@ -9765,7 +9766,7 @@ int LuaScriptInterface::luaPlayerGetTown(lua_State* L)
// player:getTown()
Player* player = tfs::lua::getUserdata<Player>(L, 1);
if (player) {
tfs::lua::pushUserdata(L, player->getTown());
pushTown(L, *player->getTown());
tfs::lua::setMetatable(L, -1, "Town");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary, since the pushTown method already adds the metatable

if (town) {
tfs::lua::pushUserdata(L, town);
pushTown(L, *town);
tfs::lua::setMetatable(L, -1, "Town");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not set the metatable, since it is already added in the pushTown method

@@ -0,0 +1,24 @@
Town = {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is never read, it must be added to the core.lua file

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is also necessary to add the __index metamethod that refers to the same table

Town.__index = Town

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

setField(L, "name", town.name);
tfs::lua::pushPosition(L, town.templePosition);
lua_setfield(L, -2, "templePosition");
tfs::lua::setMetatable(L, -1, "Town");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding the metatable is impossible, since the table Town does not actually exist in the registry LUA_REGISTRYINDEX this is because the TFS API registers the metatables with the registerClass method in this index

so it will be necessary to search for the table in the global environment and then apply it as a metatable manually

	lua_getglobal(L, "Town");
	lua_setmetatable(L, -2);

Copy link
Contributor

@ramon-bernardo ramon-bernardo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are a few considerations: as I always say, we can use auto whenever possible ;)

@@ -1219,7 +1219,7 @@ class Player final : public Creature, public Cylinder
Party* party = nullptr;
Player* tradePartner = nullptr;
SchedulerTask* walkTask = nullptr;
Town* town = nullptr;
const Town* town = nullptr;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you explain why it became a const?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is assigned with the return value of getTown (i.e. player->town = towns->getTown(id)) which became const Town*

{
auto it = townMap.find(townId);
if (it == townMap.end()) {
return nullptr;
}
return it->second;
return it->second.get();
}

const TownMap& getTowns() const { return townMap; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use auto and keep the type inferred directly from the townMap variable ;)

Copy link
Member Author

@ranisalt ranisalt Sep 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather do it separately. Probably would change to decltype(auto) getTowns() const to avoid copying

However, end goal is to not have anything related to towns in the core. The concept of a town only makes sense for the game, not for the engine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants