Garry's Mod Instances

From GMod Wiki

Jump to: navigation, search

If you remember, in the first lesson I talked briefly on instances. I'm going to explain in further detail about them here.

Theory

As a bit of a recap, I'll remind you there can be up to three instances at once - the Server Instance, the Client Instance, and the Menu Instance. When loading a script, a Lua Instance turns the plain text inside of it into code. This process is called compiling, and is done every time a script is loaded. Right after the code is compiled, it runs on the instance it was compiled on. Scripts can be loaded as much as you like. You can load a script, modify it outside of the game, and load it up again as much as you want.

In Practice

So, to load a script on the server instance, I would do:

lua_openscript myscript

Now this script is run on the server instance.

If I wanted to load a script on the client instance, I would do:

lua_openscript_cl myscript

Now this script is run on the client instance.

It's important to note that anything a script does once loaded can't be reversed automatically. Loading a script is like dumping a bucket of oil in a pond. Once it gets in there, you need to drain the whole pond to get rid of it - or, should I say, you have to restart Garry's Mod or change the map to clear any loaded scripts from the instances.

The reason a script cannot be taken out once it is loaded is because Lua has no idea what your script did, or how to reverse it. You can, however, clean up anything loaded by your script very easily, but I won't cover this here. Maybe in the next lesson.

Now, with that out of the way, lets move on.

Instance Occurrences

I mentioned that there could be two instances at once maximum - the client and server instance. HOWEVER - these two instances won't always be together.


1. NewerShared.png

This is used on both the server and the client.

2. NewerServer.png

A Garry's Mod dedicated server (which is the name of a program that runs on a dedicated server machine) only has the Server Instance on it.
As such you cannot run clientside scripts on a dedicated server.


3. NewerClient.png

The Garry's Mod multiplayer client (which is running whenever a client joins a multiplayer game) has the Client Instance and shared instance on it. So, you cannot run serverside scripts on the Garry's Mod client.
  • NOTE: You don't need to make clients download serverside scripts! Serverside scripts are only needed for the server, not the clients!

4. Menu.png

The menu has its own instance, the menustate. The menustate is similar to the client instance, but it's only accessible in the menu or when ESC is pressed and the most functions of the client instance aren't available there.

Hopefully I've helped you understand this a little bit better. As far as the tutorial goes, we won't be applying this stuff we learned right now, but in the future we will be. Just thought it's important I got it in.


Yarin Kaul Icon ArrowSquare32 left.png Parentheses with arithmetic

Yarin Kaul Icon ArrowSquare32.png Back to Lua Tutorial Series

Hooks Yarin Kaul Icon ArrowSquare32 right.png


Personal tools
Namespaces
Variants
Actions
Navigation
Lua Scripting
Functions
Hooks
Toolbox