Snake-like coin system

Following [this tutorial][1] i created random spawn coin system. I want to create coin system the old nokia 3310 snake’s way: when character collides with the coin - the next coin appears in a random space on the screen. How to get rid of “ERROR!” in Spawn Coin Repeat? I know I made something wrong but it’s for me immense problem and I don’t know how to make proper communication between those blueprints. Any idea?

52326-addcoin.jpg

Sometimes blueprints throw an error like that if you connect the nodes before compiling. Delete the error node (which is the function that refers to your event), compile, and try to add the node back.

Edit: Could you also post what the error is saying in the log?

Target must be your CoinSpawning blueprint, you need a reference to the CoinSpawning blueprint in your Coin blueprint and put it in the target pin.

The problem is that you are not referencing your coin spawner. Put a reference to it in the “Target” input of your Spawn Coin Repeat function call, and it should work! :slight_smile:

If you don’t know how, the easiest way here is probably to give the child component’s actor a variable which you set to a reference to its owning parent actor, right after you add it.

You mean the child component’s actor is Coin Blueprint? Unfortunately Sir, but it’s hard to understand for me without screenshot with example. Could you explain using example?

Thanks for reply anyway :slight_smile:

I know I need a reference but the problem is i don’t know what is demanded reference in this case. I can’t connect the nodes, so that means I am using wrong reference. I will put screenshot below in a moment.

If I understand your system correctly, you have a “CoinSpawning” blueprint actor, which creates “Coin” blueprint actors as child actors at random locations in an area. What you need to do is create an actor variable in your Coin blueprint of type “CoinSpawning” (call it e.g. “parent spawner”), and directly after adding the child actor component, set the value of that variable in the child actor you just added to “self”. That will give your child actor a reference to its parent, which you can then use to call functions in it - specifically in this case, the Spawn Coin Repeat function.

An easier way to achieve this, by the way, would be to bind an event in your coin spawner BP to the “Event Destroyed” event of your Coin BP - which will then be called when the coin gets destroyed, and could be used to initialize the spawning of a new coin. That would mean that you wouldn’t have to worry about references to parent and all that, or worry about what the spawner does within each coin - it would let you compartmentalize your logic better.

Firstly I want to let you know that Spawn Coin Repeat isn’t “context sensivite”:

And secondly I don’t know how to put correct reference:

I am really confused at this point. My ideas totally run-down. I know something is wrong, but I can’t even describe it better than now.

That’s because Spawn Coin Repeat is not a function of the Coin actor, but of the CoinSpawning actor. You need to get a reference to it in order to access it.

Here’s a primer on blueprint communication: Direct Actor Communication Quick Start Guide | Unreal Engine Documentation

Like others just tell, in your “Coin” BP you must create new variable for your “CoinSpawning” BP.

If you want it do the easy way, just set that variable to be exposed on create and you can set it in creating (spawning) node, for set it you can just use “self reference” node.

After that you just need get that variable in “Coin” BP and connect it to that Target pin when you trying spawn new coin :slight_smile:

And just for sure, I will spawn new coin before destroying old one. I’m programmer and really I’ll be happy to see people to not cut branch of tree under them… (Like destroying BP and after that need something to do from it).