AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Tagged enums (https://forums.alliedmods.net/showthread.php?t=170639)

Backstabnoob 10-27-2011 09:36

Tagged enums
 
Hey, got some problems with this one:

Code:
#include <amxmodx> #include <ApolloRP> #include <ApolloRP_Skills> new Handle: g_SqlTuple, g_Cache[512] new g_Skill[32][SkillType] public plugin_init() {     g_SqlTuple = ARP_SqlHandle()         register_clcmd("myskill", "myskill")         if (g_SqlTuple == Empty_Handle)         set_fail_state("[ARP] Skills: Failed to retrieve SQL handle.")     } public myskill(id)     for(new SkillType:iVar = Lockpicking; iVar < SkillType; iVar++)         client_cmd(id, "echo %d: %d", iVar, g_Skill[id][iVar]) public plugin_natives() {     register_library("arp_skills")         register_native("ARP_GetUserSkill", "_ARP_GetUserSkill")     register_native("ARP_SetUserSkill", "_ARP_SetUserSkill") } public _ARP_GetUserSkill(Plugin, Params) {     if(Params != 2)         return -1             log_amx("GETTING SKILL: %d", _:get_param(2))         return g_Skill[get_param(1)][SkillType:get_param(2)]             } public _ARP_SetUserSkill(Plugin, Params) {     if(Params != 3)         return -1         log_amx("SETTING SKILL: %d", _:get_param(2))         g_Skill[get_param(1)][SkillType:get_param(2)] = get_param(3)         return 1 } public client_putinserver(id) {     new Authid[36]     get_user_authid(id, Authid, 35)         new Data[1]     Data[0] = id         format(g_Cache, 511, "SELECT * FROM arp_skills WHERE steamid='%s'", Authid)     SQL_ThreadQuery(g_SqlTuple, "SelectHandle", g_Cache, Data, 1) } public SelectHandle(FailState, Handle:hQuery, szError[], iErrorCode, iData[], iDataSize) {     if (FailState == TQUERY_CONNECT_FAILED)         set_fail_state("Could not connect to SQL database!")     else if (FailState == TQUERY_QUERY_FAILED)         set_fail_state("Query failed to execute!")             if(SQL_NumResults(hQuery) == 0)     {         new Authid[36]         get_user_authid(iData[0], Authid, 35)                 format(g_Cache, 511, "INSERT IGNORE INTO arp_skills (steamid) VALUES ('%s')", Authid)         SQL_ThreadQuery(g_SqlTuple, "IgnoreHandle", g_Cache)                 Set(iData[0], 1)                 return     }         for(new SkillType: iVar = Lockpicking; iVar < SkillType; iVar++)         g_Skill[iData[0]][iVar] = SQL_ReadResult(hQuery, _:iVar + 1)         }   public IgnoreHandle(FailState, Handle:hQuery, szError[], iErrorCode, iData[], iDataSize) {         if(iErrorCode)         set_fail_state(szError)         if (FailState == TQUERY_CONNECT_FAILED)         set_fail_state("Could not connect to SQL database!")     else if (FailState == TQUERY_QUERY_FAILED)         set_fail_state("Query failed to execute! Error")         return PLUGIN_CONTINUE }     public client_disconnect(id) {         new Authid[36]     get_user_authid(id, Authid, 35)                 format(g_Cache, 511, "UPDATE arp_skills SET lockpicking=%d, mining=%d, herbalism=%d, enchanting=%d, engineering=%d, cooking=%d, guncrafting=%d WHERE steamid='%s'",     g_Skill[id][Lockpicking], g_Skill[id][Mining], g_Skill[id][Herbalism], g_Skill[id][Enchanting],g_Skill[id][Engineering], g_Skill[id][Cooking], g_Skill[id][Guncrafting], Authid)         SQL_ThreadQuery(g_SqlTuple, "IgnoreHandle", g_Cache)         Set(id, 1)     } Set(Who, With)     for(new SkillType: iVar; iVar < SkillType; iVar++)         g_Skill[Who][iVar] = With

Trying to create a skills API, it all works pretty well but for some reason the enums go wrong.

Code:
#pragma library "arp_skills" native ARP_GetUserSkill(const Client, const SkillType: Skill) native ARP_SetUserSkill(const Client, const SkillType: Skill, EndValue) enum SkillType {     Lockpicking = 0,     Mining,     Herbalism,     Enchanting,     Engineering,     Cooking,     Guncrafting }

With this usage:

ARP_GetUserSkill(id, Lockpicking)
ARP_SetUserSkill(id, Lockpicking, ...)

The "lockpicking" is 2. I don't know why, it should be 0, but then the data saved in database are wrong and things (it saves in Herbalism).

Not only that, the funny thing is that it loads data the same weird way:

Code:
for(new SkillType: iVar = Lockpicking; iVar < SkillType; iVar++)         g_Skill[iData[0]][iVar] = SQL_ReadResult(hQuery, _:iVar + 1)

Not only it reads data the same wrong way (it reads Herbalism [which is third or something] into lockpicking), it also works as it should, but simply the enum is somehow wrongly organised.

I've been trying to find the mistake I made for a long time till now but couldn't think of anything, I have never had these problems with enums before.

Xellath 10-27-2011 10:15

Re: Tagged enums
 
Can't really find anything wrong.

What's "myskill" printing? Have you tried debugging (logging) the values when sending to see if anything bogus is going on?

EDIT: How does the table structure look by the way?

Backstabnoob 10-27-2011 16:32

Re: Tagged enums
 
myskill prints like:

0: 1
1: 1
2: 7 // this should be 0 instead of 2, as 2 is herbalism and 0 is lockpicking
3: 1
...

the table structure is in the same order as the enum is, but the first column is authid


authid
lockpicking
mining
herbalism
...

Yeah I tried debugprinting the values in the native call and it's 2 again, instead of 0. Can't really think of anything wrong there...

Backstabnoob 11-17-2011 14:06

Re: Tagged enums
 
Anyone can see what may be wrong?

Sylwester 11-17-2011 21:00

Re: Tagged enums
 
add:
PHP Code:

new g_SqlColumnName[SkillType][] = {
    
"lockpicking",
    
"mining",
    
"herbalism",
    ....


and then edit SelectHandle:
PHP Code:

    for(new SkillTypeiVar LockpickingiVar SkillTypeiVar++)
        
g_Skill[iData[0]][iVar] = SQL_ReadResult(hQuerySQL_FieldNameToNum(hQueryg_SqlColumnName[iVar])) 



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

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