AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Enum struct, how do we access the struct property as parameter? (https://forums.alliedmods.net/showthread.php?t=340663)

JoshGomez 12-02-2022 07:25

Enum struct, how do we access the struct property as parameter?
 
How do we access the struct property as parameter (FPlayerChances:CameraShakeChance)?

PHP Code:

bool:Game_CanUseAbility(client,FPlayerChances:index)
{
    new 
Float:fChance GetRandomFloat(0.01.0);
    if(
FPlayerChancesclient ][ index ] >= fChance//issue here
    
{
        return 
true;
    }
    
    return 
false;
}

bool:Game_ShakeVictimOnAttack(victim,attacker)
{
    new 
Float:fShakeAmplitude FPlayerPropertiesattacker ].CameraShake;
    if(
fShakeAmplitude 0.0 && Game_CanUseAbility(attacker,FPlayerChances:CameraShakeChance)) //issue
    
{
        if(!
Game_IsAbilityCoolingDown(attacker,EPlayerCooldowns:CameraShakeCooldown,ABILITY_COOLDOWN)) //issue
        
{
            
Effect_Shake(victim,fShakeAmplitude,ABILITY_COOLDOWN);
            return 
true;
        }
    }
    
    return 
false;



Cruze 12-04-2022 07:34

Re: Enum struct, how do we access the struct property as parameter?
 
Please show your enum struct

azalty 12-04-2022 16:55

Re: Enum struct, how do we access the struct property as parameter?
 
index.CameraShakeChance or FPlayerChances[client][index].CameraShakeChance ?

I'm pretty confused about your code. I don't really get it, you pass an enum struct as a parameter called index (weird name) yet you access it with FPlayerChances[client][index] like wtf.
And old syntax too :'(

JoshGomez 12-06-2022 07:25

Re: Enum struct, how do we access the struct property as parameter?
 
Quote:

Originally Posted by Cruze (Post 2794372)
Please show your enum struct

PHP Code:

enum struct EPlayerChances
{
    
float LifeStealChance;
    
float ArmorStealChance;
    
float FlashProtectionChance;
    
float DamageChance;
    
float CameraShakeChance;
    
float FreezeChance;
}

EPlayerChances FPlayerChances[MAXPLAYERS 1]; 


JoshGomez 12-06-2022 07:30

Re: Enum struct, how do we access the struct property as parameter?
 
Quote:

Originally Posted by azalty (Post 2794412)
index.CameraShakeChance or FPlayerChances[client][index].CameraShakeChance ?

I'm pretty confused about your code. I don't really get it, you pass an enum struct as a parameter called index (weird name) yet you access it with FPlayerChances[client][index] like wtf.
And old syntax too :'(

No, that not what I want I had an old script that was working before which I want to refactor, but I got this warning which I forgot the description of, something like index based enum was removed.
The code in the example is not the initial code as it was work in progress of the refactoring so it may be lot of confusion.

I want to access the property of the enum by flag or string using "reflection", is that possible? Otherwise, I was thinking of refactor the data structure to StringMap.

Bacardi 12-06-2022 13:57

Re: Enum struct, how do we access the struct property as parameter?
 
enum struct just "sugar coating" code.

If you want get items work as array, using index, one way is this.
PHP Code:

enum
{
    
LifeStealChance 0,
    
ArmorStealChance,
    
FlashProtectionChance,
    
DamageChance,
    
CameraShakeChance,
    
FreezeChance,
    
ChanceTOTAL
};

enum struct EPlayerChances
{
    
float Chance[ChanceTOTAL];
}
EPlayerChances FPlayerChances[MAXPLAYERS+1];

// You use it like this:
// FPlayerChances[attacker].Chance[CameraShakeChance]; 


Second way.
You can create function, inside enum struct as well
Code:

enum
{
        LifeStealChance = 0,
        ArmorStealChance,
        FlashProtectionChance,
        DamageChance,
        CameraShakeChance,
        FreezeChance,
        MaxChances
}

enum struct EPlayerChances

{
        float LifeStealChance;
        float ArmorStealChance;
        float FlashProtectionChance;
        float DamageChance;
        float CameraShakeChance;
        float FreezeChance;

        bool CanUseAbility(int index)
        {
                // out of bounds
                if(index < LifeStealChance || index >= MaxChances)
                        return false;
               
                float a;
                float b = GetRandomFloat(0.0, 1.0);
               
                switch(index)
                {
                        case LifeStealChance:
                        {
                                a = this.LifeStealChance;
                        }
                        case ArmorStealChance:
                        {
                                a = this.ArmorStealChance;
                        }
                        case FlashProtectionChance:
                        {
                                a = this.FlashProtectionChance;
                        }
                        case DamageChance:
                        {
                                a = this.DamageChance;
                        }
                        case CameraShakeChance:
                        {
                                a = this.CameraShakeChance;
                        }
                        case FreezeChance:
                        {
                                a = this.FreezeChance;
                        }
                }
               
                if(a >= b)
                        return true;
               
                return false;
        }
}

EPlayerChances PlayerChances[MAXPLAYERS+1];

enum struct EPlayerProperties
{
        float CameraShake;
}
EPlayerProperties PlayerProperties[MAXPLAYERS+1];

bool Game_ShakeVictimOnAttack(int victim, int attacker)
{
        float ShakeAmplitude = PlayerProperties[attacker].CameraShake;

        if(ShakeAmplitude > 0.0 && PlayerChances[attacker].CanUseAbility(CameraShakeChance))
        {
                // Gonna be too much figure these out
                //Game_IsAbilityCoolingDown()

                return true;
        }

        return false;
}


Anyone, please mention, if this code sample is wrong. I just throw this from my head.

LinLinLin 12-06-2022 16:53

Re: Enum struct, how do we access the struct property as parameter?
 
why we usually use MAXPLAYERS+1 not MAXPLAYERS?
MAXPLAYERS is 65 actually, if we need to traversal an array of player from 0-64, it is ok.
is it not always equal to 65 in the other source game?

Bacardi 12-06-2022 18:17

Re: Enum struct, how do we access the struct property as parameter?
 
PHP Code:

#define MAXPLAYERS      65  /**< Maximum number of players SourceMod supports */ 

Different games has different max player count.
There can be 65 players in some games.


So if you create array
Code:

array[65]
...you have array, valid array indexs from 0 to 64 (total 65 index)


Usually we use client indexs as array index. You have to remember, client index start from 1 (0 is console)
Now you use above array, starting index 1 to 64 (total 64 index)

In code we loop using dynamic variable MaxClients, this can return result 65

In code loop, to catch finale client index 65
PHP Code:

for(int i 1<= MaxClientsi++) 

...plugin would throw error, Array index out-of-bounds.

This is reason why should add +1 while create array, because array index has offset +1 when using client indexs.






Also
SourceTV adds +1 extra slot in some games (ex. CS:S).

Code:

Unable to load plugin "addons/metamod/bin/win64/server"
maxplayers set to 65
Unknown command "r_decal_cullsize"
Network: IP 192.168.1.2, mode MP, dedicated Yes, ports 27015 SV / 27005 CL
Initializing Steam libraries for secure Internet server
No account token specified; logging into anonymous game server account.  (Use sv_setsteamaccount to login to a persistent account.)
L 12/07/2022 - 00:32:08: [GEOIP] GeoIP2 database loaded: GeoLite2-City (2022-11-21 15:35:08 UTC) (G:\server\counter-strike source\cstrike\addons\sourcemod\configs\geoip\GeoLite2-City.mmdb)
L 12/07/2022 - 00:32:08: [GEOIP] GeoIP2 supported languages: de en es fr ja pt-BR ru zh-CN
Executing dedicated server config file server.cfg
Using map cycle file 'cfg/mapcycle_default.txt'.  ('cfg\mapcycle.txt' was not found.)
Set motd from file 'cfg/motd_default.txt'.  ('cfg/motd.txt' was not found.)
Set motd_text from file 'cfg/motd_text_default.txt'.  ('cfg/motd_text.txt' was not found.)
SV_ActivateServer: setting tickrate to 66.7
Unknown command "sm_bombsite_swap"
Unknown command "sm_bombsiteA_pos"
Unknown command "sm_bombsiteB_pos"
Connection to Steam servers successful.
  Public IP is x.
Assigned anonymous gameserver Steam ID [A:1:270043136:22053].
VAC secure mode is activated.
status
hostname: Do not come here.
version : 6630498/24 6630498 secure
udp/ip  : 192.168.1.2:27015  (public ip: x)
steamid : [A:1:270043136:22053] (90166709731231744)
map    : de_dust2 at: 0 x, 0 y, 0 z
tags    : increased_maxplayers
players : 0 humans, 0 bots (65 max)
edicts  : 261 used of 2048 max
# userid name                uniqueid            connected ping loss state  adr
tv_enable
"tv_enable" = "0"
 notify
 - Activates SourceTV on server.
tv_enable 1
maxplayers set to 66 (extra slot was added for SourceTV)
status
hostname: Do not come here.
version : 6630498/24 6630498 secure
udp/ip  : 192.168.1.2:27015  (public ip: x)
steamid : [A:1:270043136:22053] (90166709731231744)
map    : de_dust2 at: 0 x, 0 y, 0 z
tags    : increased_maxplayers
players : 0 humans, 0 bots (66 max)
edicts  : 261 used of 2048 max
# userid name                uniqueid            connected ping loss state  adr

But this would give glitches, errors and crashes to server.

JoshGomez 12-08-2022 11:05

Re: Enum struct, how do we access the struct property as parameter?
 
Quote:

Originally Posted by Bacardi (Post 2794602)
enum struct just "sugar coating" code.

....

Anyone, please mention, if this code sample is wrong. I just throw this from my head.

Thanks I will try your first solution that seems to be nice as I don't want a big switch case.

I just wonder why the enum does not have a name?
PHP Code:

enum PlayerChanceType
{
    
LifeStealChance 0,
    
ArmorStealChance,
    
FlashProtectionChance,
    
DamageChance,
    
CameraShakeChance,
    
FreezeChance,
    
    
ChanceTotal


Can I do this?
PHP Code:

enum struct PlayerCharacter
{
    
float Chance[view_as<PlayerChanceType>(ChanceTotal)];
}

PlayerCharacter PlayerCharacters[MAXPLAYERS 1]; 

Is this correct? Another thing why is it not possible to have another enum with the same name?

I wish the ChanceTotal to just be Total but I got another enum with different properties, but Total is already used it says.

LinLinLin 12-08-2022 15:20

Re: Enum struct, how do we access the struct property as parameter?
 
ok, i haven't seen the situation that MaxClients can over the MaxPlayers, i just make plugin for L4D2 and MAXPLAYERS is big enough for this game(65 - 31).


All times are GMT -4. The time now is 22:46.

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