Daniel Falci
Sign in Buy the course

Cast To in practice: a character that buys furniture

The lesson builds a purchase system with Cast To: the character has a Cash variable and a Purchase custom event with a Price input; a chair Blueprint uses Actor Begin Overlap and Cast to First Person Character and calls Purchase with its price. A Branch checks that the price is not above the cash, so the money never goes negative.

See the course Watch a free lesson

From the course

Taken from the hands-on project of the course; the lessons show every step on video.

What are the steps in the character Blueprint?

What are the steps in the chair Blueprint?

What do I see?

When the character touches the chair the money shows 800. Each touch subtracts 200 more, 600, 400 and so on, until it goes below zero. That is the problem to solve.

How do I stop the money from going negative?

Right after the Purchase event add a Branch with the condition Price less than or equal to Cash. If true, continue the purchase. If false, print Not Enough Money. Now it counts 800, 600, 400, 200, 0 and then refuses.

Why is it reusable?

A second chair instance can also be bought, because Cast To works with more than one instance of the same Blueprint. A table Blueprint with its own box, Actor Begin Overlap, Cast to First Person Character and a price of 500 works too. Once the purchase logic is in the character Blueprint, you do not change it to add new products.

Free PDFs that go with this page

One-page guides made from the lessons, sent by e-mail.

Frequently asked questions

What does Cast to First Person Character do in this example?

It checks that the overlapping actor is the character and gives access to its Purchase event and variables.

Why use a Branch after Purchase?

To compare Price with Cash and avoid negative money.

Can I add more products without changing the character?

Yes. Each product Blueprint casts to the character and calls Purchase with its own price.

Related guides

See the course