Raised This Month: $ Target: $400
 0% 

Optimize code, menu and nvault


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
hornet
AMX Mod X Plugin Approver
Join Date: Mar 2010
Location: Australia
Old 06-10-2012 , 00:38   Re: Optimize code, menu and nvault
Reply With Quote #7

There's quite a number of issues here.

First of all, you can't level up, because you start at level 0 and level * 10.0 = 0.0 .

( Just note that I've only added and edited parts to make your plugin work, not to make it more efficient )

So change your Ham_TakeDamage hook to:

Code:
public fwd_TakeDamage_Pre(victim, inflictor, attacker, Float:damage, damage_bits) 
{ 
	if(!is_user_connected(attacker)) 
		return HAM_IGNORED 
	
	new weapon = get_user_weapon(attacker) 
	if (weapon == CSW_HEGRENADE) 
		return HAM_IGNORED 
	
	new level = g_iLevels[attacker][weapon] 
	SetHamParamFloat(4, ( level + 1 ) * 10.0) 
	return HAM_IGNORED; 
}
This will display the message properly when you select a menu item:

Code:
public menu_handler(id, menu, item) 
{ 
	if( item == MENU_EXIT ) 
	{ 
		menu_destroy(menu); 
		return PLUGIN_HANDLED; 
	} 
	
	client_print(id, print_chat, "Weapon %s - Level %d ", Weapons[item][_Nome] , g_iLevels[ id ][ Weapons[ item ][ _Valor_CSW ] ] ) 
	menu_destroy(menu) 
	
	return PLUGIN_HANDLED; 
}
The reason you can't save or load properly is because your level variable isn't saving with the same alignment to your data structure. You can't save them in order of weapon constant, because your data structure isn't in weapon constant order.

So instead you can save and load according to the structure:
Code:
public Load(id) {     new szData[128];         nvault_get( iVault , g_iSave[id], szData , charsmax( szData ) )     new szPiece[ 32 ], i;         /*I wont lie ;) Exolent taught me the use of strtok() */         while( contain( szData, " " ) >= 0 )     {               strtok( szData, szPiece, charsmax( szPiece ), szData, charsmax( szData ), ' ' )                         g_iLevels[ id ][ Weapons[ i ][ _Valor_CSW ] ] = str_to_num( szPiece );                 if( szPiece[ 0 ] )             i ++;     }         if( !i )     {         Save( id );         Load( id );     } } public Save(id) {     new szData[128],temp[40]         for(new i; i < sizeof Weapons; i++)     {         formatex(temp, charsmax( temp ), " %d",  g_iLevels[ id ][ Weapons[ i ][ _Valor_CSW ] ] )         add(szData, charsmax( szData ), temp)     }         nvault_set( iVault , g_iSave[id] , szData ); }

And change client_putinserver() to this:
Code:
public client_connect(id) {     get_user_authid(id, g_iSave[id], charsmax( g_iSave[] ))     Load(id) }

See how it goes.

Also, I'd recommend that you don't use is_user_connected() to ensure that the attacker is indeed a player in your Ham_TakeDamage and Ham_Killed hooks.
__________________
Quote:
vBulletin Tip #42: Not much would be accomplished by merging this item with itself.

Last edited by hornet; 06-10-2012 at 00:55.
hornet is offline
 



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 00:21.


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