Raised This Month: $119 Target: $400
 29% 

Enum Structs Available This Holiday Season


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
BAILOPAN
Join Date: Jan 2004
Old 12-17-2018 , 01:50   Enum Structs Available This Holiday Season
Reply With Quote #1

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);

__________________
egg

Last edited by BAILOPAN; 03-10-2019 at 19:19.
BAILOPAN is offline
Maxximou5
AlliedModders Donor
Join Date: Feb 2013
Old 12-17-2018 , 02:37   Re: Enum Structs Available This Holiday Season
Reply With Quote #2

Like a breath of fresh air. Well done!
__________________
Maxximou5 is offline
Starbish
AlliedModders Donor
Join Date: Oct 2011
Location: South Korea
Old 12-19-2018 , 03:33   Re: Enum Structs Available This Holiday Season
Reply With Quote #3

I really appreciate this!

but looks like latest mac compiler can't compile this expression...?
__________________
Starbish is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 12-19-2018 , 21:06   Re: Enum Structs Available This Holiday Season
Reply With Quote #4

If I ever get around to working on it again, this will greatly simplify some the code to a plugin I was working on to replace the aging UMC plugin.
__________________
Am I back? Well, we'll see.
Powerlord is offline
Timocop
Senior Member
Join Date: Mar 2013
Location: Germany
Old 12-22-2018 , 13:52   Re: Enum Structs Available This Holiday Season
Reply With Quote #5

All this sugar is pretty epic. Already added support to BasicPawn.
Lets hope we dont get diabetes
__________________
Timocop is offline
ofir753
Senior Member
Join Date: Aug 2012
Old 12-22-2018 , 14:00   Re: Enum Structs Available This Holiday Season
Reply With Quote #6

Looks great but why 'enum' is needed its seems like c struct
ofir753 is offline
KliPPy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 12-22-2018 , 14:21   Re: Enum Structs Available This Holiday Season
Reply With Quote #7

Because it's still just syntactic sugar over arrays, just like old enum structs, but with nicer syntax.
__________________

Last edited by KliPPy; 12-22-2018 at 14:22.
KliPPy is offline
Powerlord
AlliedModders Donor
Join Date: Jun 2008
Location: Seduce Me!
Old 12-23-2018 , 00:34   Re: Enum Structs Available This Holiday Season
Reply With Quote #8

Quote:
Originally Posted by KliPPy View Post
Because it's still just syntactic sugar over arrays, just like old enum structs, but with nicer syntax.
Exactly. I don't know about you, but one plugin I was writing literally has a text document to remind me of what the object graph is supposed to look like because they're not easily expressed through old-style enum structs.

Edit: Also text document because I'm too lazy to use a UML diagram maker to do it.
__________________
Am I back? Well, we'll see.

Last edited by Powerlord; 12-23-2018 at 00:39.
Powerlord is offline
LeToucan
New Member
Join Date: Jan 2016
Old 12-24-2018 , 03:19   Re: Enum Structs Available This Holiday Season
Reply With Quote #9

Is there a way we can push enum structs with array fields in them into an ArrayList? For example,

PHP Code:
#include <sourcemod>

enum struct Foo {
    
int x;
    
int y;
    
char strTest[32];
    
int iTest[16];
}

public 
void OnPluginStart() {
    
PrintToChatAll("Start");
    
    
Foo f;
    
strcopy(f.strTestsizeof(f.strTest), "Bar");
    
f.iTest[0] = 123;
    
    
ArrayList arr = new ArrayList();
    
    
PrintToChatAll("f.strTest = \"%s\", f.iTest[0] = %d"f.strTestf.iTest[0]);
    
    
arr.PushArray(fsizeof(f));
    
Foo copy;
    
arr.GetArray(0copysizeof(copy));
    
    
PrintToChatAll("copy.strTest = \"%s\", copy.iTest[0] = %d"copy.strTestcopy.iTest[0]);
    
PrintToChatAll("End");

has the following output

Code:
Start
f.strTest = "Bar", f.iTest[0] = 123
copy.strTest = "", copy.iTest[0] = 0
End
LeToucan is offline
hmmmmm
Senior Member
Join Date: Mar 2017
Location: ...
Old 12-25-2018 , 06:25   Re: Enum Structs Available This Holiday Season
Reply With Quote #10

Seems like a bug to me. You should open an issue on the github page: https://github.com/alliedmodders/sourcepawn/issues
hmmmmm 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 15:39.


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