Skip to content

Hook example #2

Gratt-5r2 edited this page Apr 9, 2022 · 3 revisions

← Previous example 'Hello, world!'

Add an instance number to the NPC name

Instance number is an index of the Gothic.dat bytecode which contains the initialization code. This number is used in the InitByScript functions of come classes. In this example, we uses the oCNpc class.

Find the name

In the Names file find the line with the oCNpc::InitByScript function.

image

Create a new function declaration

Create a function by this example.

void __fastcall oCNpc_InitByScript( oCNpc* _this, void*, int instance, int savegame );

Create a hook

auto Hook_oCNpc_InitByScript = CreateHookByName( &oCNpc::InitByScript, &oCNpc_InitByScript, Hook_Auto );

Create a new function definition

The NPCs name is array. In the game used 0 element. To change the name use operators + of zSTRING class.

void __fastcall oCNpc_InitByScript( oCNpc* _this, void* p0, int instance, int savegame ) {
  // TODO
  _this->name[0] = _this->name[0] + " " + zSTRING( instance );
}

Call original method

Before of the name changing we should call the original functions.

Hook_oCNpc_InitByScript( _this, p0, instance, savegame );

Full code of this hook

void __fastcall oCNpc_InitByScript( oCNpc* _this, void*, int instance, int savegame );

auto Hook_oCNpc_InitByScript = CreateHookByName( &oCNpc::InitByScript, &oCNpc_InitByScript, Hook_CallPatch );

void __fastcall oCNpc_InitByScript( oCNpc* _this, void* p0, int instance, int savegame ) {
  Hook_oCNpc_InitByScript( _this, p0, instance, savegame );
  _this->name[0] = _this->name[0] + " " + zSTRING( instance );
}

Check the result

Compile, place .dll in the game, start the game.

image


← Previous page | Next page →

Clone this wiki locally