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

Solved Trying to play with the new enum structs, but....


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
NIGathan
Senior Member
Join Date: Aug 2011
Location: /dev/null
Old 01-29-2019 , 23:37   Trying to play with the new enum structs, but....
Reply With Quote #1

I've nearly pulled all my hair out trying to figure out how to make this thing work...

It almost seems like struct member functions return simply that they exist? I don't even know anymore.. Every time I think I figure it out something else stops it..

This was supposed to be a simple test plugin to see how the new structs worked, but I ended up having to add more and more to it that it doesn't even seem worth it anymore.

Can struct members not be referred to as arrays?

If I define a new struct from the local scope and call a member function that calls another member function, does the inner function lose scope of its own members? Does it only have "this"?

Maybe I just really overestimated what these are capable of, or I severely misunderstand how to use them... Anyway, source is attached, any help or insight is greatly appreciated.
Attached Files
File Type: sp Get Plugin or Get Source (transitional_test.sp - 191 views - 4.9 KB)

Last edited by NIGathan; 02-03-2019 at 11:40.
NIGathan is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 01-30-2019 , 19:04   Re: Trying to play with the new enum structs, but....
Reply With Quote #2

The enum struct is initialized to 0 like all arrays. Calling IsValid() first without setting Edict will result in true being returned, since the "worldspawn" (entity 0) doesn't have a targetname set usually. So I doubt this is a bug with enum structs, but just some brain lag on your side ;) At which order do you execute your test commands? Can you paste the output your plugin produces including what you expect?

You shouldn't need to use view_as on the return type of the member functions. If you still think there is a bug here, could you please shrink your test case down to a minimum?

No, enum struct members don't work like normal arrays (yet).
__________________
Peace-Maker is offline
NIGathan
Senior Member
Join Date: Aug 2011
Location: /dev/null
Old 01-30-2019 , 22:31   Re: Trying to play with the new enum structs, but....
Reply With Quote #3

I don't think that's entirely true. So I declared full before onPluiginStart, and initialized Edict to -1 there, then I get this error in console:
Code:
nigel: !testfull prop_physics_override models/weapons/c_models/c_bread/c_bread_cornbread.mdl
L 01/30/2019 - 19:24:35: [SM] Exception reported: Entity -1 (-1) is not a CBaseEntity
L 01/30/2019 - 19:24:35: [SM] Blaming: transitional_test.smx
L 01/30/2019 - 19:24:35: [SM] Call stack trace:
L 01/30/2019 - 19:24:35: [SM]   [0] AcceptEntityInput
L 01/30/2019 - 19:24:35: [SM]   [1] Line 62, transitional_test.sp::MapReward::Kill
L 01/30/2019 - 19:24:35: [SM]   [2] Line 68, transitional_test.sp::MapReward::Summon
L 01/30/2019 - 19:24:35: [SM]   [3] Line 144, transitional_test.sp::funcFullTest
This is the first and only command I've run since loading the plugin.
Line 62 should not have been reached. Summon() calls Kill(), which calls IsValid(). IsValid() checks that Edict is greater than -1, but Edict is -1 which isn't greater than -1, so it should be returning false and Kill() should not run AcceptEntityInput.

Slight update, adding "== true" to the if (this.IsValid()) has made some changes occur:
Code:
nigel :  !rcon sm plugins reload transitional_test
nigel :  !testfull prop_physics_override models/weapons/c_models/c_bread/c_bread_cornbread.mdl
[SM] Summoned entity #620.
nigel :  !testkill
[SM] full.Edict: 620, full.Name: fullTest
[SM] full.Entity: prop_physics_override, full.Model: models/weapons/c_models/c_bread/c_bread_cornbread.mdl
[SM] full.SpawnOrigin: (-14016.1,-726.6,-7695.9), full.SpawnAngles: (24.3,110.2,0.0)
[SM] full.GetOrigin: (-14016.1,-726.6,-7695.9), full.GetAngles: (24.3,110.2,0.0)
I shot the cornbread between summoning and killing, killing did not kill, and GetOrigin/Angles did not get the new values, so now here is the conflicting parts of the code:
PHP Code:
if (view_as<bool>(full.IsValid()) == false)
    
ReplyToCommand(client,"[SM] Entity is not valid."); 
and
PHP Code:
if (view_as<bool>(this.IsValid()) == true)
{
    
PrintToChatAll("[SM] Killing..."); // Never prints. // Now it prints with view_as, but it doesn't work correctly...
    
AcceptEntityInput(this.Edict,"Kill",0,0,0);

In the first one, IsValid() must be returning true, because it doesn't print, and in the second, it must be returning false, because it doesn't print either..

Last edited by NIGathan; 01-30-2019 at 22:45.
NIGathan is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 01-31-2019 , 10:23   Re: Trying to play with the new enum structs, but....
Reply With Quote #4

You are right, this is a bug in the compiler. The compiler inserts a load of the enum struct base address into pri after each method call. The function's return value is passed in pri too, which gets overwritten.

So your if (this.IsValid()) call is actually checking the address of the enum struct instead of the return value of IsValid in some expressions. I don't really understand enough of the internals to debug it properly, but I've passed it on.

Thanks for the good explanation!
__________________
Peace-Maker is offline
NIGathan
Senior Member
Join Date: Aug 2011
Location: /dev/null
Old 01-31-2019 , 19:43   Re: Trying to play with the new enum structs, but....
Reply With Quote #5

Thanks for your reply and passing this along! This explains why view_as gave me differing results as well.
NIGathan is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 01-31-2019 , 20:34   Re: Trying to play with the new enum structs, but....
Reply With Quote #6

Yeah, it's not the "casting" using view_as<> that's causing your weird behavior, but the comparison afterwards. true is equivalent to 1 and false to 0. So you compared the address of the enum struct variable, e.g. 0x6c3, to 1 and 0 which is both false.

You can track the progress here
https://github.com/alliedmodders/sourcepawn/issues/310
__________________
Peace-Maker is offline
NIGathan
Senior Member
Join Date: Aug 2011
Location: /dev/null
Old 02-01-2019 , 11:13   Re: Trying to play with the new enum structs, but....
Reply With Quote #7

Well shouldn't view_as be making them both true regardless of the address because it's not a null address? Also, would methodmaps provide a temporary fix for this? I'm somewhat confused about them because they can't actually store data, do they simply wrap around a tag?

Update:
Code:
transitional_test.sp(25) : error 082: properties cannot be arrays
I guess I can't solve this with a methodmap wrapping an enum struct...

Last edited by NIGathan; 02-01-2019 at 19:23.
NIGathan is offline
NIGathan
Senior Member
Join Date: Aug 2011
Location: /dev/null
Old 02-03-2019 , 11:39   Re: Trying to play with the new enum structs, but....
Reply With Quote #8

So after a good bit of trial and error I have managed to create a fully working test plugin that utilizes StringMaps to store data to and from an enum struct within a methodmap wrapper. I tried to use ArrayLists first for performance's sake, but the BlockSize seemed to truncate arrays.. This makes a methodmap act more like a real class object, but sacrifices performance.

I've struggled to find many real use examples of how to make a methodmap fully contained so hopefully this will prove useful to some of you here.
Attached Files
File Type: sp Get Plugin or Get Source (transitional_test.sp - 142 views - 8.7 KB)

Last edited by NIGathan; 02-03-2019 at 11:50.
NIGathan is offline
Peace-Maker
SourceMod Plugin Approver
Join Date: Aug 2008
Location: Germany
Old 02-04-2019 , 10:15   Re: Trying to play with the new enum structs, but....
Reply With Quote #9

Quote:
Originally Posted by NIGathan View Post
Well shouldn't view_as be making them both true regardless of the address because it's not a null address?
No, view_as only tells the compiler that you think you know better what tag that blob of data should have. It doesn't change the actual value in any way. Like in C, any value other than 0 is considered true in a boolean way. But comparing the data blob (with the bool tag, but that doesn't matter for the actual comparison) with another constant will compare the actual constant's values instead of considering the tag and thinking "well, a boolean should only be 1 or 0".

PHP Code:
enum NIGathan {
    
NI_false,
    
NI_true
};
public 
void OnPluginStart()
{
    
PrintToServer("%d"view_as<bool>(2) == true); // prints false (0)
    
PrintToServer("%d"view_as<bool>(1) == true); // prints true (1)
    
PrintToServer("%d"NI_true == view_as<NIGathan>(true)); // prints true (1)
    
PrintToServer("%d"false == view_as<bool>(NI_false)); // prints true (1)
    
PrintToServer("%d"true == NI_true); // tag mismatch warning and prints true (1)

You should just wait for methods to be fixed on structs and not waste your time for a weird workaround.
__________________
Peace-Maker is offline
Reply



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 09:05.


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