AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Enum Structs Available This Holiday Season (https://forums.alliedmods.net/showthread.php?t=312822)

Bacardi 12-25-2018 06:30

Re: Enum Structs Available This Holiday Season
 

LeToucan 12-26-2018 15:33

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by Bacardi (Post 2630883)

So since enum structs are treated as an array, when using array fields they are treated as 2 dimensional arrays, and thus wouldn't correctly work with ArrayLists? The section outlining using them with ArrayLists could be updated, as this part makes it seem like my snippet should work.
Quote:

The exception is when interacting with opaque data structures like ArrayList. For example, this is considered valid:

klippy 12-26-2018 16:10

Re: Enum Structs Available This Holiday Season
 
You never gave blocksize to the ArrayList constructor.

Powerlord 12-26-2018 16:20

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by LeToucan (Post 2631124)
So since enum structs are treated as an array, when using array fields they are treated as 2 dimensional arrays, and thus wouldn't correctly work with ArrayLists? The section outlining using them with ArrayLists could be updated, as this part makes it seem like my snippet should work.

While I can't say I know how Pawn internally treats 2-dimensional arrays, in this case you're storing an array reference nside another array.

It helps if you know arrays are a reference type. When I say that, I mean when you do this:

PHP Code:

int players[MAXPLAYERS+1]; 

you're actually creating an array with a length of MAXPLAYERS+1, then storing the memory address of the start of that array in players

LeToucan 12-26-2018 16:23

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by KliPPy (Post 2631130)
You never gave blocksize to the ArrayList constructor.

Thank you, after setting the blocksize to sizeof(f), it worked perfectly.

gubka 01-05-2019 21:52

Re: Enum Structs Available This Holiday Season
 
BAILOPAN, great update, so handy, btw can we wait some update in methodmaps part ? We need some improvements there, something look like a clases with proper constructor and destructor or may be a posibility to create values inside a methodmap class, like in a new struct and use them only there

Timocop 01-06-2019 09:02

Re: Enum Structs Available This Holiday Season
 
methodmaps != classes

While 'enum structs' can hold multiple values because they are arrays, methodmaps can only hold one.

If you want to store something *in* methodmaps use StringMap's (or any other list). StringMap's are more readable. Example:

PHP Code:

methodmap Player StringMap
{
    public 
Player(int id)
    {
        
StringMap map = new StringMap();
        
        
map.SetValue("id"id);
        
map.SetValue("userid"GetClientUserId(id));
        
map.SetValue("money"100);
        
        return 
view_as<Player>(map);
    }
    
    
property int Userid
    
{
        public 
get() {
            
int i;
            
this.GetValue("userid"i);
            return 
i;
        }
    }
    
    
property int Id
    
{
        public 
get() {
            
int i;
            
this.GetValue("id"i);
            return 
i;
        }
    }
    
    
property int Money
    
{
        public 
get() {
            
int i;
            
this.GetValue("money"i);
            return 
i;
        }
        public 
set(int i) {
            
this.SetValue("money"i);
        }
    }
    
    public 
bool IsValid()
    {
        return (
GetClientOfUserId(this.Userid) > 0);
    }
    
    public 
bool InGame()
    {
        return (
IsClientInGame(this.Id));
    }
}

public 
void OnClientPutInServer(int client)
{
    
Player player = new Player(client);
    
    
CreateTimer(30.0SetPlayerMoneyTimerplayer);
}

public 
Action SetPlayerMoneyTimer(Handle timerPlayer player)
{
    if(!
player.IsValid() || !player.InGame())
        return;
    
    
player.Money 2500;
    
    
delete player;


if you remove the sugar it will look like this (decompiled):
PHP Code:

Player:Player.Player(id)
{
    new 
StringMap:map StringMap.StringMap();
    
StringMap.SetValue(map"id"idtrue);
    
StringMap.SetValue(map"userid"GetClientUserId(id), true);
    
StringMap.SetValue(map"money"any:100true);
    return 
map;
}

Player.Userid.get(Player:this)
{
    new 
i;
    
StringMap.GetValue(this"userid"i);
    return 
i;
}

Player.Id.get(Player:this)
{
    new 
i;
    
StringMap.GetValue(this"id"i);
    return 
i;
}

void:Player.Money.set(Player:thisi)
{
    
StringMap.SetValue(this"money"itrue);
    return 
void:0;
}

bool:Player.IsValid(Player:this)
{
    return 
GetClientOfUserId(Player.Userid.get(this)) > 0;
}

bool:Player.InGame(Player:this)
{
    return 
IsClientInGame(Player.Id.get(this));
}

public 
void:OnClientPutInServer(client)
{
    new 
Player:player Player.Player(client);
    
CreateTimer(30.0SetPlayerMoneyTimerplayer0);
    return 
void:0;
}

public 
Action:SetPlayerMoneyTimer(Handle:timerPlayer:player)
{
    new 
var1;
    if (!
Player.IsValid(player) || !Player.InGame(player))
    {
        return 
Action:0;
    }
    
Player.Money.set(player2500);
    
CloseHandle(player);
    
player MissingTAG:0;
    return 
Action:0;



asherkin 01-06-2019 09:38

Re: Enum Structs Available This Holiday Season
 
New-style enum structs having a "methodmap" built-in is meant to solve the problem of methodmaps not having storage - what is lacking?

Santi. 01-09-2019 16:21

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by BAILOPAN (Post 2629530)
Hi Folks,

As of PR #934, SourceMod 1.10 has support for enum structs. This is some long-overdue syntactic sugar that makes SourcePawn feel more like a real language. Enum Structs are internally implemented as arrays, but the syntax is a lot nicer than before and we've fixed a lot of the corner cases and rough edges around using them. You can also have enum struct methods now.

You can read more about enum structs either in the Transitional Syntax Docs, or in the commit message.

If you're too impatient to read that, I'll just copy and paste the sample code from the doc:

PHP Code:

enum struct Rectangle {
  
int x;
  
int y;
  
int width;
  
int height;
 
  
int Area() {
    return 
this.width this.height;
  }
};
 
void DoStuff(const Rectangle r) {
  
PrintToServer("%d, %d, %d, %d"r.xr.yr.widthr.height);



I love you man.. this is what we needed. Thanks bro!

Powerlord 02-07-2019 00:48

Re: Enum Structs Available This Holiday Season
 
I can't help but notice when I try to include an enum struct in a native definition that the compiler tells me that I can't do this:

Code:

error 135: cannot use enum struct type "MapChoices_GetMapData" in natives
Won't this limit their usefulness?

Also, the error is reporting the native name rather than the enum struct name.


All times are GMT -4. The time now is 05:07.

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