So I'm getting started with lua and I want to start with programming a very simple hitscan pistol.
Problem is so many tutorials have fluctuating information, some saying to place your weapon in your lua folder, or your addons folder, some saying to have 1 lua file, others saying I need a shared, client, and serverside file.
Anyone got the best tutorial that doesn't have non-corresponding information?
i have been nonstop coding lua for like a month and pretty much went trough a crash course. thing with lua is that there’s dozens of ways to do 1 thing. im on my phone so cant explain on lot but i can help you start with the basics and stuff on steam or whatever. let me know
[DOUBLEPOST=1501089089,1501087021][/DOUBLEPOST]
n, some saying to place your weapon in your lua folder, or your addons folder,
Both can work, however creating all your code into addons is easier. You'll have everything in 1 folder. would very recommend making pretty much all of your work in addons. there's alot of good wrappers for gmad to upload your addons(GAC) to the workshop.
some saying to have 1 lua file, others saying I need a shared, client, and serverside file.
this can work. You can make a shared client and server file or do it all in one.
Generally the way i like to lay it out is have my addon folder containing the following folder structure:
Addons/MyAddon:
- Lua
- Models (if you use models a player needs)
- Materials (if you use special images and stuff)
- scripts (cars usually)
and Addons/MyAddon/Lua:
- autorun
- Client
- Shared
- Server
in the autorun folder, you want a lua file named init.lua or something. Name doesn't really matter since it runs automatically.
From that lua file. I like to initialize shit, send the required files to clients and so on.
The best way to organise your code is to do every thing in a diffrent file.
So you have 1 file that starts up the mysql (lets name it mysql.lua) You obviously never want to send the mysql data to the client since that's plain dumb and wont work so you do this server side. Therefor, you put this server sided code in the server folder. Upon startup of the server you want mysql.lua to initalize so you have that out the way and can do all your mysql needs instantly.
Therefor, your init.lua should look like this
Code:
if CLIENT then -- only run code within this if statement if its the client
- we're not sending anything to the client right now.
end
if SERVER then -- only run code in this if statement if server side
include("server/mysql.lua")
end
Note that the include says server/mysql.lua and not just mysql.lua. When you do include() which is a shared function(meaning it will work on clients and servers) it is always relative to the lua folder(wether that be your addon folder or the gmod/lua folder) so if you have folder x in folder y. and folder y is in the lua folder. your include will look like:
include("y/x/file.lua")
You should only include lua files.
Note: if you include() something client side. Make sure your client has access to that code! you can do that using AddCSLuaFile("Fileinyouraddonsfolder.txt")
addCSLuaFile is relative to your addon folder
If you send a script to the client which depends on another script that has been sent to the client(like 2 diffrent vgui panels) you do include("scripyouneed.lua") in the one script. and put AddCSLua in your client if statement in your init file. this will look as follows.
Code:
if CLIENT then -- only run code within this if statement if its the client
AddCSLuaFile("scriptthatneedssomething.lua")
AddCSLuaFile("scriptthatisneeded.lua")
end
if SERVER then -- only run code in this if statement if server side
include("server/mysql.lua")
end
that is a crude crash course on basic lua. i suggest looking at other people's code on garrysmod.org and reading that. Then search those things up in the gmod wiki and see what everything does. make sure you understand your code and dont ctrl+v it
if you run into any issues let me know and i'll try to help you.
[DOUBLEPOST=1501089336][/DOUBLEPOST]
Okay, no matter what you are going to need a shared, client and a serverside file for what you are doing i believe,
no you dont
if it is going to be a weapon then i guess you should just throw it into the weapon file.
what file