.:: Bots United ::.

.:: Bots United ::. (http://forums.bots-united.com/index.php)
-   General Bot Coding (http://forums.bots-united.com/forumdisplay.php?f=24)
-   -   Plugin crash... (http://forums.bots-united.com/showthread.php?t=6786)

He@dShot 20-05-2008 00:09

Plugin crash...
 
Why does my plugin crashes when I use either globalvars_t->time, or globalvars_t->maxClients ? ? ?

I made a pointer to globalvars_t in my header file like so:

float think_timer;
globalvars_t *globals;

when I use it in StartFrame() in my metamod plugin like so:

if (timer < globals->time)
{
// do something
}

IT CRASHES: what is wrong ???:angry::angry::angry:

The Storm 20-05-2008 15:13

Re: Plugin crash...
 
From the code you showed it seems that you are just using invalid pointer, it's normal to crash. You must give it proper value in h_export.cpp. Here is code sample from one "stub" metamod plugin:
Code:

//! Holds engine functionality callbacks
enginefuncs_t g_engfuncs;
globalvars_t  *gpGlobals;

// Receive engine function table from engine.
// This appears to be the _first_ DLL routine called by the engine, so we
// do some setup operations here.
void WINAPI GiveFnptrsToDll( enginefuncs_t* pengfuncsFromEngine, globalvars_t *pGlobals )
{
        memcpy(&g_engfuncs, pengfuncsFromEngine, sizeof(enginefuncs_t));
        gpGlobals = pGlobals;
}

Now you can do extern the variable in any header or source file to use it(hint don't declare the pointer in to the header files, just extern it, it must be declared only once in a .cpp file and after that only "externed").

Sinse I see that you don't have much expirience in C++ I will give you little example that is explained well, lets say that you already declared and initalized the gpGlobals pointer in h_export.cpp and after that you want to use it in other source file like for example "my_plugin_source.cpp". You must put this
Code:

extern globalvars_t *gpGlobals;
after the include line or if you have header "my_plugin_source.h" that is included by "my_plugin_source.cpp" better put it there. Anyway I recommend other better practice for such global variables/pointers. Make two files like "globals.cpp" and "globals.h", declare the variable/pointer in to the "globals.cpp" and do the extern in "globals.h", then include "globals.h" in any source file that you want to use the variables/pointers that you declared there, in h_export.cpp leave just the function GiveFnptrsToDll() to give your global pointer proper value.

Here is code examples:

globals.h file
Code:

#include <extdll.h>

#include <h_export.h>

extern enginefuncs_t g_engfuncs;
extern globalvars_t *gpGlobals;

globals.cpp file
Code:

#include "globals.h"


//! Holds engine functionality callbacks
enginefuncs_t g_engfuncs;
globalvars_t *gpGlobals;

h_export.cpp file

Code:

#include "globals.h"

// Receive engine function table from engine.
// This appears to be the _first_ DLL routine called by the engine, so we
// do some setup operations here.
void WINAPI GiveFnptrsToDll( enginefuncs_t* pengfuncsFromEngine, globalvars_t *pGlobals )
{
        memcpy(&g_engfuncs, pengfuncsFromEngine, sizeof(enginefuncs_t));
        gpGlobals = pGlobals;
}



All times are GMT +2. The time now is 16:42.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.