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)

BAILOPAN 12-17-2018 01:50

Enum Structs Available This Holiday Season
 
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);



Maxximou5 12-17-2018 02:37

Re: Enum Structs Available This Holiday Season
 
Like a breath of fresh air. Well done!

Starbish 12-19-2018 03:33

Re: Enum Structs Available This Holiday Season
 
I really appreciate this!

but looks like latest mac compiler can't compile this expression...?

Powerlord 12-19-2018 21:06

Re: Enum Structs Available This Holiday Season
 
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.

Timocop 12-22-2018 13:52

Re: Enum Structs Available This Holiday Season
 
All this sugar is pretty epic. Already added support to BasicPawn.
Lets hope we dont get diabetes :lol:

ofir753 12-22-2018 14:00

Re: Enum Structs Available This Holiday Season
 
Looks great but why 'enum' is needed its seems like c struct

klippy 12-22-2018 14:21

Re: Enum Structs Available This Holiday Season
 
Because it's still just syntactic sugar over arrays, just like old enum structs, but with nicer syntax.

Powerlord 12-23-2018 00:34

Re: Enum Structs Available This Holiday Season
 
Quote:

Originally Posted by KliPPy (Post 2630549)
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.

LeToucan 12-24-2018 03:19

Re: Enum Structs Available This Holiday Season
 
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


hmmmmm 12-25-2018 06:25

Re: Enum Structs Available This Holiday Season
 
Seems like a bug to me. You should open an issue on the github page: https://github.com/alliedmodders/sourcepawn/issues


All times are GMT -4. The time now is 06:45.

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