Jump to content

Hook System


TylerH
 Share

Recommended Posts

File Name: Hook System

File Submitter: TylerH

File Submitted: 13 Dec 2009

File Category: Lua Scripts

 

This is a relatively simple hook system, a near duplicate of the functionality offered in Garry's Mod.

 

The API is rather simple:

  • GetHooks - Returns the entire table of hooks
  • Add(event_name, name, func) - Adds your callback function with the unique name to be called under event_name.
  • Remove(event_name, name) - Removes your previously set callback with the name from event_name
  • Call(event_name, ...) - Triggers all callbacks for the given event_name, passing the arguements ... (any that you want). Will also return any return values.

Here is a simple example:

In your entity, module, whatever:

function DrawMy****()
DrawRect(50,50,500,500)
DrawText("Cake",0,60)
end
hook.Add("HUDPaint","MyEntity_DrawMy****", DrawMy****)

 

In your fpscontroller.lua:

 

require('hook')

...

while ...

RenderWorld()

hook.Call("HUDPaint")

Flip()

end

 

Pretty easy...works for everything from core functionality up to high level game logic. People in Garry's Mod really abuse this system, yet it is somehow rather fast.

 

Click here to download this file

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

This is cool, but it's not all inclusive to an object. I think it would be better if Josh created a Draw2D() function for each object just like Update() & Spawn() so we don't have to mess around with the fpscontroller.lua file at all.

 

That seems a bit too specialized. Then you will have people who want an UpdateMatrix, Render, Draw2D, Draw3D, UpdatePhysics, OnCulled, OnCollision, all kinds of callbacks.

 

It is better if people can implement things theirselves.

 

I think the simplest set of callbacks for an entity would be:

Pre-Update

Pre-Draw

Post-Update

Post-Draw

 

 

Then you can use them for whatever you want.

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

There is already in a Collision() callback. I don't think drawing 2D is specialized. Every engine needs to be able to draw 2D and since in LE we have to do it at a specific location in the loop, it should expose a callback for it to lua so our entities can handle it.

Link to comment
Share on other sites

I don't disagree, I am just saying that a system to allow us to setup our own callbacks, however many, would be the best solution, though I guess that isn't easily doable since we can't play around in the engine (source) itself.

52t__nvidia.png nVidia 530M cpu.gif Intel Core i7 - 2.3Ghz 114229_30245_16_hardware_memory_ram_icon.png 8GB DDR3 RAM Windows7_Start.gif Windows 7 Ultimate (64x)

-----

IconVisualStudio16.png Visual Studio 2010 Ultimate google-Chrome.png Google Chrome PhotoshopLinkIndicator.png Creative Suite 5 icon28.gif FL Studio 10 MicrosoftOfficeLive.png Office 15

-----

csharp.png Expert cpp.png Professional lua_icon.png Expert BMX Programmer

-----

i-windows-live-messenger-2009.pngskype-icon16.pngaim_online.pnggmail.pngicon_48x48_prism-facebook.pngtunein-web.pngyahoo.giftwitter16.png

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...