I have been trying to code a new key system for hl2 and have come across an error which i am assuming is due to variables not being initialized before being used, but not sure why its not working!
Whenever u try to reference CItemKeyBase anywhere else outside item_keys.cpp and item_keys.h, the game crashes.
- Code: Select all
#ifndef ITEM_KEYS_H
#define ITEM_KEYS_H
#pragma once
#include "cbase.h"
#include "items.h"
// Max different coloured keys
#define SF_MAX_KEYS 6
#define SF_KEY_BLACK 131072
#define SF_KEY_BLUE 262144
#define SF_KEY_GREEN 524288
#define SF_KEY_RED 1048576
#define SF_KEY_WHITE 2097152
#define SF_KEY_YELLOW 4194304
class CItemKeyBase : public CItem
{
public:
DECLARE_CLASS( CItemKeyBase, CItem );
DECLARE_DATADESC();
CItemKeyBase::CItemKeyBase()
{
iKeyLists[1]=((int)SF_KEY_BLACK);
iKeyLists[2]=((int)SF_KEY_BLUE);
iKeyLists[3]=((int)SF_KEY_GREEN);
iKeyLists[4]=((int)SF_KEY_RED);
iKeyLists[5]=((int)SF_KEY_WHITE);
iKeyLists[6]=((int)SF_KEY_YELLOW);
};
void Spawn( void );
void Precache( void );
bool MyTouch( CBasePlayer *pPlayer );
bool bGivenKey(CBasePlayer *pPlayer, int iKey);
virtual int GetKey(int key) { return iKeyLists[key]; };
virtual int GetMaxKeys() { return iMaxKeys; };
private:
int iKeyLists[SF_MAX_KEYS];
static int const iMaxKeys = (int)SF_MAX_KEYS;
const char *m_smodel;
};
#endif
Not sure why this is happening but i have also tried using this as an array initializer but that doesn't seem to like it at all:
- Code: Select all
static const int iKeyLists[] = { (int)SF_KEY_BLACK,(int)SF_KEY_BLUE,(int)SF_KEY_GREEN,(int)SF_KEY_RED,(int)SF_KEY_WHITE,(int)SF_KEY_YELLOW };
Any help would be greatly appreciated



