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)

asherkin 02-07-2019 03:11

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by Powerlord (Post 2638498)
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.

https://github.com/alliedmodders/sou...ment-445578252

https://github.com/alliedmodders/sourcepawn/issues/295

Ilusion9 02-11-2019 07:15

Re: Enum Structs Available This Holiday Season
 
PHP Code:


#pragma semicolon 1
#pragma newdecls required

#include <sourcemod>

enum struct PlayerInfo
{
    
char steam[65];
    
char name[65];
    
int time;
};

ArrayList g_Players;

public 
void OnPluginStart()
{
    
g_Players = new ArrayList(sizeof(PlayerInfo));
    
HookEvent("player_disconnect"Event_PlayerDisconnect);
    
    
RegConsoleCmd("sm_lastpl"Command_Last);
}

public 
void Event_PlayerDisconnect(Event event, const char[] namebool dontBroadcast
{
    
PlayerInfo info;
    
event.GetString("networkid"info.steamsizeof(PlayerInfo::steam));
    
    if (
StrEqual(info.steam"BOT"))
    {
        return;
    }
    
    
event.GetString("name"info.namesizeof(PlayerInfo::name));
    
info.time GetTime();
    
    
g_Players.PushArray(info);
    if (
g_Players.Length 10)
    {
        
g_Players.Erase(0);
    }
}

public 
Action Command_Last(int clientint args)
{    
    for (
int i 0g_Players.Lengthi++)
    {
        
PlayerInfo info;
        
g_Players.GetArray(iinfo);

        
char time[65];
        
FormatTimeDuration(timesizeof(time), GetTime() - info.time);
        
PrintToConsole(client"[%02d] %s : %s : %s ago"1info.steaminfo.nametime);
    }
    
    return 
Plugin_Handled;
}


int FormatTimeDuration(char[] bufferint maxlenint time)
{
    
int hours time 3600;
    
int minutes = (time 60) % 60;

    if (
hours 0)
    {
        return 
Format(buffermaxlen"%dh %dm"hoursminutes);        
    }
    
    if (
minutes 0)
    {
        return 
Format(buffermaxlen"%dm"minutes);        
    }
    
    return 
Format(buffermaxlen"%ds"time 60);        


Here's an example with structs and arraylists.
I don't know how sizeof of structs works, but if i have 3 members, calling g_Players = new ArrayList(3) it's wrong (only the first paramater - "steam" works fine), so g_Players = new ArrayList(sizeof(PlayerInfo)) it's the correct one.

BAILOPAN 02-18-2019 16:00

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by gubka (Post 2633119)
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

Enum structs are the near-term workaround for lack of instance-variables in methodmaps.

BAILOPAN 02-18-2019 16:02

Re: Enum Structs Available This Holiday Season
 
I've made a very slight change to the enum struct syntax. The final closing brace (}) must now be followed by a newline. Semicolons are not allowed even with #pragma semicolon, to be consistent with most other close-brace scenarios.

SZOKOZ 02-27-2019 00:58

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by BAILOPAN (Post 2640204)
I've made a very slight change to the enum struct syntax. The final closing brace (}) must now be followed by a newline. Semicolons are not allowed even with #pragma semicolon, to be consistent with most other close-brace scenarios.

That explains the error I just encountered with the latest build. The snippet in the original post should be updated to reflect the change.

BAILOPAN 03-10-2019 19:19

Re: Enum Structs Available This Holiday Season
 
Done, thanks!

kossolax 05-30-2019 16:29

Re: Enum Structs Available This Holiday Season
 
Hello,

Why this one is invalid ?
error 008: must be a constant expression; assumed zero


PHP Code:

enum struct Foo {
    
int a;
    
int b;
}

Foo bar[65];

public 
void OnPluginStart() {    
    
Foo i bar[0];
    
i.1;
    
i.2;


this is valid though: bar[0].a


used version 6421

Fyren 05-30-2019 20:36

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by kossolax (Post 2653741)
Why this one is invalid ?

For now, I think you can do Foo i; i = bar[0];. I checked that it compiled, but not that it worked as expected. Assignment here is handled differently than initialization and initialization only works with constants. We'll look into it further.

TheDS1337 06-27-2019 11:04

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by Fyren (Post 2653765)
For now, I think you can do Foo i; i = bar[0];. I checked that it compiled, but not that it worked as expected. Assignment here is handled differently than initialization and initialization only works with constants. We'll look into it further.

I just noticed that enum structs works like Arrays (I pushed a struct to an array using PushArray and retrieved it back and it worked), but it doesn't actually work in the same sense as arrays can be accessed using pointers

Fyren 06-28-2019 00:32

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by TheDS1337 (Post 2656933)
I just noticed that enum structs works like Arrays

As the very first post says, this feature is merely syntactic sugar.


All times are GMT -4. The time now is 19:10.

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