Jump to content

Hooks


Josh
 Share

Recommended Posts

What do you think?:

 

AddHook(hookname,function)

RunHooks(hookname)

 

Example:

 

AddHook("UpdateHook", UpdateBullets() )

AddHook("UpdateHook", UpdatePlayers() )

 

Then somewhere in the main Lua loop:

 

RunHooks("UpdateHook")

 

This can be coded entirely in Lua. Does gmod do anything fancier than this?

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Well in his example there you could run x code whenever a number of Updatexxx functions are run. IMO I guess it has a use, but surely its a sign of bad design?

Programmer, Modeller

Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64

Visual Studio 2008 | Photoshop CS3 | Maya 2009

Website: http://srichnet.info

Link to comment
Share on other sites

You can add update loops to the main loop while keeping all the code contained in one file.

 

Let's say you include a script that has to update something every frame. Instead of adding extra code in the main loop, you can add a hook in the included script, and that way you only have to include a single file. It just makes it easier to mix and match components.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Rick, please pay $10, buy Garry's Mod, and browse through the Lua. The entity system that Leadwerks originally implemented in the editor in the pre-alpha releases was based on an adaptation of mine of how Garry's Mod handled entities.

 

The hook system is the same. garry's Mod uses it to allow addons and gamemodes to add hooks to be run when certain things are called. i.e. Setup my gamemode's Update function to be called when the game itself Ticks, or play a message on screen when the game calls the PlayerConnected hook.

 

It is a very powerful system.

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

I tend to agree with Rick - Garrys Mod is very adhoc and so Hooks were no doubt a very useful tool. But in LE its usefulness is considerably less. That said, it fits right into the whole speedy programming and ease of use that LUA brings to the party.

Programmer, Modeller

Intel Core i7 930 @ 3.5GHz | GeForce 480 GTX | 6GB DDR3 RAM | Windows 7 Premium x64

Visual Studio 2008 | Photoshop CS3 | Maya 2009

Website: http://srichnet.info

Link to comment
Share on other sites

The point it you shouldn't be coding your entire game into your main loop, that has to be the stupidest thing ever. You need to level your code and divide it into areas that execute and correlate to different levels of interactivity.

 

I mean, I definitely find it easier to code say my UI module and my AI module seperately, and then add them to the game loop via hooks, than to be fiddling with the main loop 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

The point it you shouldn't be coding your entire game into your main loop, that has to be the stupidest thing ever. You need to level your code and divide it into areas that execute and correlate to different levels of interactivity.

 

I mean, I definitely find it easier to code say my UI module and my AI module seperately, and then add them to the game loop via hooks, than to be fiddling with the main loop itself.

 

 

That makes sense. I think once we are able to remove the need for us to use the main loop this would be useful. We just don't seem to be there yet. The main loop is still in lua files and we need to specify that loop. I agree that it would be nice to have the main loop inside the exe. In order for that to happen we would need ways to set all sorts of properties to config what we want to use and how to use it.

 

A good start I think would be the camera. It would be nice if that was more module. Maybe it could have it's own lua file. That way you could just point to what lua camera file you want to use and it uses that style. That would allow the flexibility of people writing camera modules that people can just plug in because all FP or 3rd person cameras aren't all created equally for all game types. I think this would be better than creating a generic camera in the exe and only allow changing some settings because someone somewhere will need something totally unique that the settings exposed won't allow. So if it was just a lua file they could control it.

 

I'm picturing treating the camera as another entity that you drag into the scene. You could then drag it to another entity to make the target association and set a lua file for the camera in the properties. Then the lua file would control how the camera works.

Link to comment
Share on other sites

An info_camera node would be nice, with a Drop down in the property editor that had choices from a recursive search of the Models/Entities/Cameras folder, so it could display "First Person Camera (Leadwerks)", "Third Person Camera (Klepto2)", "Third Person Camera (TylerP9P)"

 

Where it reads the camera type (FPS, TPS, RTS, etc.) and the author from the Lua file of the camera entity, and adds it to the drop down list. Then at run time, info_camera basically relays to the selected camera entity, creates it, and then that implementation takes over.

 

Seems like the best way to get "Drag and Drop" abilities for an infinitely large possible number of camera configurations.

 

The possibilities are endless...

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...