Can't figure out the best way to annotate game code (example inside!) #2458
Replies: 3 comments 4 replies
-
One other thing that bugs me, consider this: actions.isSupport = {
supportAttack = true,
supportDefence = true,
supportBoth = true,
supportAttackPike = true,
supportDefencePike = true,
}
---@param action ActionName
---@return boolean
function actions.IsSupport(action)
return actions.isSupport[action]
end When hovering over
Now when I annotate the table ---@type table<ActionName, boolean>
actions.isSupport = {
supportAttack = true,
supportDefence = true,
supportBoth = true,
supportAttackPike = true,
supportDefencePike = true,
} I get less verbose typehints: Can I annotate it and make sure it's still verbose when hovering? |
Beta Was this translation helpful? Give feedback.
-
It seems the better way to go about this is using class: ---@class ActionTable
---@field [ActionName] ActionName[]
---@class ActionTable
local forbids = {} This ends up giving me this kind of type hints:
Which is ALMOST what I want, now I just need to find a way to make sure actual value also gets displayed along the |
Beta Was this translation helpful? Give feedback.
-
I added a longer explanation on your issue, but here is some (untested) example code more tuned to your use: local actions = {}
---@enum actions.all
actions.all = {
retreat = 1,
move = 2,
swap = 3,
forcedMove = 4
}
---If you need the names of actions, you can index this
actions.names = {
"retreat",
"move",
"swap",
"forcedMove"
}
---Get the name of an action
---@param action actions.all
function actions.getName(action)
return actions.names[action]
end
local character = {}
---@param action actions.all
function character:setAction(action)
self.action = action
end
function character:getAction()
return self.action
end
---Example usage
character:setAction(actions.all.retreat)
local characterAction = character:getAction()
local characterActionName = actions.getName(characterAction)
-- "Character is performing action: retreat"
print("Character is performing action: " .. characterActionName) |
Beta Was this translation helpful? Give feedback.
-
I'm new to Lua, essentially trying to help my friend right now with their game project. It has existing code, I wish to add some type annotations to it so that it's easier to understand what's going on and have nice IDE tooltips, I'm using vscode plugin right now.
So, here's my simple case, I have table:
Now first of all simply using enum on it won't give me nice typehints (I even made an issue here : #2435)
but I found a way to get around it like this:
I can now nicely use
ActionName
as parameter in functions, but I get warnings and otherwise not-very-useful typehints when doing sth like this:It says:
Fields cannot be injected into the reference of `table<ActionName, ActionName[]>` for `swap`. To allow injection, add `[any]: any` to the definition.Lua Diagnostics.(inject-field)
I'm probably not the first who tries to do sth like this, would appreciate any pointers / thoughts / links to guides! My background is mostly python (if that matters).
Beta Was this translation helpful? Give feedback.
All reactions