Raised This Month: $ Target: $400
 0% 

Data structure doesn't work properly in native function


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
t3hNox
Senior Member
Join Date: Oct 2009
Old 04-01-2012 , 08:33   Data structure doesn't work properly in native function
Reply With Quote #1

Code:
Code:
... enum _:PowerInfo {     iTime,     szName[32],     szModel[32],     iColor[3] } ... /* Natives */ public plugin_natives() {     register_native("register_powerup","native_register_powerup", 0)     //native register_powerup(iTime, szName[], szModel[], iColor[3]) (from .inc) } public native_register_powerup( iPlugin, iParams ) {     if(iParams != 4)     {         log_amx("Power Up: Registration failed (parameters, item: %d).", g_iTotalPowers+1)         return PLUGIN_HANDLED     }     else if(g_iTotalPowers >= MAX_POWERUPS)     {         log_amx("Power Up: Maximum reached (%d).", MAX_POWERUPS)         return PLUGIN_HANDLED           }         new regInfo[PowerInfo], iTempColor[3], iNum, szTempName[32], szTempModel[32]         iNum = get_param(1)     server_print("Time: %i", iNum)         get_string(2, szTempName, charsmax(szTempName))     get_string(3, szTempModel, charsmax(szTempModel)) //  get_string( 2, regInfo[ szName ], charsmax( regInfo[ szName ] ) ) //  get_string( 3, regInfo[ szModel ], charsmax( regInfo[ szModel ] ) )         if(!file_exists(szTempModel))     {         log_amx("Power Up: Registration failed (no model found: %s).", szTempModel)         return PLUGIN_HANDLED       }     else server_print("model: %s", szTempModel)         get_array( 4, iTempColor, sizeof( iTempColor ) )     regInfo[ iColor ][ 0 ] = clamp( iTempColor[0], 0, 255 )     regInfo[ iColor ][ 1 ] = clamp( iTempColor[1], 0, 255 )     regInfo[ iColor ][ 2 ] = clamp( iTempColor[2], 0, 255 )         copy(regInfo[szModel], charsmax(regInfo[szModel]), szTempModel)     copy(regInfo[szName], charsmax(regInfo[szName]), szTempName)         regInfo[ iTime ] = clamp(iNum, 0, MAX_TIME )         server_print("Power Added PRE ! [Name: %s | Time: %i (orig: %i)]", regInfo[szName], regInfo[iTime], get_param(1))         ArrayPushArray( g_aPowerUps, PowerInfo:regInfo );     g_iTotalPowers++         server_print("Power Added Post ! [Name: %s | Time: %i]", regInfo[szName], regInfo[iTime])         return (g_iTotalPowers - 1) } ...

Code is badly organized and not optimized because of all the debugging..

The problem is with regInfo[iTime] because it gets messed up. It will not containing get_param(1) value but instead some value related to regInfo[szModel].
I have changed the order of retrieving values and everytime I set regInfo[iTime] before setting regInfo[szModel] it will get changed to a (seemingly) random value but it will always be the same. On the other hand, if I set regInfo[iTime] after setting regInfo[szModel] it will affect the first letter of regInfo[szModel] string causing the plugin to fail because of invalid model location.

Everything else works properly. I can retrieve the iTime value later for the dynamic array, but it doestn't contain the right value.

edit: Some debug log.
Current code (which crashes the server because of invalid model)
Code:
L 04/01/2012 - 15:53:12: -------- Mapchange to de_dust2 --------
L 04/01/2012 - 15:53:12: [csdm_spawn_preset.amxx] Loaded 40 spawn points for map de_dust2.
L 04/01/2012 - 15:53:12: [csdm_main.amxx] CSDM spawn mode set to preset
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] Time: 5
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] model: models/power/speed.mdl
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] Power Added PRE ! [Name: Amazing Powerup 1 | Time: 5 (orig: 5)]
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] Power Added Post ! [Name: Amazing Powerup 1 | Time: 5]
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] invalid model: odels/power/speed.mdl <- There's actually a symbol replacing "m", but doesn't show up in log file.
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] Time: 30
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] model: models/power/invisibility.mdl
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] Power Added PRE ! [Name: Amazing Another 2 | Time: 30 (orig: 30)]
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] Power Added Post ! [Name: Amazing Another 2 | Time: 30]
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] invalid model: odels/power/invisibility.mdl
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] Time: 0
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] model: models/power/recoil.mdl
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] Power Added PRE ! [Name: Excellent Powerup 3 | Time: 0 (orig: 0)]
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] Power Added Post ! [Name: Excellent Powerup 3 | Time: 0]
L 04/01/2012 - 15:53:14: [zzPowerUps.amxx] invalid model:
If I move "regInfo[ iTime ] = clamp(iNum, 0, MAX_TIME )" line simply above the two copy functions. Doesn't cause a crash but get regInfo[iTime] value messed up.
Code:
L 04/01/2012 - 15:55:04: -------- Mapchange to de_dust2 --------
L 04/01/2012 - 15:55:05: [csdm_spawn_preset.amxx] Loaded 40 spawn points for map de_dust2.
L 04/01/2012 - 15:55:05: [csdm_main.amxx] CSDM spawn mode set to preset
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] Time: 5
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] model: models/power/speed.mdl
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] Power Added PRE ! [Name: Amazing Powerup 1 | Time: 109 (orig: 5)]
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] Power Added Post ! [Name: Amazing Powerup 1 | Time: 109]
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] model: models/power/speed.mdl
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] Time: 30
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] model: models/power/invisibility.mdl
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] Power Added PRE ! [Name: Amazing Another 2 | Time: 109 (orig: 30)]
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] Power Added Post ! [Name: Amazing Another 2 | Time: 109]
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] model: models/power/invisibility.mdl
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] Time: 0
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] model: models/power/recoil.mdl
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] Power Added PRE ! [Name: Excellent Powerup 3 | Time: 109 (orig: 0)]
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] Power Added Post ! [Name: Excellent Powerup 3 | Time: 109]
L 04/01/2012 - 15:55:06: [zzPowerUps.amxx] model: models/power/recoil.mdl

Last edited by t3hNox; 04-01-2012 at 09:13.
t3hNox is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 04-01-2012 , 12:21   Re: Data structure doesn't work properly in native function
Reply With Quote #2

I see what is happening but I can't explain why based on the code you posted.

Just an FYI, 109 is not a random number, it is the the ascii value code for the letter 'm' which is the very first character of your model string. So this error is the result of data being saved in the wrong location.

It looks like when you have regInfo[ iTime ] = clamp(iNum, 0, MAX_TIME ) below the copy lines it is saving the iTime value in the first position of szModels string instead of iTime. I tried to reproduce the problem but could not. Are you sure that is the exact code causing a problem? Are you leaving anything in or out? For starters I would rename the items in your enum into something not identical to what you would name a variable, like iTime to PI_Time, szName to PI_Name, szModel to PI_Model, iColor to PI_Color.

PHP Code:
#include <amxmodx>
#include <amxmisc>

enum _:PowerInfo
{
    
iTime,
    
szName[32],
    
szModel[32],
    
iColor[3]
}
new 
regInfo[PowerInfo]
const 
MAX_TIME 55;

public 
plugin_init() 
{
    new 
iNum random45 );
    
server_print("Original num=%d" iNum );
    
    new 
szTempModel[] = "models/power/speed.mdl";
    new 
szTempName[] = "Excellent Powerup";
    
    
regInfoiTime ] = clamp(iNum0MAX_TIME )
    
copy(regInfo[szModel], charsmax(regInfo[szModel]), szTempModel)
    
copy(regInfo[szName], charsmax(regInfo[szName]), szTempName)
    
//regInfo[ iTime ] = clamp(iNum, 0, MAX_TIME )
    
    
server_print("[Name: %s | Model: %s | Time: %i]"regInfo[szName], regInfo[szModel], regInfo[iTime])

__________________

Last edited by Bugsy; 04-01-2012 at 12:25.
Bugsy is online now
t3hNox
Senior Member
Join Date: Oct 2009
Old 04-02-2012 , 14:17   Re: Data structure doesn't work properly in native function
Reply With Quote #3

Thank you for your advice.

I changed the enum names to something more unique and also changed the plugin's name. After adding the "new" version of plugin to server everything worked as it was supposed. (I had also done some tweaks in other parts in that function but it probably wasn't the solution.)
Weird problem, though.

Last edited by t3hNox; 04-02-2012 at 14:18.
t3hNox is offline
Reply



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


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