• Print

Author Topic: Making a gamemode  (Read 7083 times)

0 Members and 1 Guest are viewing this topic.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Making a gamemode
« on: January 20, 2017, 09:21:46 am »
So I'm really interested in making my own gamemode, and I've got an idea for it, too (it's based off of Town of Salem), but I was wondering if anyone could help me with structuring. I know the only required files are 'shared.lua', 'cl_init.lua' and 'init.lua', but when I was talking to some people when I joined a gamemode-making contest that was on Facepunch (I ended up leaving because I had no time for it) and they recommended having more than just that. What I was wondering is, I'm not sure what kind of files I'll need to make and how they'll all go together and such.


I can give more information if needed because this is pretty vague, you can find my Trello page here.
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Making a gamemode
« Reply #1 on: January 20, 2017, 03:36:15 pm »
It's best to structure your gamemode so that it's easy for you to find things.

If you have a set of functions designed for loading/saving players, you might have a sv_playersave.lua file or something like that.

If you want to keep your custom hud code separate, you could have a cl_hud.lua

That being said.. garrysmod will only load 3 files by default when your gamemode starts.

init.lua
cl_init.lua
shared.lua

You'll need to include your files you wish to load inside of those files. As well as AddCSLuaFile them if they are client or shared files.

Personally, I have a modules folder inside my gamemode folder that I iterate through and include all of the files in my gamemode. This makes adding new files very easy.

This is what the top of my init.lua file looks like.

Code: Lua
  1. AddCSLuaFile( "cl_init.lua" )
  2. AddCSLuaFile( "shared.lua" )
  3. include( "shared.lua" )
  4.  
  5. local files, dirs = file.Find("gmstranded/gamemode/modules/client/*.lua", "LUA")
  6. for k, v in pairs( files ) do
  7.         ServerLog("Stranded: Loading module (" .. v .. ")\n")
  8.         AddCSLuaFile( "gmstranded/gamemode/modules/client/" .. v )
  9. end
  10.  
  11. local files, dirs = file.Find("gmstranded/gamemode/modules/server/*.lua", "LUA")
  12. for k, v in pairs( files ) do
  13.         ServerLog("Stranded: Loading module (" .. v .. ")\n")
  14.         include( "gmstranded/gamemode/modules/server/" .. v )
  15. end
  16.  

Top of my cl_init.lua:

Code: Lua
  1. include( "shared.lua" )
  2.  
  3. local files, dirs = file.Find("gmstranded/gamemode/modules/client/*.lua", "LUA")
  4. for k, v in pairs( files ) do
  5.         print("Stranded: Loading module (" .. v .. ")")
  6.         include( "gmstranded/gamemode/modules/client/" .. v )
  7. end
  8.  

And, top of my shared.lua file:
Code: Lua
  1. local files, dirs = file.Find("gmstranded/gamemode/modules/*.lua", "LUA")
  2. for k, v in pairs( files ) do
  3.         print("Stranded: Loading module (" .. v .. ")")
  4.         include( "gmstranded/gamemode/modules/" .. v )
  5.         AddCSLuaFile( "gmstranded/gamemode/modules/" .. v )
  6. end
  7.  

Essentially at this point, if I want to add a new shared file, I just create a new lua file in gamemode/modules and it loads the file in both the clientside and serverside lua states.

If I want to create just a clientside file, I add one to gamemode/modules/client

and likewise for serverside: gamemode/modules/server


I hope this points you in the right direction.. if you want more info or help, just ask.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Making a gamemode
« Reply #2 on: January 20, 2017, 03:42:08 pm »
Just as a side-note...

Before I started doing this, I also just put everything into 1 file (back when I first learned how to code gamemodes) and it wasn't that bad at first.. but...

My gamemode grew over time, and now there's some 50,000 lines of code or so in the gamemode folder and having all of that in just 3 files was becoming a real pain in the bum to handle and finding functions was hard. So, I split it up into files that made sense to me.

While yours certainly won't look like mine, here is an example of the files that I have in my serverside modules folder. I still DO have code in my init file, but the idea is to keep all code of similar function in their own individual files.



This also makes it easy to work with other people on your gamemode since you might each be working on different files instead of trying to manage who has done what to the same file.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Making a gamemode
« Reply #3 on: January 20, 2017, 05:12:10 pm »
Thank you so much for that information. I don't have a really long attention span so keeping track of all of the files and if they should be on the server or client or shared is just so much for me, but I'll give it a shot.

Sent from Tapatalk. Owner of iViscosity Gaming.

I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Making a gamemode
« Reply #4 on: January 20, 2017, 07:38:49 pm »
Trust me, this is the best and easiest way to do this.

Take a look at how ULX and ULib are structures. Very similar.
If you are going to be writing something that is big (and gamemodes are big) then this kind of file structure is the way to go.

Also the different between server, client and shared is very easy..

Only clientside stuff such as HUD and derma element stuff is clientside, while most of your code will be server.
Information you want available to both the server and the client, functions that behave the same on both or static tables and variables that are the same in both states should go in your shared files.

If this is a concept that doesn't make sense to you, you probably shouldn't try writing a gamemode.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Making a gamemode
« Reply #5 on: January 21, 2017, 08:50:29 pm »
Ok so I wanna clarify, you have your init.lua, cl_init.lua, and shared.lua all in the 'gamemode' folder, and the rest on the 'modules' folder, is that correct?


EDIT: Also, have you given Sublime Text 3 a try? You can get a gLua syntax highlighter and I just like how it looks a lot better than Notepad++, just my opinion though :D
« Last Edit: January 21, 2017, 08:57:07 pm by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Making a gamemode
« Reply #6 on: January 22, 2017, 11:38:14 am »
That is correct, it's mostly just to keep it clean and organized. By default, only init and cl_init load. Everything else, including shared.lua has to be included. The reason I have my other files in a gamemode/modules folder is so that I can have that for loop I showed you automatically iterate and include/AddCSLuaFile all of my additional files and I don't have to include/AddCSLuaFile each one of them separately.

You could have everything right in your gamemode folder if you wanted, you would just need to include/AddCSLuaFile each one in the respective init/cl_init file as needed. My method just makes that easier for adding or removing new files. I just drop it in the folder it needs to be in and the gamemode handles everything else.



And, yes: I've tried Sublime. I don't like it. I have a GLua syntax highlighter for notepad++ and it works great.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Making a gamemode
« Reply #7 on: January 24, 2017, 05:06:20 pm »
Quick question, if a file is added to the client, they really don't need to be included in other clientside files because they have the code on their system, is that correct?
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

Offline MrPresident

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 2727
  • Karma: 430
    • |G4P| Gman4President
Re: Making a gamemode
« Reply #8 on: January 24, 2017, 07:33:42 pm »
It still needs to be included in the cl_init file. Just because a client has a file doesn't mean it gets run.

The gamemode folder is like the top level lua folder when handling non gamemode stuff.. Scripts don't get run in that folder, but can be accessed. In a gamemode, there is no 'autorun' folder like in the lua folder, so the only files that get 'autorun' are the 2. init and cl_init. You need to include any other files you wish to execute.

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 803
  • Karma: 58
Re: Making a gamemode
« Reply #9 on: January 25, 2017, 03:33:03 am »
Makes sense. Thanks

Sent from Tapatalk. Owner of iViscosity Gaming.

I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.

  • Print