I am trying to make a score like Swing ninja, where you pick up an actor and it adds to your score, but I couldn’t make sense of the example in Swing Ninja. I got the pickup done, but I want it to update every time the player overlaps the pickup. In code you could just say Score = Score + Whatever, but no idea how to do that in Blueprint.
Your setup is correct, that is indeed how to add and set int.
Is it even printing the new value? Maybe that event isn’t being called. Put a breakpoint on a node (e.g. click Spawn Emitter at Location and press F9), then play your game and see if it’s halting there.
I set this to Auto-Receive inputs (it’s in the BP’s Defaults if you wanna try) and then drop it in the level. As I spam the G-Key, I get a constant upward count. 1… 2… 3… etc.
No, it just prints 1 over and over. Is it because each pickup is individual, and so each is running the same code without adding to some global variable, say in the level or on my character blue print?
No, you’d want that to be within the player or within some other class that overarched your game. You probably COULD do it in the pickup, but then all pickups would have to be talking to one another all the time, which would be unnecessarily heavy.
You shouldn’t really need an interface. Blueprints are built to be able to communicate with each other directly. You just need to know how to call them. One possible approach would be:
I made a new Class Blueprint called ScoreHolder. It has a Score variable (int), and a function named Increment Score. The graph for that function looks something like this:
From your pickup class, use a Begin Play event to trigger a Get All Actors of Class, set to look for your ScoreHolder class. This will store them in an array. Get the first (0) element of that array and cast it to a ScoreHolder class. This is required because that node doesn’t know what’s in there by itself. Take the output of that and store it into a new variable with a type of ScoreHolder.
Still with me?
Then you can just use that variable to call your Increment Score function whenever you like. The graph for my (not quite a) pickup class looks like this:
Also, note that you don’t have to make a separate class just to hold your score. You can put that in your GameMode (that’s probably where I’d put it), or on your character, or whatever you like.
Hey Luka, as counterpart0 discovers earlier in this post, his problem was that he was doing all the math in the pickup class itself. That’s a problem since each pickup is basically only keeping its own score when it gets picked up. What you need is for some other Blueprint (the character, the game, a separate actor) to keep track of when things get picked up, and increment the score from outside the pickup.
All of this is covered in the posts above. I even put in some pretty pictures! I hope they help!
EDIT: This answer brought to you by who is having AnswerHub difficulties at this time.
You can create the variable when you click Asscore holder C (blue pin), then choose “Promote to variable”, it automatically creates the Score holder variable then.