This is just a simple example of lite c code for text conversations with NPC's, can also be used for objects.
var anim_percentage = 0;
STRING* player_message_str = "#100"; // the message can have up to 100 characters
STRING* npc_message_str = "#100"; // the message can have up to 100 characters
FONT* adventure_font = "adventure.bmp"; // This Font is a bitmap placed in the folder with your Level Files.
// you should have a few bitmap font images with 3DGS
// pick different coloured fonts for good effect.
FONT* arial_font = "Arial#20b";
//SOUND* message_wav = "message.wav";
TEXT* player_message_txt =
{
pos_x = 200;
pos_y = 20;
string(player_message_str);
red = 255;
font(arial_font);
flags = SHOW;
}
TEXT* npc_message_txt =
{
pos_x = 200;
pos_y = 60;
string(npc_message_str);
red = 255;
font(adventure_font);
flags = SHOW;
}
function player_msg_show(STRING* message, duration)
{
str_cpy((player_message_txt.pstring)[0], message);
player_message_txt.flags |= SHOW; // display the message
//snd_play(message_wav, 70, 0);
wait (-duration); // for the specified number of seconds
player_message_txt.flags &= ~SHOW; // and then hide the message
}
function npc_msg_show(STRING* message, duration)
{
str_cpy((npc_message_txt.pstring)[0], message);
npc_message_txt.flags |= SHOW; // display the message
//snd_play(message_wav, 70, 0);
wait (-duration); // for the specified number of seconds
npc_message_txt.flags &= ~SHOW; // and then hide the message
}
function npc_move()
{
while (anim_percentage < 40) // the loop runs until the "idle" animation percentage reaches 100%
// lower percentage if you want movement to stop when talking stops
{
ent_animate(my, "idle", anim_percentage, NULL); // set the animation you want here "idle", "stand" or whatever
anim_percentage += 0.1 * time_step; // controls the animation speed
wait (1);
}
anim_percentage = 0;
}
action NPC_Merchant() // attach to your NPC
{
// use a wave file with speach if you want to
//SOUND* advice_wav = "advice.wav";
//PLACE ALL OF THE REQUIRED SPEECH TEXT IN THIS ACTION SECTION - YOU COULD USE A DIFFERENT ACTION SECTION
//FOR EACH NPC THAT YOU WANT TO SPEAK INCLUDING TEXT FOR OBJECTS.
while (!player) {wait (1);}
while (vec_dist (player.x, my.x) > 130) {wait (1);} // wait until the player comes really close to the NPC
npc_move();
player_msg_show("CHUCK.. what have you got to sell trader", 6); // 6 is the length of time the text is on the screen
// increase for longer times
wait (-4); // length of time between messages - increase or decreas as required
npc_msg_show("TRADER.. how much Dirham do you have sir", 4);
wait (-4);
player_msg_show("CHUCK.. give me your best price for that ruby encrusted sword", 6);
wait (-4);
npc_msg_show("TRADER.. sorry that item is not for sale.", 4);
}