How to alternate between two actions on ActorOnClicked event

Hello. I am begineer in Unreal Engine 4 and I’m still learning, watching all kinds of tutorials etc. My first really simple project is 3x3 Tic Tac Toe. However, I came across one issue. I have event ActorOnClicked and this just basically checks whether the tile in playboard has been clicked. If not, we then set material (in my case just setting color to sprite). I already know the problem why this isn’t working (event is fired up only once when the mouse is clicked) but can’t find a solution to how to work properly with events and basically alternate between X and Os. Could someone help me with this?

Hi

Can you give me some more details about what you are seeing instead and exactly what you would like this to do? Any additional details may help us find out the best solution.

It looks like your Branch is set to always go to True, which is a dead-end. Here is an example of how you may want to set that up instead:

Let me know if this helps.

Thanks for your blueprint but unfortunately it didn’t work. Color doesn’t seem to change. Here are some more details about it. By following official example ‘Blueprint puzzle’ I wanted to create Tic Tac Toe. I have one blueprint for ClickBlockGrid which has under components just default RenderComponent and nothing else. In Event Graph there’s a code copied from example for creating ClickBlocks, blocks that can be clicked. They are created using ForLoop with public variables Size and BlockSpacing. Then there are just some calculations on positioning of ClickBlock and it is spawned via method SpawnActor. I attached screencap of mentioned blueprint.

Then there I have a second blueprint for ClickBlock. This has as a component Static Mesh which represents one Tic Tac Toe block. Blueprint of this is the screencap that I attached to original question.

So here’s my problem. I would like to change between one of two images (X or O) when user has clicked ClickBlock, like in 2-player Tic Tac Toe. On my turn I spawn Xs and on enemy turn he spawns Os.

issue of this is that event ActorOnClicked is called just once when the ClickBlock is clicked. When I create variable to remember the value if the image has been set, I’m not able to use it anymore because next time I click on ClickBlock, I just call another time event ActorOnCLicked. So with 3x3 grid there are 9 calls to event ActorOnClicked. Hope this helps. I uploaded a screencap with game running (don’t mind the X texture on tiles, they are just for testing purposes)

Hi

So in your original image, you are showing a Sprite that you want to change the color of, but it looks like you are not using Sprite Components to display your ClickBlocks. Can you send me a screenshot of your ClickBlock’s components? If you have a Sprite Component without a Sprite assigned to it, nothing will change color.

If you want to change the color of a material on your Static Mesh Components in your ClickBlock, you will need to set the Material or use a Dynamic Material Instance. Here is some additional information about that:

Hello,

I understand that I should be changing material but for simplicity (I still haven’t worked with Materials, I’m learning blueprints) I’m setting colors and not materials. Ultimately, I’m going to use materials but right now I still haven’t finished coding logic of game, this is where I’m stuck

My problem is that I can’t seem to remember state if the ClickBlock was clicked and if it has set material and then use it to color another ClickBlock with different color. This is basically what I want to achieve by this.[link text][2] Every click will spawn different mark on ClickBlock (in my case just colors). Thank you very much for your help.

I see now. I ran some tests with a remade version of this and I have found that Sprites do not count as a “Clickable” object. If you were to put a print string immediately after the Click Event, you will find that it is not triggering at all.

This is a known issue it turns out.

I fixed this by making a StaticMeshComponent in the ClickBox BP. but I set it behind the Sprite so it is not visible. I scaled it to match the dimensions of the Square Sprite, so it is hidden, but still clickable.

Let me know if that helps.

Hello,

sorry for such a late reply. I probably poorly stated my question. My problem isn’t with ClickBlocks and ActorOnClicked event not firing up. It is working and even the color of ClickBlock is changing after click as it can be seen on image with green ClickBlocks. My problem is that I want to change this color on every second click. 1st click - green color, 2nd click - red color, 3rd - green again and so on. It’s like drawing a different symbol on TicTacToe playboard for both the players (one is using X as his mark, the second is using O as his mark).

I can’t manage to get this working using ActorOnClicked because with 3x3 playboard this event is fired up 9 times because of number of ClickBlocks and even if I set some bool variable at the end of the event, I can’t get its value because it is a new ActorOnClicked event .

Hopefully this time I explained it a little bit better. Sorry for so much hassle and thanks for your great help and advices

Ok, I got it working now. When I click between the boxes, the first click is X, second is O, third is X, etc. I misunderstood you earlier, sorry about that.

First, you will need to make a Game Mode. Make a new Blueprint of the Game Mode class and name it (In my example I called it “TicTacToeGame”)

Now give that new Game Mode a Bool variable (I called mine “IsXTurn?”) that will hold the turn order, since there are only ever 2 players.

In your level’s World Settings menu, you will need to change the Game Mode to this new one we’ve made.

Next, go back to your ClickBlock BP and set it up like this:

The “Cast To TicTacToeGame” nodes are only available if you drag from the “Get Game Mode”

Getting the Bool can only be done from dragging off of the “As TicTacToeGameC” pin on the Cast To node.

I threw in a few comments to help explain it, but it may still be a bit confusing. Let me know if you need any clarification.

Hello ,

thank you for helping me with my issue. I understood all the things you created in blueprint (I worked in Java for some time) and now it all works as it should. However, I’m still struggling to grasp the mechanics and dynamic of Blueprints. Your help gave me better insight on how things in blueprints work. Right now I’m trying to migrate my game mechanics to game mode and exploring the possibilities of PlayerController blueprint. Thank you very much for your help.