In this tutorial, you'll learn how to make your own items appear in the world, and in the editor. The item you will be creating is a megahealth (because it is almost a straight copy of a normal health box). This tutorial was written by Steb. (Its my first tutorial, so bear with me...)

ok, here we go...remember to give or take a few when it comes to line numbers...

Goto game.c, line 55. Add the following line :

GenVSI_SetClassSpawn(VSI, "ItemHealth", Item_HealthSpawn);
GenVSI_SetClassSpawn(VSI, "ItemMHealth", Item_MHealthSpawn);
GenVSI_SetClassSpawn(VSI, "ItemRocket", Item_RocketSpawn);

This tells the engine whenever it comes across an 'ItemMHealth', to goto the function Item_MHealthSpawn (defined later)

Goto game.c, line 125. Add the following line :

GenVSI_ProcIndex(VSI, 8, Shredder_Control);
GenVSI_ProcIndex(VSI, 9, Item_ControlHealth);

Goto game.h, line 114. Add the following lines

geBoolean Item_ShredderAmmoSpawn(GenVSI *VSI, void *PlayerData, void *ClassData);

geBoolean Item_MHealthSpawn(GenVSI *VSI, void *PlayerData, void *ClassData);
geBoolean Item_ControlMHealth(GenVSI *VSI, void *PlayerData, float Time);

All these 2 lines of code do is prototype the functions so the compiler will not throw up a warning. we'll define in the file MHealth.c that we'll make later on...

Goto game.h, line 64. Add the following line :

#define SOUND_INDEX_GRENADE 19
#define SOUND_INDEX_PICKUP_MHEALTH 20

This sets a 'placeholder' for the pickup sound.

Goto game.c, line 105. Add the following line :

GenVSI_SoundIndex(VSI, SOUND_INDEX_PICKUP_HEALTH, "Wav\\PickupHealth.Wav");
GenVSI_SoundIndex(VSI, SOUND_INDEX_PICKUP_MHEALTH, "Wav\\MHealth.Wav");

This line adds the actual filename to the 'placeholder'. You'll need to download this sound from the bottom of the page.

create a new file MHealth.c (the name doesn't really matter, call it what you want) and add it to the project. You can download this file at the bottom of the page, or you can copy and paste it from here. I'll be explaining things as we go along from here.

#include "Game.h" // lets this file interface with the rest of the game
 
#define MHEALTH_RESPAWN 60.0f // Number of seconds before item respawns
 
//=====================================================================================
// Item_TriggerMHealth
//=====================================================================================
// when the item is touched, this function is called.
 
static geBoolean Item_TriggerMHealth(GenVSI *VSI, void *PlayerData, void *TargetData)
{
GPlayer *Player, *Target;
Player = (GPlayer*)PlayerData;
Target = (GPlayer*)TargetData;
 
if (GenVSI_GetTime(VSI) < Player->NextThinkTime)
return GE_TRUE;
 
if (Target->ClientHandle == CLIENT_NULL_HANDLE)
return GE_TRUE;
 
//if (Target->Health == 100)
//return GE_TRUE; // Doesn't need health, leave it for someone else
//Commented the above 2 lines because the Mega Health adds 100 health regardless of current health status.
 
Player->ViewIndex = 0xffff;
Player->NextThinkTime = GenVSI_GetTime(VSI) + MHEALTH_RESPAWN;
 
// Increase the health of the player that touches this health
Target->Health += 100;
 
GenVSI_PlaySound(VSI, SOUND_INDEX_PICKUP_MHEALTH, &Player->XForm.Translation);
GenVSI_SetClientHealth(VSI, Target->ClientHandle, Target->Health);
 
return GE_TRUE;
}
 
//=====================================================================================
// Item_ControlMHealth
//=====================================================================================
// This function gets called when respawning (and maybe other things...anyone know when exactly?)
 
geBoolean Item_ControlMHealth(GenVSI *VSI, void *PlayerData, float Time)
{
geVec3d Pos;
GPlayer *Player;
Player = (GPlayer*)PlayerData;
 
if (GenVSI_GetTime(VSI) < Player->NextThinkTime)
return GE_TRUE; // Not time to come back yet
 
if (Player->ViewIndex == 0xffff) // Comming back into view
{
Player->Roll = 0.0f;
GenVSI_SpawnFx(VSI, FX_EXPLODE2, &Player->XForm.Translation, SOUND_INDEX_ITEM_SPAWN);
Player->ViewIndex = MESH_INDEX_HEALTH;
}
Pos = Player->XForm.Translation;
 
Player->XForm.Translation.X = 0.0f;
Player->XForm.Translation.Y = 0.0f;
Player->XForm.Translation.Z = 0.0f;
 
geXForm3d_RotateY(&Player->XForm, Time*2.0f);
 
Player->XForm.Translation = Pos;
 
return GE_TRUE;
}
 
//=====================================================================================
// Item_MHealthSpawn
//=====================================================================================
// This function gets called when the world wants to spawn a new mega health box
 
geBoolean Item_MHealthSpawn(GenVSI *VSI, void *PlayerData, void *ClassData)
{
ItemHealth *Health;
GPlayer *Player;
 
Player = (GPlayer*)PlayerData;
 
Player->Control = Item_ControlMHealth; // defined above
//Player->ControlIndex = 9;
Player->Trigger = Item_TriggerMHealth; // defined above
 
Player->Time = 0.0f;
 
Player->Mins.X =-20.0f; // next 6 lines tell engine the items bounding box size (for collision detection)
Player->Mins.Y =-20.0f;
Player->Mins.Z =-20.0f;
Player->Maxs.X = 20.0f;
Player->Maxs.Y = 20.0f;
Player->Maxs.Z = 20.0f;
 
// Set the view info
Player->ViewFlags = VIEW_TYPE_MESH;
Player->ViewIndex = MESH_INDEX_HEALTH;
Player->FrameTime = 0.0f;
 
// Clean up the matrix
geXForm3d_SetIdentity(&Player->XForm);
 
Health = (ItemHealth*)ClassData;
 
// Set the initial pos
Player->XForm.Translation = Health->Origin;
 
Player->VPos = Player->XForm.Translation;
 
return GE_TRUE;
}

Now that you've sucessfully added the new item into the game, you'll need to let the level editor know its there.

Goto genvs.h, line 124

} FogLight;
 
// ItemMHealth
#pragma GE_Type("Item.ico")
typedef struct ItemMHealth{
#pragma GE_Published
geVec3d Origin;
#pragma GE_Origin(Origin)
} ItemMHealth;

The block of code above is read by the gedit.exe level editor. It tells the editor of a new item called ItemMHealth, with an origin property. for more information about defining new entities to the editor, see gedit.hlp, appendix A.

Well, thats all there is to it...

  1. Define control, trigger and spawn functions in game.h and game.c
  2. Write functions...
  3. Write editor interfacing structure.

As with most things, its not perfect...heres a few things that could be done to improve it

  1. Change the model (cannot be done until beta 2 is released) *easy*
  2. Have players health count down 1 health point every second if it is over 100. *probably easy again*

All comments goto steb,you can download a zip file with the source file MHealth.C and a wave sound by clicking here