Download

Working with Variables and Interfaces

I’m sure the answer is easy.

I have a simple system where the player has to bring “coins” to a beacon object. I have set up an interface where the coin is being destroyed when overlapping with the beacon.

Now I am just trying to “add +1” coin to this system.

My first solution was to create two variables :
-CoinValue in the Coin BP
-Coins in the Beacon BP

This is my first set-up:
(In CoinBP > Interface)

f8ef16402d0adbbc59e020a06d028d4344d417b2.jpeg

Since I couldn’t solve the error, I thought maybe I didn’t have to Cast to the Beacon, so I just created another Coins Variable in the Coin BP and tried it that way:

ss1.JPG

Now I get no errors, but when I test it, my Coins don’t add up!

I’m sure I’m missing something very basic… Thanks in advance!

What was the idea behind using an interface for this? Is it because multiple things can receive coins?

One object can receive many types of “coins”.
Plus it’s just easier to handle, duplicate the BP and there you go.

You think there would have been a better solution?

I was just confused as to why you’d use an Interface unless you had multiple types of beacons that could all receive coins.
Why not just make a Parent Blueprint called “coin” and then make child blueprints of that one? Whenever you encounter something, you cast it to coin then call the function to give coins.
If all the coins just add more coins to your total any way and you don’t have different kinds of coins (like blue, red, green currencies instead of just red) then you might as well make use of inheritance as all the coins share certain traits.

Definitely need to learn about that. Thanks for the info!

There might be a misunderstanding here. Those coins are not absorbed by the player; he really pushed them physically to the beacon. This is why I was using an interface.

Yes I understand that, but whenever your beacon encounters a coin does it need to know what kind of coin it is at all? Or does it just need to know how many coins to add to the total?
Unless you have multiple types of Beacons that all need to be able to take coins, then there is no reason to use an interface. An interface should be used when you have multiple entities in your game, that needs to have the same functionality but you don’t care about the implementation.

So say you have a door, a soda machine and a cash register. When you look at any of those and press a button, a raycast is thrown out and hits one of those three items. No matter which of the three items you hit, you just want them to do a “Use” function call and then do whatever that entails. An interface can help here because while the door can open with the Use Function, the Soda Machine can dispense a soda and the cash register plays a “sales” animation. But they all make use of the same interface.

I’ve made a video on this subject here:
https://youtube.com/watch?v=hzMgcVgMRe0

Yes my Beacon need to know what enters it, plus at the moment there is just one Beacon but I want to leave open the possibility of having more as a gameplay variant.

Thanks for helping defining what an Interface is since I’m just starting out with them but this is more or less what I had in mind.

I will now watch your video out of curiosity, but I’ve never had an answer to the question: Is it even possible to make use of Variables within an Interface?

I sincerely appreciate that you’re taking your time to answer me!

Interfaces are strictly for Method Signatures. Nothing else.

So their were wrong suggesting it was the solution for what I needed yesterday?

I’m so confused now!

You want to store the value int on your money actor blueprint then create a value int in your game instance then on your thirdperson blueprint you want to get an onactorbegin overlap then cast to money blue print and off money blueprint get your money value int then cast to your game instance and get your value int from that and then you want to set the money value from your game instance to both of them added up. I’m sorry that’s hard to follow but if you read it carefully and recreate that you should be good the reaon your first example doesn’t work is that you don’t have your object pin connected up you want to connect that to the on begin overlap in your third person.

Wow talk about confusing. My poor french/ue4 beginner brain is dizzy just reading that.
I just wonder why everybody seem to have their version of the answer.

Will study your encrypted message and let you know about it! :stuck_out_tongue:

It looks like you’re telling the engine to remove the coin counter each time it tries to add it. and destroy also the converter node
No wonder it can’t add the coins.

Programming is why. Programming lets you solve the same issue in a great many ways.

here I’ve made one up for you here it is sorry my screen capture is playing up so its in picture form

https://forums.unrealengine.com/attachment.php?attachmentid=137538&stc=1

https://forums.unrealengine.com/attachment.php?attachmentid=137539&stc=1

https://forums.unrealengine.com/attachment.php?attachmentid=137540&stc=1

https://forums.unrealengine.com/attachment.php?attachmentid=137541&stc=1

https://forums.unrealengine.com/attachment.php?attachmentid=137541&stc=1

https://forums.unrealengine.com/attachment.php?attachmentid=137542&stc=1

https://forums.unrealengine.com/attachment.php?attachmentid=137543&stc=1

https://forums.unrealengine.com/attachment.php?attachmentid=137544&stc=1

https://forums.unrealengine.com/attachment.php?attachmentid=137545&stc=1

https://forums.unrealengine.com/attachment.php?attachmentid=137546&stc=1

Sorry I know that is also quite hard to follow but my capture software isn’t working atm but that works here’s a zip of all the photos they’re numbered in order so follow it step by step

actually it wont let me upload the zip and there’s a few pictures missing in that last post I made you should still be able to follow it though if you send me your email I can send the zip to you otherwise youll just have to try your best to follow it but I tested that example and it works.

I’ll try my best to walk you through it.

  1. create a game instance

2 ad variable in game instance called total money or something like that. make it editable and expose on spawn.

3 go to edit in the ue4 browser and editor pref then maps and modes then scroll down to game instances and find the one you just created name it something easy to recognise like my game instance or the like

4 vreate an empty actor and a sphere collision and a static mesh

5 add a variable called value and make it editable and expose on spawn

6 in the event graph for your money actor do what I showed you in the picture compile and save

7 drag your money actor on to the level

8 go to your thirdperson character blueprint and add all the script I showed you in the picture then compile and save and it should all work.

Hope this was helpful its quite hard to show you without avtually showing you.

Man you’re the best, I can’t believe how dedicated you are to help other people. I’m a generous person, but you’re setting a new standard here!

I got it working yesterday night though. I was convinced to have sent an update on the thread to mention it, thanking everybody in the meantime, but apparently I was so tired that I didn’t send it or something.

I made it through blueprint class entirely. When the coinBP is overlapping with the beaconBP, the beacon detects it and stores the variable. Nothing else. It was super simple but for some reason, and without really understanding how, it appears I was making the system look more complicated than it actually was. And I was convinced it could be done by blueprint class entirely.
Again, I’m an absolute beginner here, and maybe this will create a problem I am not yet aware of. To me the logic seems respected and so far, no bug, no warning no nothing.

That said, I will now proceed to follow the tutorial you took the time to put up for me. I’m here to learn and nothing else, and I couldn’t be happier that this community seem to have the same obsession.

Will let you know if it works using your method! Infinite thanks!

Man I wish I had seen this before I jumped on to my desktop and made a walkthrough :stuck_out_tongue: Well done on getting it working I would advise trying my way though as at the moment your way will not save data between levels. Currently if you were to load a new level the data stored would be reset our ways are not that different the only difference is that I use a custom game instance the good thing about using a the game instance to store data is that it stores data between levels. I tried to go as slow as possible to help you follow. Also thanks for the compliment I’m just learning as well and have received a lot of help from this forum so it’s nice to give back where you can.

here’s the walkthrough : also I forgot to cast to thirdperson character in the moneyblueprint before the destroy actor it still works without it but ATM if any other actor where to overlap it would destroy the money actor so you want to cast to thirdperson so that it only works for your character.
https://www.youtube.com/watch?v=vZBFc1r6zlQ&list=PLdkKGnZwGKqS0PMPHrgmq57g4UOMoeNFt&index=12