AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Coding MM:S Plugins & SM Extensions (https://forums.alliedmods.net/forumdisplay.php?f=75)
-   -   Copying ConVars (https://forums.alliedmods.net/showthread.php?t=140953)

c0ldfyr3 10-17-2010 22:35

Copying ConVars
 
Hi,

At the moment my ZombieMod plugin uses long prefixes for it's cvars [zombie_] and I'd like to offer an alternative while still keeping the old ones.

What would be the best way to go about it while keeping the cvars in synch with one another, will I really have to check each one in a callback and change the value of the other once I do manage to replicate them all?

Thanks in advance,
c0ld

API 10-18-2010 01:22

Re: Copying ConVars
 
You can create all the ConVar's using the same callback, then replace "zombie_" with "", format "zm_" to prefix, and SetConVar.

c0ldfyr3 10-18-2010 18:28

Re: Copying ConVars
 
Quote:

Originally Posted by pimpinjuice (Post 1328174)
You can create all the ConVar's using the same callback, then replace "zombie_" with "", format "zm_" to prefix, and SetConVar.

See I want them to list the same details too, is there copy convar type functionality anywhere already??

API 10-18-2010 22:20

Re: Copying ConVars
 
Hmm, details? What I had in mind was this type of setup. Please excuse my psuedo-C

CreateConsoleVariable("zombie_a", "description for a", "1", CallbackGlobal);
CreateConsoleVariable("zm_a", "description for a", "1", CallbackGlobal);
CreateConsoleVariable("zombie_b", "description for b", "2.5", CallbackGlobal);
CreateConsoleVariable("zm_b", "description for b", "2.5", CallbackGlobal);

// global callback
void CallbackGlobal(const char *ConvarName, const char *OldValue, const char *NewValue)
{
// check if ConvarName is prefixed with either zombie_ or zm_, substr() stuff
// if it is prefixed, replace with the counter prefix and bam...
SetConsoleVariable(CounterPrefixedName, NewValue);
}

c0ldfyr3 10-19-2010 05:46

Re: Copying ConVars
 
Hey Pimp,

The double declarations is what I'm trying to avoid, I wanted to loop my convar container and create copies which I could alter so even as I add new ones I won't need duplicate declarations.

The Callback I was expecting no matter what as no matter how I duplicate them be it manully with duplicate vars or i find a way to programmaticaly do it they will still be two distinct cvars.

Thanks for your input so far pimpin,
c0ld

API 10-19-2010 16:06

Re: Copying ConVars
 
Hmm, I see what you are saying. Maybe you can hook the input of the server console?
PSUEDO
Code:

Hook ServerConsoleInput
OnInput Check for "zombie_"
If found, search within your defined containers, and take action
OnInput Also Check for "zm_"
If found, search containers, set REAL cvar, and block "command not found"


c0ldfyr3 10-21-2010 10:01

Re: Copying ConVars
 
I have most of it figured out... The only remaining part I can't seem to get down is how to get a list of my plugins cvars before ConVar_Register is called. They only appear in the list after that call and by then it'll be too late to hitch a free ride on the cvar automation :)

c0ldfyr3 10-21-2010 19:22

Re: Copying ConVars
 
This code works and doesn't knock out anything else but none of the new cvars show up in 'meta cvars' nor do they act like cvars, I've tried changing paernt to the convar itself with the same result.

Any ideaS?

Code:

        #if defined ORANGEBOX_BUILD
                /* NOTE! g_pCvar must be set to a valid ICvar instance first. */
                g_pCVar = m_CVar;
                ConVar_Register(0, &g_Accessor);
        #else
                ConCommandBaseMgr::OneTimeInit(&g_Accessor);
        #endif

        ConCommandBase *pStart= m_CVar->GetCommands();
        while (pStart)
        {
                if ( !pStart->IsCommand() )
                {
                        ConVar *pVar = (ConVar *)pStart;
                        char *sPrefix = "";
                        const char *sName = pVar->GetName();
                        if ( Q_strncmp( sName, "zombie_", 7 ) == 0 )
                        {
                                char sNewVar[100] = "";
                                char sVarName[100] = "";
                                Q_strncpy( sNewVar, "zm_", 100 );
                                Q_StrRight( sName, Q_strlen( sName ) - 7, sVarName, 100 );
                                Q_strncat( sNewVar, sVarName, 100, Q_strlen( sVarName ) );
                                META_LOG( g_PLAPI, "Convar - %s", sNewVar );
                                float fMax = 0;
                                float fMin = 0;
                                pVar->GetMin(fMin);
                                pVar->GetMax(fMax);

                               
                                ConVar *pNewVar = new ConVar(sNewVar, pVar->GetDefault(), pVar->m_nFlags);
                               
                                pNewVar->m_pParent = pVar->m_pParent;
                                pNewVar->m_pszHelpString = pVar->GetHelpText();
                                pNewVar->m_bHasMin = pVar->m_bHasMin;
                                pNewVar->m_fMinVal = fMin;
                                pNewVar->m_bHasMax = pVar->m_bHasMax;
                                pNewVar->m_fMaxVal = fMax;
                                pNewVar->InstallChangeCallback ( CVar_CallBack );

                                m_CVar->RegisterConCommand( (ConCommand *)pNewVar );
                                m_ConVars.AddToHead(pNewVar);
                        }

                }
                pStart = const_cast<ConCommandBase *>(pStart->GetNext());
        }

        ConVar_Register(0, &g_Accessor);


c0ldfyr3 10-21-2010 23:52

Re: Copying ConVars
 
I've sort of solved it...

Code:

#define CreateConvar( sName, sDefault, lFlags, sHelp, bMin, fMin, bMax, fMax, oCallback ) \
        ConVar zm_##sName("zm_##sName", sDefault, lFlags, sHelp, bMin, fMin, bMax, fMax,oCallback ); \
        ConVar zombie_##sName("zombie_##Name", sDefault, lFlags, sHelp, bMin, fMin, bMax, fMax, oCallback );



All times are GMT -4. The time now is 13:57.

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