Flixel Adventure Game Tutorial – Part 9: Item Trading

Index
Previous part: Picking up items

In the previous part we have programmed how to pick up items from the world and delete items from our inventory. Now we will take a look at item trading, which will incorporate things we have learned in earlier in this tutorial.

Let’s start out by writing a function that will check if a player has a specific item in it’s inventory, and if the player has said item, move the dialogue to a specific line.

private function checkItem(input:String):void
{
	for (var i:int = 0; i < inventory.length; i++ )
	{
		if (inventory[i].name == input.split(",")[0])
		{
			dialogIndex = int(input.split(",")[1]);
		}
	}
 }

And add a separate function for move the dialogue to a specific line.

private function changeLine(line:String):void
{
	dialogIndex = int(line);
}

Since our dialogue is like a tree that has different branches, we should also be able to terminate a branch. Let’s make a function for that.

private function endDialog():void
{
	// Unfreeze
	freeze = false;
						 
	// hide GUI
	GUI.visible = false;
}

In the dialogue with our dear friend Richard, we’ll add a check to see if the player has a pencil in it’s inventory.

char2.dialog = ["Richard: Hey how are you today!",
	"You: I'm ok, thank you.",
	"Richard: Do you have a pencil?",
	"::checkItem@Pencil,5",
	"You: No.",
	"::endDialog",
	"You: Yes."];

Run the game, talk to Richard, pick up the pencil and talk to Richard again. Using the endDialog, changeLine and checkItem functions, you can change the course of the conversation and steer which branch is taken in the dialogue-tree. You now have the tools to make an item-trading dialogue.

char2.dialog = ["Richard: Hey how are you today!", 
	"You: I'm ok, thank you.",
	"Richard: I need to write a letter but I don't have anything to write with. Do you have a pencil?",
	"::checkItem@Pencil,5",
	"You: Nope.",
	"::endDialog",
	"You: Yes, here you go.",
	"::remInv@Pencil",
	"*You give the Pencil to Richard*",
	"Richard: Thanks a lot! Can you take this letter to Mimi, she lives in the house down the road?",
	"::addInv@Letter",
	"*Richard gives you a Letter*",
	"You: I'll see what I can do."];

Richard will trade a pencil for a letter, which he wants you to bring to Mimi (if you don’t know who Mimi is, we haven’t programmed her yet…). Richard repeats himself if you talk to him again. Perhaps we should bookmark a line where we want the conversation to start when you talk with Richard again.

Add the following variable to the Character class.

public var dialogLine:int = 0;

Now, back to the PlayState, we’ll have to specify that the conversation starts with the line stored in the character itself.

Change this:

 // Change text to dialogtext.
dialogIndex = 0;

To this:

// Change text to dialogtext.
dialogIndex = tempChar.dialogLine;

We’ll add a function in the PlayState that changes the character’s internal dialogLine.

private function changeCharLine(line:String):void
{
	tempChar.dialogLine = int(line);
}

Now we can expand Richard’s dialogue even more.

char2.dialog = ["Richard: Hey how are you today!",
	"You: I'm ok, thank you.", 
	"Richard: I need to write a letter but I don't have anything to write with. Do you have a pencil?",
	"::changeCharLine@19",
	"::checkItem@Pencil,6",
	"You: Nope.",
	"::endDialog",
	"You: Yes, here you go.",
	"::remInv@Pencil",
	"*You give the Pencil to Richard*",
	"Richard: Thanks a lot! Can you take this letter to Mimi, she lives in the house down the road?",
	"::addInv@Letter",
	"*Richard gives you a Letter*",
	"You: I'll see what I can do.",
	"::changeCharLine@15",
	"::endDialog",
	"Richard: What did Mimi say?",
	"You: I haven't delivered the letter yet.",
	"::endDialog",
	"Richard: Did you find a pencil yet?",
	"::checkItem@Pencil,6",
	"You: Nope."];

As you can see, ‘programming’ this adventure game becomes more about creating the correct dialogue, which can be confusing. However, you should be able to create a simple item-trading conversation now using the example I have provided for you.



In the next part we will take a look at what Mimi has to say and we’ll program an end and a beginning for the game.

You can download the code for this part here: Flixel Adventure Game Tutorial part 9 code.

Next Part: Winning

Join the Conversation

1 Comment

  1. There’s a very tiny mistake in the dialogue. in the part that reads:
    “::changeCharLine@15”,
    will make the program to begin in this line:
    “::endDialog”,
    however the error isn’t appreciated, because right after the freeze state becomes false, it refreezes and moves to the next line, but the number should be 16, so that the dialogue begins at the next line.

Leave a comment

Leave a Reply to Gabriel Reinhart Cancel reply

Your email address will not be published. Required fields are marked *