Garry's Mod

Garry's Mod

Niums Electricity
niumikus  [developer] 21 Jul, 2021 @ 8:45am
Making an Extension
Must Contain methods:
master = ENT:NEGetMaster(void)
-> when called, an entity must return its master.(may return self)(may NOT return invalid master)
-> this is for when an entity is made of subentities
-> just return self if the entity is not a subentity of another one.

connections = ENT:GetNEConnections()
-> MUST return all connections in a table, may be nil.
->"if self.plug then return {self.plug.NEConnectedTo} end". works in most cases
-> if returning a table, content MUST NOT be nil!


Must Contain for Devices:
watt = ENT:NEPreCalculate(void)
-> This method must return a value of how much energy it will be producing or consuming,
in watts. Use negative values for consumption
(DO NOT execute any actions in here! only return requirements)

ENT:NECalculate(net_wattage)
-> This function will be parsed the net wattage amount.
-> this number may be negative! if negative your device does not get all the power it needs! slow your device if this value is negative or disable it completely.
in this method call you must execute the actual actions of the machine.
->Irrelevant for generators.

You can technically not include these methods, but i advise using them


Optional methods:
watt = ENT:NEMidCalc(watt_net)
-> will parse the amount of watts needed/overflow for the entire grid. MUST return an amount of watt to be added/deducted from the grid (may be 0)(may not be nil)
-> This is called before NECalculate and after NEPreCalculate
-> Important: the watt_net may have been changed by those this method has been called on first.
-> suitable for accumulators.

watt = ENT:NEPostMidCalc(watt_net)
-> same as above
-> is called after NEMidCalc
-> suitable for stopping your generator from overproducing.

ENT:NEProbeCalc(total_watt)
-> parses the total net value of energy in the entire grid.
-> this will be called after NEMidCalc and before NECalculate

success, position, angles = ENT:NEPlugRequest(plug)
->Will receive a plug request from ent, must return success or failure(boolean). on sucess must return position and angles for plug to attach to.
-> entity MUST freeze itsself if it has active connections.

ENT:NEDisconnect(ent) [INTERNAL] --ONLY use if making custom sockets.
->when called, disconnect from entity! you may also call it on an entity to disconnect from it.
->the addon usually handles this ittself, so concider it [INTERNAL]


Optional Variables:
Entity.NEPlugable
-> set to true if a plug can attach to your object.
-> must contain method Entity.NEPlugRequest if set to true

Entity.NERemoving
->set to true if its to be ignored when updating the grid.(usually only used in OnRemove)

Must calls:
NElecLib.CalculateGrid(self)
-> call this every time your entity changes the amount of power it produces or consumes.

Must Dos and Don'ts:
Make your entity saveable!
-> Use ENT:PreEntityCopy() for saving data.
-> and use ENT:PostEntityPaste(_,_,tab) to load data!
-> if you want to save an entity for reference, save its EntIndex() instead, and on PostEntityPaste(_,_,tab)
use your entity index as an index in the table, this will return your desired entity(tab[entityIndex])
->you have to save the connection to your plug this way. If you're having problems doing this, just comment and i will help.

on ENT:Remove(), first set self.NERemoving to true, then call NElecLib.CalculateGrid(self)



Library:
plug = NElecLib.CreatePlug(ent, posOnEntity,direction, distance, cableAddLength )
->Creates a plug for entity: ent (this should usually be self)
->On a generic consumer/producer with no addtional plugs, the plug will handle all the connection- and gridwork, so you dont have to worry about connecting, disconnecting etc. just have your methods ready.
-ent: The entity to be created a plug for. (Entity)
-posOnEntity: the position local to the entity to create a plug on(dont worry about rotation, it does this automatically) (Vector)
-direction: the direction the plug should be created in.(Normalized vectors only!)
-distance: the distance the plug should be created at. (integer)
-cableAddLength: Extra length to add to the cable. (integer)
-> DO NOT call in ENT:Initialize(), here you are unable to check if plug already exists and creating another would be bad.

NElecLib.CalculateGrid(ent)
->The entity whos grid should be updated and calculated.
->DO NOT call in ENT:Initialize OR ENT:PostEntityPaste OR ENT:OnDuplicated
-> calling this inside these methods can cause errors and undefined behaviour
-> instead, save the values of the last state your entity was in.
->DO CALL when your entity has changed the state of how much power is producing/consuming
Last edited by niumikus; 23 Jul, 2021 @ 8:10am
< >
Showing 1-2 of 2 comments
Comrat 11 Feb @ 12:31pm 
I want to make my own addon but I'm confused on how to actually use all of this in lua
niumikus  [developer] 11 Feb @ 12:42pm 
Friend me, i'll help you with whatever you need.
< >
Showing 1-2 of 2 comments
Per page: 1530 50