AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Method maps problem (https://forums.alliedmods.net/showthread.php?t=277927)

TheDS1337 01-20-2016 14:32

Method maps problem
 
Why I keep getting the "cannot find method or property Weapon.cost" every time I try to compile this ?
Code:

methodmap Weapon
{
        public Weapon(const char[] name, int cost)
        {
                // Things to do here
        }

        property int cost
        {
                public get()
                {
                        return this.cost;
                }
               
                public set(int value)
                {
                        this.cost = value;
                }
        }
}

And how can I make a string property ? am I doing it correct here ?
Code:

        property char[] name
        {
                public get()
                {
                        return this.name;
                }
               
                public set(const char[] name)
                {
                        this.name = name;
                }
        }

        public int GetName(char[] name, int length)
        {
                return strcopy(name, length, this.name);
        }
       
        public void SetName(char[] name)
        {
                strcopy(this.name, sizeof this.name, name);
        }


Miu 01-20-2016 14:44

Re: Method maps problem
 
Stuff like

PHP Code:

        public get() 
        {
            return 
this.cost;
        } 

is self-referential: this.cost calls get(), which references this.cost, which calls get(), and so on. Methodmaps are not actual classes and so they don't create new memory for a "cost" member variable; rather, you make the accessors reference some global variable or otherwise already existing memory, like

PHP Code:

int g_iCost;

...

        public 
get() 
        {
            return 
g_iCost;
        } 


TheDS1337 01-20-2016 14:49

Re: Method maps problem
 
Oh, I think it's the wrong use of it I thought it works like classes or structs ...

EDIT: Can you give me an example with a string ?

is this correct ?

Code:

char g_weaponsName[][32];

........
        property int index
        {
                public get()
                {
                        return view_as<int> (this);
                }
        }

        property char[] name
        {
                public get()
                {
                        return g_weaponsName[this.index];
                }
               
                public set(const char[] name)
                {
                        strcopy(g_weaponsName[this.index], sizeof g_weaponsName[], name);
                }
        }

        public int GetName(char[] name, int length)
        {
                return strcopy(name, length, this.name);
        }
       
        public void SetName(char[] name)
        {
                strcopy(this.name, sizeof this.name, name);
        }


Miu 01-20-2016 15:24

Re: Method maps problem
 
You can't do

PHP Code:

char g_weaponsName[][32]; 

It needs to have a set size, like

PHP Code:

char g_weaponsName[8][32]; 

Other than that, I have no idea, I don't use methodmaps

TheDS1337 01-20-2016 15:25

Re: Method maps problem
 
I want to it be a dynamic array of strings :/

Miu 01-20-2016 15:32

Re: Method maps problem
 
Then you want ADT arrays: https://sm.alliedmods.net/api/index....oad=file&id=35

TheDS1337 01-20-2016 15:34

Re: Method maps problem
 
Quote:

Originally Posted by Miu (Post 2384954)

Okay, that's great, now I dont have to use the methodmap at all


All times are GMT -4. The time now is 08:50.

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