Raised This Month: $12 Target: $400
 3% 

Enum Structs Available This Holiday Season


Post New Thread Reply   
 
Thread Tools Display Modes
asherkin
SourceMod Developer
Join Date: Aug 2009
Location: OnGameFrame()
Old 02-07-2019 , 03:11   Re: Enum Structs Available This Holiday Season
Reply With Quote #21

Quote:
Originally Posted by Powerlord View Post
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
__________________
asherkin is offline
Ilusion9
Veteran Member
Join Date: Jun 2018
Location: Romania
Old 02-11-2019 , 07:15   Re: Enum Structs Available This Holiday Season
Reply With Quote #22

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.
__________________
Ilusion9 is offline
BAILOPAN
Join Date: Jan 2004
Old 02-18-2019 , 16:00   Re: Enum Structs Available This Holiday Season
Reply With Quote #23

Quote:
Originally Posted by gubka View Post
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.
__________________
egg
BAILOPAN is offline
BAILOPAN
Join Date: Jan 2004
Old 02-18-2019 , 16:02   Re: Enum Structs Available This Holiday Season
Reply With Quote #24

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.
__________________
egg
BAILOPAN is offline
SZOKOZ
Member
Join Date: Jan 2014
Old 02-27-2019 , 00:58   Re: Enum Structs Available This Holiday Season
Reply With Quote #25

Quote:
Originally Posted by BAILOPAN View Post
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.
__________________
May still be available for SM scripting. Just look at my Steam profile regarding my availability.
My Steam
SZOKOZ is offline
BAILOPAN
Join Date: Jan 2004
Old 03-10-2019 , 19:19   Re: Enum Structs Available This Holiday Season
Reply With Quote #26

Done, thanks!
__________________
egg
BAILOPAN is offline
kossolax
AlliedModders Donor
Join Date: Jan 2008
Location: Belgium
Old 05-30-2019 , 16:29   Re: Enum Structs Available This Holiday Season
Reply With Quote #27

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

Last edited by kossolax; 05-30-2019 at 16:32.
kossolax is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 05-30-2019 , 20:36   Re: Enum Structs Available This Holiday Season
Reply With Quote #28

Quote:
Originally Posted by kossolax View Post
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.

Last edited by Fyren; 05-30-2019 at 20:39.
Fyren is offline
TheDS1337
Veteran Member
Join Date: Jun 2012
Old 06-27-2019 , 11:04   Re: Enum Structs Available This Holiday Season
Reply With Quote #29

Quote:
Originally Posted by Fyren View Post
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
TheDS1337 is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 06-28-2019 , 00:32   Re: Enum Structs Available This Holiday Season
Reply With Quote #30

Quote:
Originally Posted by TheDS1337 View Post
I just noticed that enum structs works like Arrays
As the very first post says, this feature is merely syntactic sugar.
Fyren is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


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


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