AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Static property in methodmap (https://forums.alliedmods.net/showthread.php?t=276328)

Godis 12-19-2015 10:40

Static property in methodmap
 
Hello. I've been using static methods for my natives instead of normal functions, sort of like namespaces in other languages. I do know this is not how methodmaps are supposed to be used but it makes it much easier to distinguish what belongs to where, for example;

FooBar.Get(); instead of GetCurrentFooBar();

So my question: Is this possible?

PHP Code:

FooBar.Value 10;
PrintToChatAll("%i"FooBar.Value); 

I've tried but i can't seem to make my property static, and i really don't want to do this:

PHP Code:

FooBar foo;
foo.Value 10

I'm not a native english speaker so forgive me if i've used incorrect terms.

Mathias. 12-20-2015 16:06

Re: Static property in methodmap
 
if it is "static", don't create the setter and return the value wanted on the get.

get() = { return 10; }

EDIT:

Or if u were looking the other way arround.

int myVar = 0;

FooBar.set(int value) = { myVar = value; }
FooBar.get() = { return myVar; }

no matter how many foobar you use they will refer to the same variable value.

Godis 12-21-2015 09:19

Re: Static property in methodmap
 
Okay so what i want is not FooBar.Get()/FooBar.Set(int value), i want to have multiple values i can retrieve/set with FooBar.Value = 1/FooBar.Value, just like ConVar.IntValue works. But after trying many different things it seems like it can't be done currently so i'll have to go with FooBar.Get/FooBar.Set for the time being.

Mathias. 12-21-2015 16:20

Re: Static property in methodmap
 
I'm confuse of what your trying to do. I don't see the "static" mimic at all in what your trying to do.

posibility that I understand:
#1
if you think you have to call FooBar.Get() or FooBar.Set() is because you do not understand methodmaps properties.

You have to use FooBar.Value = 1 and FooBar.Value it just you have to declare inside your methodmap what will happen getter/setter.

PHP Code:

int g_iGlobalMimicStaticHolder;

methodmap FooBar
{
    
property int Value 
    
{
        public 
set(int value) { g_iGlobalMimicStaticHolder value };
        public 
get() { return g_iGlobalMimicStaticHolder; }
    }
}

Some where in code:
FooBar foo;
foo.Value 2;

FooBar myNewFoo;
if (
foo.Value == myNewFoo.Value)
    
PrintToServer("it work!"); 

#2
If you want to save value per instance of object then you have to store it some where. Example of unlimited storage:
PHP Code:

ArrayList g_MyValue;

public 
APLRes AskPluginLoad2(Handle myselfbool latechar[] errorint error_length)
{
    
g_MyValue = new ArrayList(1);
    
    
/* if use natives put it there */
    
return APLRes_Success;
}

methodmap FooBar
{
    public 
FooBar()
    {
        return 
view_as<FooBar>g_MyValue.Push(0/* default variable value here */); 
    }
    
    
property int Index
    
{
        public 
get() { return view_as<int>this; } // array index
    
}

    
property int Value 
    
{
        public 
set(int value) { g_MyValue.Set(view_as<int>thisvalue); }
        public 
get() { return g_MyValue.Get(view_as<int>this); }
    }


You can also declare your methodmaps function as native if you want to share w.e. memory with other plugins
property int Value
{
public native set();
...
inside AskPluginLoad2
CreateNative("FooBar.Value.set", Native_SetFooBarValue);
...

well that pretty much it

Edit: none of the above has been really tested but the logic should be right and the examples prob work just don't be suprise if I mistype a var or w.e.


All times are GMT -4. The time now is 14:53.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.