Team Spartan Cookies & Milk Forums
A very useful new feature - Printable Version

+- Team Spartan Cookies & Milk Forums (https://teamscm.co.uk)
+-- Forum: Garry's Mod (https://teamscm.co.uk/forum-4.html)
+--- Forum: Starfall (https://teamscm.co.uk/forum-7.html)
+--- Thread: A very useful new feature (/thread-883.html)



A very useful new feature - Person8880 - 25-04-2015

I've added a new function that I'm hoping people will find very useful.

You can now extend existing types in Starfall (local to your instance only), which allows you to add any number of new methods to types.

A simple example:
local Player = GetTypeDef( "Player" ) --Gets the "Player" type.
local DonatorGroups = {
donator = true,
donatingmember = true
}
--Add a function to determine if a player is a donator.
function Player:isDonator()
return DonatorGroups[ self:getUserGroup() ] ~= nil
end

print( ents.owner():isDonator() ) --true if you're in either donator group
This can be applied to every type, and obeys inheritance, i.e adding to the entity type makes the method available to all types of entity.

A few things to note:
  • Attempting to add non-function values will throw an error.
  • You cannot override existing methods, only add new ones.
This also means if you want to reimplement a removed method, you can now do so for yourself. Enjoy!