Raised This Month: $ Target: $400
 0% 

Error in Hook_LevelInit


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Ajaxx
Senior Member
Join Date: Oct 2009
Old 07-04-2012 , 02:15   Error in Hook_LevelInit
Reply With Quote #1

I'm getting the following error when trying to add an event listener in Hook_LevelInit in the Metamod:Source stub solution:

error C2355: 'this' : can only be referenced inside non-static member functions

Here's my function:

Code:
bool Hook_LevelInit(const char *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
{
	gameevents->AddListener(this, "player_death", true);

	return true;
}
Does anyone know what might be causing this?
Ajaxx is offline
Zephyrus
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 07-04-2012 , 02:33   Re: Error in Hook_LevelInit
Reply With Quote #2

Quote:
Originally Posted by Ajaxx View Post
I'm getting the following error when trying to add an event listener in Hook_LevelInit in the Metamod:Source stub solution:

error C2355: 'this' : can only be referenced inside non-static member functions

Here's my function:

Code:
bool Hook_LevelInit(const char *pMapName, char const *pMapEntities, char const *pOldLevel, char const *pLandmarkName, bool loadGame, bool background)
{
	gameevents->AddListener(this, "player_death", true);

	return true;
}
Does anyone know what might be causing this?

Hook_levelinit should either be a member function of the class of your plugin or even better you should add the listener on plugin start
__________________
Taking private C++/PHP/SourcePawn requests, PM me.

Last edited by Zephyrus; 07-04-2012 at 02:36.
Zephyrus is offline
Ajaxx
Senior Member
Join Date: Oct 2009
Old 07-04-2012 , 03:00   Re: Error in Hook_LevelInit
Reply With Quote #3

I was trying to use an example from the sample_mm solution. How would I add the listener on plugin start? Can you provide a little code snippet please? Thanks
Ajaxx is offline
Zephyrus
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 07-04-2012 , 03:21   Re: Error in Hook_LevelInit
Reply With Quote #4

I'm on mobile but just copy that gameevents line to the plugin start function where you get the interfaces
__________________
Taking private C++/PHP/SourcePawn requests, PM me.
Zephyrus is offline
Ajaxx
Senior Member
Join Date: Oct 2009
Old 07-04-2012 , 04:05   Re: Error in Hook_LevelInit
Reply With Quote #5

So I moved the listener to the Load function like this:

Code:
bool StubPlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen, bool late)
{
	PLUGIN_SAVEVARS();

	GET_V_IFACE_CURRENT(GetEngineFactory, engine, IVEngineServer, INTERFACEVERSION_VENGINESERVER);
	GET_V_IFACE_CURRENT(GetEngineFactory, gameevents, IGameEventManager2, INTERFACEVERSION_GAMEEVENTSMANAGER2);

	/* Make sure we build on MM:S 1.4 */
#if defined METAMOD_PLAPI_VERSION
	GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
#else
	GET_V_IFACE_ANY(serverFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
#endif

	SH_ADD_HOOK_MEMFUNC(IServerGameDLL, LevelInit, server, this, &StubPlugin::Hook_LevelInit, true);
	SH_ADD_HOOK_STATICFUNC(IServerGameDLL, ServerActivate, server, Hook_ServerActivate, true);

	gameevents->AddListener(this, "player_death", true);

	return true;
}
and I get the error:

error C2664: 'IGameEventManager2::AddListener' : cannot convert parameter 1 from 'StubPlugin *const ' to 'IGameEventListener2 *'

Do I need to instantiate a IGameEventListener2 and pass it to the listener?
Ajaxx is offline
Zephyrus
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 07-04-2012 , 04:24   Re: Error in Hook_LevelInit
Reply With Quote #6

Nope, your class must extend the IGameEventListener2

class StubPlugin : public ISmmPlugin, public IGameEventListener2
{
}

By heart, but you should get the point
__________________
Taking private C++/PHP/SourcePawn requests, PM me.
Zephyrus is offline
Ajaxx
Senior Member
Join Date: Oct 2009
Old 07-04-2012 , 04:46   Re: Error in Hook_LevelInit
Reply With Quote #7

Thank you very much! That solved my issue with AddListener but now I get error:

error C2259: 'StubPlugin' : cannot instantiate abstract class

here:

Code:
StubPlugin g_StubPlugin;
Ajaxx is offline
Zephyrus
Cool Pig B)
Join Date: Jun 2010
Location: Hungary
Old 07-04-2012 , 05:02   Re: Error in Hook_LevelInit
Reply With Quote #8

Quote:
Originally Posted by Ajaxx View Post
Thank you very much! That solved my issue with AddListener but now I get error:

error C2259: 'StubPlugin' : cannot instantiate abstract class

here:

Code:
StubPlugin g_StubPlugin;
You have to add all functions of the class to yours and make sure you include the header for the gameeventlistener class
__________________
Taking private C++/PHP/SourcePawn requests, PM me.
Zephyrus is offline
Ajaxx
Senior Member
Join Date: Oct 2009
Old 07-04-2012 , 05:12   Re: Error in Hook_LevelInit
Reply With Quote #9

That did the trick. I was missing FireGameEvent. Thanks a million!
Ajaxx 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 16:48.


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