garrysmod icon indicating copy to clipboard operation
garrysmod copied to clipboard

Add Entity:AddSpawnFlag and Entity:RemoveSpawnFlag

Open bloodycop6385 opened this issue 1 year ago • 4 comments

Adds NPC:GiveSpawnFlag and NPC:RemoveSpawnFlag In my opinion, it's easier to use than the bit. library, for those who don't understand it.

bloodycop6385 avatar Oct 17 '24 13:10 bloodycop6385

I feel like this should be an addition for all entities, not just npcs

Zaurzo avatar Oct 17 '24 20:10 Zaurzo

There's already an extension file for entity (includes/extensions/entity.lua) so just add it there, no need for a new file. Also, extensions are included manually within includes/init.lua, so this npc.lua file is never going to be included unless you add it to init.lua

Zaurzo avatar Oct 18 '24 21:10 Zaurzo

Yeah, fixed it

bloodycop6385 avatar Oct 22 '24 11:10 bloodycop6385

@robotboy655 I don't see the request review, so if you would kindly take a look at this.

bloodycop6385 avatar Oct 22 '24 11:10 bloodycop6385

Please do not include unrelated changes to your Pull Requests.

If you want to suggest further additions, open a second/new Pull Request.

robotboy655 avatar Nov 02 '24 23:11 robotboy655

You could inline newSpawnFlags into the SetKeyValue call if you care about micro-optimization, as it's not used anywhere else and therefore saves a variable.

Also, I don't think an explicit check if flags already exist is needed, that way you can add multiple flags in a single call, or just one at a time, leaving it up to the individual.

If you want to complete the set though, add SetSpawnFlags and ClearSpawnFlags, the latter being a C++ function along those other 2. Example:

function meta:SetSpawnFlags( Flags )
	return self:SetKeyValue( "spawnflags", Flags )
end

function meta:ClearSpawnFlags()
	return self:SetSpawnFlags( 0 )
end

function meta:AddSpawnFlags( Flags )
	return self:SetSpawnFlags( bit.bor( self:GetSpawnFlags(), Flags ) )
end

function meta:RemoveSpawnFlags( Flags )
	return self:SetSpawnFlags( bit.band( self:GetSpawnFlags(), bit.bnot( Flags ) ) )
end

Just a suggestion, and I'm not sure what robotboy655 thinks of such trivial functions.

neico avatar Nov 03 '24 11:11 neico

If you're going to nitpick then also mention that all instances of flag should be flags, including the function name, as you can add and remove multiple with each function.

neico avatar Nov 05 '24 06:11 neico

@robotboy655 Updated this once again, pls merge :( I have also tested it and it works

bloodycop6385 avatar Nov 13 '24 17:11 bloodycop6385

I don't think your recent changes are a good idea.

robotboy655 avatar Nov 13 '24 17:11 robotboy655

I don't think your recent changes are a good idea.

How come?

bloodycop6385 avatar Nov 13 '24 17:11 bloodycop6385

I don't think your recent changes are a good idea.

How come?

  • It is inconsistent with the other Flag entity methods. None of them utilize varargs.
  • I'm pretty sure varargs are slower. I haven't confirmed that though, I'm not certain

Zaurzo avatar Nov 26 '24 13:11 Zaurzo

I don't think your recent changes are a good idea.

How come?

* It is inconsistent with the other Flag entity methods. None of them utilize varargs.

* I'm pretty sure varargs are slower. I haven't confirmed that though, I'm not certain

I restored the previous version if that's the case. @robotboy655 pls merge

bloodycop6385 avatar Nov 26 '24 13:11 bloodycop6385