From 3ffd4cd8352ff8fcf38e804ee2272280390dfe8c Mon Sep 17 00:00:00 2001 From: Ryan Stecker <ryan@stecker.email> Date: Fri, 12 Sep 2014 16:39:21 -0500 Subject: [PATCH] Expose optional default values for the GetEvent* SP functions. --- core/smn_events.cpp | 32 ++++++++++++++++++++++++++++---- plugins/include/events.inc | 12 ++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/core/smn_events.cpp b/core/smn_events.cpp index 2dcf1012..e99bd9e8 100644 --- a/core/smn_events.cpp +++ b/core/smn_events.cpp @@ -207,7 +207,13 @@ static cell_t sm_GetEventBool(IPluginContext *pContext, const cell_t *params) char *key; pContext->LocalToString(params[2], &key); - return pInfo->pEvent->GetBool(key); + bool defValue = false; + if (params[0] > 2) + { + defValue = !!params[3]; + } + + return pInfo->pEvent->GetBool(key, defValue); } static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params) @@ -226,7 +232,13 @@ static cell_t sm_GetEventInt(IPluginContext *pContext, const cell_t *params) char *key; pContext->LocalToString(params[2], &key); - return pInfo->pEvent->GetInt(key); + int defValue = 0; + if (params[0] > 2) + { + defValue = params[3]; + } + + return pInfo->pEvent->GetInt(key, defValue); } static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params) @@ -245,7 +257,13 @@ static cell_t sm_GetEventFloat(IPluginContext *pContext, const cell_t *params) char *key; pContext->LocalToString(params[2], &key); - float value = pInfo->pEvent->GetFloat(key); + float defValue = 0.0f; + if (params[0] > 2) + { + defValue = sp_ctof(params[3]); + } + + float value = pInfo->pEvent->GetFloat(key, defValue); return sp_ftoc(value); } @@ -266,7 +284,13 @@ static cell_t sm_GetEventString(IPluginContext *pContext, const cell_t *params) char *key; pContext->LocalToString(params[2], &key); - pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key), NULL); + char *defValue = ""; + if (params[0] > 4) + { + pContext->LocalToString(params[5], &defValue); + } + + pContext->StringToLocalUTF8(params[3], params[4], pInfo->pEvent->GetString(key, defValue), NULL); return 1; } diff --git a/plugins/include/events.inc b/plugins/include/events.inc index 771d1578..71b723bf 100644 --- a/plugins/include/events.inc +++ b/plugins/include/events.inc @@ -145,10 +145,11 @@ native CancelCreatedEvent(Handle:event); * * @param event Handle to the event. * @param key Name of event key. + * @param defValue Optional default value to use if the key is not found. * @return The boolean value of the specfied event key. * @error Invalid or corrupt Handle. */ -native bool:GetEventBool(Handle:event, const String:key[]); +native bool:GetEventBool(Handle:event, const String:key[], bool:defValue=false); /** * Sets the boolean value of a game event's key. @@ -166,10 +167,11 @@ native SetEventBool(Handle:event, const String:key[], bool:value); * * @param event Handle to the event. * @param key Name of event key. + * @param defValue Optional default value to use if the key is not found. * @return The integer value of the specfied event key. * @error Invalid or corrupt Handle. */ -native GetEventInt(Handle:event, const String:key[]); +native GetEventInt(Handle:event, const String:key[], defValue=0); /** * Sets the integer value of a game event's key. @@ -192,10 +194,11 @@ native SetEventInt(Handle:event, const String:key[], value); * * @param event Handle to the event. * @param key Name of event key. + * @param defValue Optional default value to use if the key is not found. * @return The floating point value of the specfied event key. * @error Invalid or corrupt Handle. */ -native Float:GetEventFloat(Handle:event, const String:key[]); +native Float:GetEventFloat(Handle:event, const String:key[], Float:defValue=0.0); /** * Sets the floating point value of a game event's key. @@ -215,10 +218,11 @@ native SetEventFloat(Handle:event, const String:key[], Float:value); * @param key Name of event key. * @param value Buffer to store the value of the specified event key. * @param maxlength Maximum length of string buffer. + * @param defValue Optional default value to use if the key is not found. * @noreturn * @error Invalid or corrupt Handle. */ -native GetEventString(Handle:event, const String:key[], String:value[], maxlength); +native GetEventString(Handle:event, const String:key[], String:value[], maxlength, const String:defvalue[]=""); /** * Sets the string value of a game event's key.