Facepunch / garrysmod-requests

Feature requests for Garry's Mod
83 stars 24 forks source link

Lua bindings to Lua refresh #1376

Open Dragoteryx opened 4 years ago

Dragoteryx commented 4 years ago

Lua refresh is a great functionnality, but when a file that requires another file to executed first is refreshed it just causes an error. For example, when you are working on an entity, and refresh the entity's shared.lua, it recreates the ENT table, refreshes the file, then loads the new ENT table. This behaviour is impossible to recreate in Lua.

Let's say for example I've made a custom bank class, with each bank stored in let's say for example lua/banks/, I need to iterate over every Lua file that matches lua/banks/*lua.

And I repeat those steps for every bank in lua/banks. So far everything is working as expected.

Then you want to change something inside the BANK table, so you make your change, save the file, Lua refresh happens and says that the BANK table isn't defined.

The only way to fix this is to define the BANK table yourself at the start of the file then call a function at the end of the file and provide it the BANK table, but that means you have to do yourself what the ENT table does seamlessly.

What could fix this would be 2 hooks called GM:BeforeLuaRefresh(fileName) and GM:AfterLuaRefresh(fileName), that would let you execute code before and after the file is refreshed. Maybe it would be possible to return true inside GM:BeforeLuaRefresh to prevent the file from refreshing as a bonus?

That would also let you log Lua refreshes and know exactly what files got refreshed and what files didn't, which would make it easier to debug your code.

thegrb93 commented 4 years ago

That's why it's good coding practice to only define functions within your lua files and do your initialization within those functions. So whenever a refresh occurs, only the functions get updated, and if you need further re-initialization you can call a restart concommand that calls your initialization functions.

Or you can be like me and just use a super fast SSD to restart the map after every change.