AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Solved [Optimize] How to... (https://forums.alliedmods.net/showthread.php?t=303762)

neatek 12-20-2017 10:42

[Optimize] How to...
 
Hello everyone,

Question: which of types variables to be best place for storing data for menus?
Possible answers:
KeyValues?
2 x ArrayList?
Global MenuHandle?

Which of this will be faster?
Ex:
HTML Code:

data - "Key1" => "Helloworld" <- 2xArrayList? KeyValues? MenuHandle?

Then we will create menu from data:

Menu:
1. Helloworld


nosoop 12-20-2017 11:28

Re: [Optimize] How to...
 
I'm fond of KeyValues since you can load a configuration file and iterate over the KeyValues handle, adding the menu entries as you go.

Never used a global Menu handle, but if you're able to generate the menu once and reuse the handle just for displaying, that would probably be better.

neatek 12-20-2017 12:22

Re: [Optimize] How to...
 
Quote:

Originally Posted by nosoop (Post 2567274)
Never used a global Menu handle, but if you're able to generate the menu once and reuse the handle just for displaying, that would probably be better.

yep, only one time.

Question2: How to check MySQL connection? Is connected or not connected?

PHP Code:

Handle StableConnection null// Handle? Or Database?

public void DB_CreateConnect() {
    
// here, how to check MySQL Connection?
    // != null  ?! or != INVALID_HANDLE?
    
if(StableConnection != null)  {
        
LogError("Cant connect to database, connection already exists.");
    }

    if(!
SQL_CheckConfig("some_of_databases.cfg")) {
        
LogError("Cant find 'some_of_databases.cfg' in databases.cfg");
    }

    
SQL_TConnect(DB_Callback"some_of_databases.cfg");
}

public 
void DB_Callback(Handle ownerHandle hndl, const char[] errorany data)
{
    if (
hndl == null)
    {
        
LogError("Connection to SQL database has failed! Error: %s"error);
        return;
    }

    
StableConnection hndl;
    
SQL_SetCharset(hndl"utf8");
    
PrintToServer("Successful connect to database.");



xines 12-20-2017 17:10

Re: [Optimize] How to...
 
Quote:

Originally Posted by neatek (Post 2567285)
PHP Code:

Handle StableConnection null// Handle? Or Database?
// here, how to check MySQL Connection?
// != null  ?! or != INVALID_HANDLE? 


Check if != null, meaning connection is not nullified aka connected. or check if == null meaning not connected, and return the error.
Handle? Or Database? -> Database
null or INVALID_HANDLE -> null

Also for connection use this little beauty https://sm.alliedmods.net/new-api/dbi/Database/Connect and for queries https://sm.alliedmods.net/new-api/dbi/Database/Query

Something like this for reference:

PHP Code:

Database StableConnection null

public 
void OnPluginStart()
{
    
Database.Connect(OnDatabaseConnect"my_database_name");
}

public 
void OnDatabaseConnect(Database idb, const char[] errorany data)
{
    if (
idb == null || strlen(error) > 0)
    {
        
PrintToServer("[Mysql] Unable to connect to database (%s)"error);
        
LogError("[Mysql] Unable to connect to database (%s)"error);
        return;
    }
    else
    {
        
//Set to our global variable database named "StableConnection"
        
StableConnection idb;
        
        
//Do a query of your choice upon connection?
        
char sQuery[512];
        
FormatEx(sQuerysizeof(sQuery), "DELETE FROM my_database_name WHERE map = 'de_dust2'"); //Example (non serious) query, change this to whatever you want...
        
        //Send query
        
StableConnection.Query(SQL_ErrorChecksQuery_DBPrio_Normal);

        
//Log Success Print.
        
LogMessage("[Mysq] Successfully connected to database!");
    }
}

//Callback Function to check for errors
public void SQL_ErrorCheck(Handle ownerDBResultSet results, const char[] errorany data)
{
    if (
results == null || strlen(error) > 0)
    {
        
LogError("[Mysql] Query failed! %s"error);
    }




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

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