LUA:Money System Part 1
From GMod Wiki
Lua: Money System |
Description: | A simple tutorial on how to create a basic money system. |
Original Author: | KillerLUA |
Created: | March 12, 2010 |
Contents |
Introduction
In this tutorial, I'm going to tell you how to create a basic money system in your garrys mod gamemodes. In a later tutorial, I will show you how to get a basic money hud working. For now, Let's get started on our money system!
Requirements
Knowledge Prior to Using the Tutorial:
- A pre-coded gamemode
- Some Lua experience
- A brain
What you're going to learn:
- How to use meta tables
- Adding new functions to meta tables
- How to store and get networked integers (whole numbers)
- A simple way of saving variables
Setting up the files
First, we are going to create a new file in our gamemode called 'sh_player.lua'
Next, we want the file included by the client and the server.
Add the following lines of code to init.lua (Serverside code of your gamemode):
include( 'sh_player.lua' ) AddCSLuaFile( "sh_player.lua" )
include( 'sh_player.lua' ) - Includes the file, including all it's functions. The code is also run on the server start AddCSLuaFile( "sh_player.lua" ) - Makes the player download the clientside file
Then, add these lines to cl_init.lua (Clientside code of your gamemode):
include( 'sh_player.lua' )
Used the same way as above.
Meta Usage
Now, go to your sh_player.lua file, add the following code. Then, I'll explain it's usage.
local meta = FindMetaTable("Player") --Get the meta table of player
Basicly, we define meta with all the functions that can be called via ply:SomeFunction()
To add functions, we use simple code like this:
local meta = FindMetaTable("Player") --Get the meta table of player function meta:WowFunction(command) self:ConCommand(command) self:ChatPrint("Wow function ran the console command: " .. tostring(command)) end
Now, we can call it like any ordinary player function!
ply = LocalPlayer() ply:WowFunction()
This is how are cash function is going to work, first we are going to draw up a list of functions:
player.SetMoney( amount )
player.GetMoney()
player.AddMoney( amount )
player.TakeMoney( amount )
player.SaveMoney()
player.SaveMoneyTXT()
Let's begin by coding all these functions,
First, the set money function,
function meta:SetMoney(amount) self:SetNetworkedInt( "Money", amount ) self:SaveMoney() end
Now, we will add the get money function
function meta:SetMoney(amount) self:SetNetworkedInt( "Money", amount ) self:SaveMoney() end function meta:GetMoney() return self:GetNetworkedInt( "Money" ) end
Now, for the add money function. Which basicly calls the set money function.
function meta:SetMoney(amount) self:SetNetworkedInt( "Money", amount ) self:SaveMoney() end function meta:GetMoney() return self:GetNetworkedInt( "Money" ) end function meta:AddMoney(amount) local current_cash = self:GetMoney() self:SetMoney( current_cash + amount ) end
To finalize everything on meta usage, here is the full code:
local meta = FindMetaTable("Player") --Get the meta table of player function meta:AddMoney(amount) local current_cash = self:GetMoney() self:SetMoney( current_cash + amount ) end function meta:SetMoney(amount) self:SetNetworkedInt( "Money", amount ) self:SaveMoney() end function meta:SaveMoney() local cash = self:GetMoney() self:SetPData("money", cash) end function meta:SaveMoneyTXT() file.Write(gmod.GetGamemode().Name .."/Money/".. string.gsub(self:SteamID(), ":", "_") ..".txt", self:GetMoneyString()) end function meta:TakeMoney(amount) --Add money function here self:AddMoney(-amount) end function meta:GetMoney() return self:GetNetworkedInt( "Money" ) end
Now continue to part two on how to finish it off. I will also give an example of how it can be used.