private variables
#1

I'm trying to make variables private, so they can only be used inside the file where they are declared. The reason for this is because I only want people to access the variable through a function.

Lets say we have example.inc with the following contents:
pawn Код:
new iVariable = 0;

GetVariable()
{
    return iVariable;
}
Then in the main script:
pawn Код:
#include <example>

// This should NOT be possible, since iVariable cannot be accessed directly outside example.inc
iVariable = 5;

// This SHOULD be possible, since we are accessing iVariable though a public function
new iVariableCopy = GetVariable();
iVariableCopy = 5;
I know PAWN can't utilise the "private" keyword, so I went searching. The SA-MP wiki says:

Quote:

You can also have static functions which can only be called from the file in which they are declared. This is useful for private style functions.

Could this also be applied for variables? Static variable in my experience are variables that can only be modified once, not really what I'm after, or is it?

Anyone knows how I would implement this? To give you an idea, a Java example of what I would want would be:
pawn Код:
private int iVariable = 5;

public int getVariable() {
    return this.iVariable;
}
Thank you in regards.
Reply
#2

EDIT: Misunderstood, rethinking
Reply
#3

Quote:
Originally Posted by Rajat_Pawar
Посмотреть сообщение
I doubt it can be - new and delete dynamically allocate memory, right? So there can be only one such at a time - probably instead of new - using C++ style keywords (int, float) can probably help.
Correct me if I am wrong!
I don't think you understand very well what I'm trying to acheive. I think because you saw the peice of Java code you think I'm trying to create some kind of OO-styled script. I'm just trying to make a variable accessible only inside the script where it's declared -- where other scripts need to use the getter to access this variable. Should be perfectly possible although I have no idea how.
Reply
#4

static.
Reply
#5

or even "stock static".
Reply
#6

When you declare a variable with "static" in the global scope, it's limited to the scope of that file.

When you declare them inside a function, they will also be global variables but then limited to the scope it was declared in.

pawn Код:
#include <a_samp>

static something = 5; // just like "new" but limited to this file

SomeFunction() {
    // this is like a global variable, but limited to this function
    static myVar = 5;
   
    myVar += 1;
   
    return myVar;
}

main() {
    printf("%d", SomeFunction()); // prints: 6
    printf("%d", SomeFunction()); // prints: 7
    printf("%d", SomeFunction()); // prints: 8
    printf("%d", SomeFunction()); // prints: 9
}
Reply
#7

Quote:
Originally Posted by Slice
Посмотреть сообщение
...
Thanks!

Quote:
Originally Posted by ******
Посмотреть сообщение
Did you even TRY before asking? Or have you read any existing large code-bases like YSI?
I read the wiki, ******d it and used the search function. If none of those give me a clear answer maybe it's good someone made a topic about it, no? I do get your point though, the answer to my question seems simple enough.
Reply
#8

Quote:
Originally Posted by Sinner
Посмотреть сообщение
Thanks!



I read the wiki, ******d it and used the search function. If none of those give me a clear answer maybe it's good someone made a topic about it, no? I do get your point though, the answer to my question seems simple enough.
It's well-documented on the wiki, actually. Searching for "static" will get you there.

https://sampwiki.blast.hk/wiki/Scripting...s#static_local
Reply
#9

I simply use this:

pawn Код:
#define private static
For my scripts.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)