There are various shops that appear in role playing games. INNs are probably the most common and easiest to implement. When a player uses an INN his/her health and magic points are restored for a small fee. In some games even status effects are healed. Some sort of INN system is usually mandatory when creating a RPG.

How do I create an INN system?

Well there are various ways to do this. First we will probably need a NPC(Non player character) to represent as a INN keeper. So first create a new object with a visible sprite.

How can I talk to the NPC?

We will need some conditionals. The hero must be right next to the NPC and facing the correct direction. So the player should only be able to talk to the NPC when he is facing them.

The Hero is facing the NPC

So the code would be something like this:

if (distance_to_object(Hero) < 8 and (Hero.x = x and Hero.y = y+32 and Hero.direction = 90)) or
   (distance_to_object(Hero) < 8 and (Hero.x = x and Hero.y = y-32 and Hero.direction = 270)) or
   (distance_to_object(Hero) < 8 and (Hero.x = x+32 and Hero.y = y and Hero.direction = 180)) or
   (distance_to_object(Hero) < 8 and (Hero.x = x-32 and Hero.y = y and Hero.direction = 0))

Okay that makes sense but where do I put this code?

We are going to put this code in a script called create_INN. This will take two arguments: the price of the INN and the name of the INN keeper. Next I create some variables to represent the INN keeper’s dialogue. There is four cases here:

  • welcomeText – When the hero first talks to the INN keeper
  • leaveText – When the hero refuses to stay at the INN
  • needMoreMoneyText – When the hero needs more money to stay at the INN.
  • wakeUpText – When the hero stays at the INN.

First the script checks if the hero is facing the NPC. Then it check if ready = true. Ready just determines the event is ready to proceed. We then create an INN Controller(we will make this next). If the player decides to stay at the INN then we must check if their money is <= the price of the INN. If true the hero stays at the INN else the default dialogue is displayed.

//Useage create_INN(price,name)

price = argument0;
name = argument1;

welcomeText = string(name) + string(“#”) +
              string(“Welcome to the INN, young traveler. We offer the finest rooms at ” + string(price) +
              string(“G a night. Do you wish to stay?”));
              
leaveText = string(name) + string(“#”) +
              string(“That is unfortunate. Maybe next time.”);
              
needMoreMoneyText = string(name) + string(“#”) +
                    string(“I am terribly sorry. You need more gold to stay here.”)
                    
wakeUpText = string(name) + string(“#”) +
             string(“Good Morning! You look well rested.”);

if (distance_to_object(Hero) < 8 and (Hero.x = x and Hero.y = y+32 and Hero.direction = 90)) or
   (distance_to_object(Hero) < 8 and (Hero.x = x and Hero.y = y-32 and Hero.direction = 270)) or
   (distance_to_object(Hero) < 8 and (Hero.x = x+32 and Hero.y = y and Hero.direction = 180)) or
   (distance_to_object(Hero) < 8 and (Hero.x = x-32 and Hero.y = y and Hero.direction = 0))
{
    if ready = true
    {
        instance_create(0,0,INNController);
        textbox_question(welcomeText,1,messageBox,questionBox,”Yes”,”No”);
    }
        
    if global.choice = “Yes”
    {
        if global.gold >= price
        {
            global.gold -= price;
        
            global.RyeHP = global.RyeMaxHP;
            global.RyeMP = global.RyeMaxMP;
            global.CasieHP = global.CasieMaxHP;
            global.CasieMP = global.CasieMaxMP;
            global.ArrowHP = global.ArrowMaxHP;
            global.ArrowMP = global.ArrowMaxMP;
            
            INNController.alarm[0] = 1;
            INNController.displayText = wakeUpText;
        
            
        }
        else
        {
            textbox(needMoreMoneyText,1,messageBox);
            with INNController
                instance_destroy();
        }
    }
    else
    {
        textbox(leaveText,1,messageBox);
        with INNController
                instance_destroy();
    }
 
}

Okay what’s next?

First we need to give the INN keeper some variables. So in the create event:

image_speed = 0.2;

ready = true;

We will need an alarm

if !instance_exists(INNController)
    ready = true;
else
    alarm[0] = 10;

We need this to set ready back to true. Lastly when the hero presses the confirm key:

if ready = true
{
    create_INN(10,”Man”);
    ready = false;
    alarm[0] = 1;
}        

Now we create the INNController?

Yep. The INNController will be mostly just drawing stuff. So create a new object.

alpha = 0.0;
displayText = “”;
ready = true;
   

What is alpha? I know displayText is a string and ready is a bool…

Alpha is going to determine the alpha color of the screen. It will slowly turn the screen to black. I will get to this in a second. displayText is assigned in the create_INN script and you already know what ready does.

Put this in the draw event:

if alpha = 0.0 and ready = true
{
    draw_set_halign(fa_left);
    draw_background(goldBox,336,0);
    draw_text_shadow(346,11,global.gold,c_white,c_black,4);
    draw_text_shadow(455,11,”G”,c_yellow,c_black,4);
}

draw_set_color(c_black);
draw_set_alpha(alpha); // If you want to tint the screen, pass a value less than one.
draw_rectangle(0, 0, room_width – 1, room_height – 1,false);

The first statement draws the player’s currency if the screen hasn’t changed in alpha. The next statements are changing the screens alpha.

alpha += 0.1;

if alpha = 1.0
    alarm[1] = 60;
else
    alarm[0] = 3;

Just changing the alpha values and in alarm1:

ready = false
alpha -= 0.1;

if alpha != 0.0
    alarm[1] = 3;
else
{
    textbox(displayText,1,messageBox);
    instance_destroy();
}

And the destroy event:

draw_set_alpha(1);

Just changes the screen back to normal.

Leave a comment