SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Thread Tools Display Modes
Old 13/07/2018, 08:56 AM   #1
[HLF]Southclaw's Avatar
Join Date: Apr 2009
Location: England
Posts: 4,947
Reputation: 1510
Default Environment Variables


Provides access to environment variables in Pawn.

Rule III of the Twelve Factor App states:

Store config in the environment

An app’s config is everything that is likely to vary between deploys (staging, production, developer environments, etc). This includes:
  • Resource handles to the database, Memcached, and other backing services
  • Credentials to external services such as Amazon S3 or *******
  • Per-deploy values such as the canonical hostname for the deploy

Apps sometimes store config as constants in the code. This is a violation of twelve-factor, which requires strict separation of config from code. Config varies substantially across deploys, code does not.

(read the rest here)


Simply install to your project:

sampctl package install Southclaws/pawn-env
Include in your code and begin using the library:

#include <env>
Not a sampctl user? See the GitHub for source and binaries.


This package provides one function GetEnv which stores into dest the environment variable identified by name and returns the length of the result.

For example, this code loads a MySQL password from an environment variable:

main() {
    new mysqlPassword[128];
    ret = GetEnv("MYSQL_PASSWORD", mysqlPassword);
    if(ret) {
        // use mysqlPassword to connect to DB
    } else {
        print("Environment variable `MYSQL_PASSWORD` not set.");

On Windows, to build the standard Windows .dll file: open CMakeLists.txt with Visual Studio 2017+ and develop/build using MSVC tools. To build the Linux build on Windows, run make build-linux and Docker will be used to compile it. On Linux, use CMake: mkdir build && cd build && cmake .. && make.


To test on Windows or Linux with the need for containers:

make test-windows
To the Linux build on Windows with a Docker container:

make test-linux



[HLF]Southclaw is offline   Reply With Quote
Old 13/07/2018, 09:13 AM   #2
Big Clucker
Verc's Avatar
Join Date: Apr 2018
Location: 🇯🇵
Posts: 174
Reputation: 29
Default Re: Environment Variables

What is this used for? Can you explain more?
Verc is offline   Reply With Quote
Old 13/07/2018, 09:15 AM   #3
coool's Avatar
Join Date: Feb 2013
Location: Republic of Pakistan
Posts: 587
Reputation: 34
Default Re: Environment Variables

coool is online now   Reply With Quote
Old 13/07/2018, 10:14 AM   #4
[HLF]Southclaw's Avatar
Join Date: Apr 2009
Location: England
Posts: 4,947
Reputation: 1510
Default Re: Environment Variables

Originally Posted by Verc View Post
What is this used for? Can you explain more?
If you don't know what it is, you probably don't need it (seems like a running theme with my recent releases...)

Anyway, environment variables are basically standard in all languages. An environment variable is a configuration parameter specific to the machine running the code - not a file, not hard coded and not in a database. Those qualities are important for the reasons explained above.

So the prime use-case for this plugin is to provide a way of configuring gamemodes via the environment not via a file or a database - this is useful if you package your gamemode into a VM image or a Docker image - without environment variables, you'd need to either hard-code configuration into the image or mount a configuration file to the image at runtime, both of these are not really ideal options.



[HLF]Southclaw is offline   Reply With Quote
Old 13/07/2018, 10:48 AM   #5
SyS's Avatar
Join Date: Oct 2015
Posts: 1,996
Reputation: 472
Default Re: Environment Variables

SyS is offline   Reply With Quote
Old 13/07/2018, 10:06 PM   #6
Little Clucker
Join Date: Jul 2013
Posts: 10
Reputation: 0
Default Re: Environment Variables

Thank you for another good release
haoss is offline   Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment for scripting?? YoussefHammad Scripting Help 4 17/03/2018 01:22 PM
Your programming environment? Luis- Everything and Nothing 22 28/10/2014 11:32 PM
Show your coding environment Smally Everything and Nothing 50 12/07/2014 10:57 AM
show environment/map of SA at onplayerrequestclass, how? niels44 Scripting Help 7 24/05/2012 05:34 PM
What environment do use use for Web designing SourceCode Everything and Nothing 20 12/01/2012 04:14 PM

All times are GMT. The time now is 11:21 AM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.