-
-
Notifications
You must be signed in to change notification settings - Fork 383
-
---@param dmginfo CTakeDamageInfo function foo(dmginfo) ---@type Player | Entity local attacker = dmginfo:GetAttacker() # attacker must be player after this! if not attacker:IsPlayer() then return end # but I still get errors like: Cannot assign `Entity|Player` to parameter `Player`. # How can i cast it as such and not Player|Entity end
I want to cast a variable type after declaration for the global scope, my idea is that after i check if the attacker is a certain type i want to cast it as just this type for all the code below
Beta Was this translation helpful? Give feedback.
All reactions
I believe that you ran into another strange known issue: #2326
where the @cast
is not working if you file doesn't have a trailing newline 🙈
Could you try to add a new empty newline at the end of your file?
Replies: 2 comments 3 replies
-
use @cast
https://luals.github.io/wiki/annotations/#cast
=> ---@cast attacker Player
Beta Was this translation helpful? Give feedback.
All reactions
-
I've already tried.
image
function ENT:OnTakeDamage(dmginfo) ---@type Player|Entity local attacker = dmginfo:GetAttacker() if not attacker:IsPlayer() then return end ---@cast attacker Player local wep = attacker:GetActiveWeapon()
Beta Was this translation helpful? Give feedback.
All reactions
-
I believe that you ran into another strange known issue: #2326
where the @cast
is not working if you file doesn't have a trailing newline 🙈
Could you try to add a new empty newline at the end of your file?
Beta Was this translation helpful? Give feedback.
All reactions
-
🎉 1
-
good catch!, works wonders now, would have never guessed it myself
Beta Was this translation helpful? Give feedback.
All reactions
-
😄 1
-
If it’s Vscode-EmmyLua, You can use return_cast here to enable automatic inference.
Beta Was this translation helpful? Give feedback.
All reactions
-
👍 1