Need some help with the usage of some basic concepts

Hey guys!

As you may understand i’m kind of new to the Gmod lua coding as i’m more of an VB.net/C# coder but well… there it’s allways fun to do something new!

But well guys, I would like to ask you for the help with the concepts of :

Meta // Well, I googled around and what I got is that the word meta means that something is an abstraction from another concept

Hooks // I just can’t understand the concept of using hooks. I just can’t get what they mean and do. And why and how do you use hook.add ?!

: and . // Well, i know how to use them but i would be really glad if someone would be nice and explained these operators.

Well, that’s about it!
So yeah , thanks in advance!

Mike

A metatable in Lua is a table that describes how other tables (and userdata) handle certain events, such as being indexed or called.

Hooks are a way to call functions when certain events occur in the gamemode, for example, adding a hook to the PlayerDeath event will cause your function to be called whenever a player dies.
[lua]local function playerDeath(…)
print(…)
end
hook.Add(“PlayerDeath”, “playerdathhook”, playerDeath[/lua]

The . is used to index a table, for example, if you had a table called foo
[lua]local foo = {}[/lua]
and you add a member to that function called bar
[lua]foo.bar = 2[/lua]
you can index that function with the . operator
[lua]print(foo.bar) – outputs 2[/lua]

The : operator is for calling methods on an object (a table)
When you call the function with : a “hidden” argument is passed to the function which is just the table that is being called on, eg
[lua]object:method()
object.method(object)[/lua]
these are the same.
Inside the function, this hidden argument can be accessed with the variable self.
[lua]function foo:getBar()
print(self.bar)
end[/lua]

I hope I explained that well enough, generally I am bad at explaining things :downs:

[editline]09:41PM[/editline]

I just read through what I posted and it was a pretty poor attempt to explain it, but oh well.

  1. Meta is commonly used when dealing with metatables. I’m sure someone else will be able to tell you what exactly they are but all you need to know is that you can use them to make your life easier by adding methods to objects.

Say I was coding a money system and wanted an easy way to give money to players. I can get the metatable of the player object and add to it.
[lua]local meta = FindMetaTable(“Player”) – Note that meta is just the name I chose, it could be named anything

function meta:AddMoney(amount)
self.Money = self.Money + amount
end[/lua]

And as you can see when defining methods you can use self to reference to the object the method is being used on, that’s another advantage.

  1. Hooks allow you to execute code whenever a certain event happens, some hooks even allow you to return a value to change how the game will act. Most hooks also give you useful values that you can use.

Example :
[lua]hook.Add(“PlayerSpawn”, “MyFirstHook”, function(ply)
if ply:Nick() == “satzeN” then
ply:SetHealth(1000)
end
end)[/lua]
Here we’re hooking to the playerspawn event, so everytime a player spawns this code will be ran and ply will represent the spawning player.

[editline]04:44PM[/editline]

I knew I would get Ninja’d. :ninja:

Thanks mate!
You really made it clear for me!

But i feel that i still don’t get the Meta part , but the rest is 100% clear for me mate!

Thanks again!
:wink:

  • Well, i was going to reply to MakeR but this one is also true for Quebecs helpful answer to! ;p

You can read about metatables here.

thanks again!

[editline]09:17PM[/editline]

1 more question guys!

If I code a function like that:

function GM:CanPlayerSuicide( ply )
return true
end

Then why don’t i need to use any hook for it?
Is because it’s an allready registered function that i’m just “editing” or ?

bumb , also 1 i’ve came upon 1 more question ,
How do I use the “networked” functions like GetNetworkedInt etc?
What’s the main plus of using these instead of the normal variables?

Sorry for asking that many questions, but well… i would like to have a solid ground to build on! :slight_smile:

You could do this serverside:

[lua]
hook.Add(“PlayerInitialSpawn”, “CheckOwner”, function( ply )
if ply:Nick() == “satzeN” then
ply:SetNetworkedBool(“OWNER”, true )
end
end )
[/lua]

And then, ANY player could do something like:

[lua]
for k, v in pairs( player.GetAll() ) do
if v:GetNetworkedBool(“OWNER”) then
print( v:Nick() … " is the owner!" )
end
end
[/lua]

so , we can basicly say that’s a lua version of an shared variable right`?

And please could you answer on the question before this? Thanks

Those functions are already defined in the base gamemode, when you are making a gamemode you can override them like you are doing there. If you are not making a gamemode then you can’t override those functions like that, you need to hook onto them.

Thanks again dude! You rock!

Now the only 1 that i don’t get is the Networking one!

[lua]
function GAMEMODE:PlayerInitialSpawn( ply )
blalbal
end
[/lua]

?

Yes, I know. I wanted to keep it as simple as possible. What I meant by can’t is that you shouldn’t.

I still didnt get any real explanation of the Networked ones , would be awesome if someone of you could explain it a bit better :wink:

Networked variables are set on the server on entities using SetNetworkedBlah(“Name”, value). That value is then sent to all clients that can see the entity (So for all practical purposes, all players) that can retrieve it using GetNetworkedBlah(“Name”) on the same entities.

So you should be using it when you you have a value you want to share to every player. A good example in a roleplay gamemode would be a player’s title because every client needs to know about it. A bad example would be a player’s money. You don’t need every client to know how much every other client has.

So yes, it is a shared global variable that is unique to the entity it was set on.

:byodood:

[editline]07:50PM[/editline]

bufer overfle in net mesage xd xd

Fine, never use NetworkedVariables, ever.

Awesome! ;p

[editline]09:04PM[/editline]

BUMB…again
with a new question!
Right now i’m creating a gamemode to just teach myself the usage of … yeah basicly everything! But i usually come upon the problem with no real error message in the console but without the gamemode loaded!

The only message that i get is :
Registering gamemode ‘SpecialOPS’ derived from ‘base’

Ops… didnt see this one :
ScriptEnforce: “specialops\gamemode\cl_init.lua” blocked

but why is it blocked?!

Example:
http://www.filefront.com/16184141/Wtf%20Example.zip

another question -
if i declare something local then is it a function only variable or is it a current file only? or is it “both”?

any 1 know why the “ScriptEnforce: “specialops\gamemode\cl_init.lua” blocked” happens then `?

I think the new Scriptenforcer blocks anything not on the clientside Lua file list Are you doing AddCSLuaFile(“cl_init.lua”) somewhere in your init?

About local, it only exists in the code block it’s in. Be that a function, and if, a loop, whatever. If I run this code:

[lua]
if true then
local x = 5
end
print(x)
[/lua]

it’ll print nil, because x is local to that if block and it doesn’t exist outside it. If you make a local variable outside any statement block, it’s local to the file.