Set the new value of an int after a subtract?

Hello, i have a problem that’s really got me scratching my head from a while.

My game’s goin’ pretty well and i’m making some important update, but i’m now stuck.

I’ve set a simple wood and stone gathering system, it works pretty fine, chop a tree/stone and get 10 units updating the ui icon with the actual value.

I set a temporary actor were drop gathered resources, i’m ok until the “add the gathered amount into”, then i can’t find a way to update the inventory value with the new one

The deposit expect 16 stones, so if i have 20 stones in the inventory i shoud be left with 4.
I tried to swap the Dep Pietra and Pietre in a Minus node afterward but it give wrong value, i tried to make a new variable “Difference” were i add Pietre to it, then subract to Pietre again, but it always give 0 the second time i drop something.

Edit: Some wrong math sorry.

If the result of this is not the expected then the problem is elsewhere:

I’m managing this inside the game mode blueprint, i’m calling it into the deposit actor, and logic says that’s the right math, but it output a wrong value.
If i have 10 stones and the deposit expect 16, when i drop those the deposit say 6 but my inventory output 4.

If instead i try to minus the original deposit value it’s wrong aswell since it will always subtract 16 no matter the amount of stones dropped.

With the Pietre as max value it keep subtract the value exponentially, if i remove and leave at zero it will always 0 my inventory.

Why are you subtracting from DepPietra and why are you using a clamp when all you want is to subtract from Pietre?

I’ll try to explain.
I’m making a simple resource gather system.
I chop down a tree or mine a rock and i get 10 units of (Pietre in this case).
There’s a placeholder deposit that should be a building to rebuild, and need an x amount of resources to finish (DepPietre).
I have to subtract Pietre (the amount in my inventory) to DepPietre (what it need), but i can’t find a way to update my inventory value.
Hope it’s clear, or i’ll try to make some screenshots or a video if i can post it.
Sorry for the variable names, i’m using italian for my convenience.

The clamp it’s to avoid to go under 0.

It is.

It should be Pietre - DepPietre. You want to only subtract if Pietre >= DepPietre or subtract from Pietre until its 0, as in multiple deposits to reach the requiered cost?

You said you are doing this in the game mode. How are you doing communication between deposit actor ↔ pawn ↔ game mode?

I want to subtract until the DepPietre it’s 0, it’s a placeholder for a burned building to rebuild, it will need a set amount of wood and stones, ence the subtract.
I should try to convert my inventory Pietre into an Array instead of a single?

I’m casting to the Game mode from the deposit actor, i make it so i have a global character inventory and deposit variables so i can set the amount of resources required into the single actor and make a single cast instead of many to multiple actors.
The deposit have a simple overlapping box collider to interact and call the function DepositaPietre from the Game mode with a gate, and AggiungiPietreInventario when mining from a rock.

Mind that both, inventory and deposits, are simple number and i don’t have an actual inventory widget, only two icons in the upper right corner that i update inside the widget with a bind.

Made a demo project to see if I have a clear idea of what you are after. Please let me know if I understood:
Materials

These are the 2 functions you want to play around with:

DepositMaterials in BP_Deposit

GetMaterials in the BP_GameInstance

DepositTest.zip (70.9 KB)


The math should be the same for whatever you decide to use to hold the inventory.

Hope it helps.

I take a look and keep you posted, thanks in advance!

Sorry but i can’t open it, i’m on 5.0.3, i try to replicate.
The GameInstance it’s my GameMode or another bp?

I used game instance. You can easily move it to game mode or any actor / component.

Can you convert to a previous version? I’m looking into but there are many variables and seems one bp interface, but i don’t know how was set, i get an error that it can’t load map and the folders are all empty.

Sorry, 5.2.1 is the one I have atm.

It’s a simple interface with 1 in 1 out int parameter that was overwritten in the game instance:

Best I can do right now is add comments:

Deposit actor

Used a timer to call the function every second to test that the math is working:

Game Instance


Hope it’s clear enough.

Ok i’ll give it a try, at worst i’ll install 5.2.1 :sweat_smile:

1 Like

Oh my i think i solved by accident XD
Just to be sure if you can confirm that’s not a coincidence and it’s actually working:


I kept the gathering logic and call for the function inside the game mode when dropping:


Then i made a local variable (Tempo Pietre) and did the math, and it seems to work, i tried a couple of times with different amount of materials and the numbers updates fine.
Am i not just dreaming? XD

To be fair it’s a mix of your suggestions and some other tutorials i watched about inventory and ammo reload systems.

Maybe it’s a “stick and stones” solutions but seems to work, hope it won’t create problems afterward.

Great!

I’m having a hard time following because I can’t read italian (despite my name… i know).

The way I’ve seen it usually be done is you have 1 input parameter with the ‘ask’ amount, you check how many is left and then you output the ask or how much is left. So even if yours is working, still think you should move it to a function and the only int you need for Pietre is how many you have in that class, no this:
image
That’s why the interface has an in int and returns an int:
image
In the demo I made I only have 1 in for the material:
image

This is by no means the only way or the best way to go about this. Its up to you to pick and chose.

IMO not knowing why it is working is just as bad as it not working. :yum:

Yeah i seen mostly using data tables or levels, but i started with two variables for each material for own commodity and two for related deposit, Pietre (stones) and Legna (Wood). After some more thinkering everything seems to align, and as someone said, if it ain’t broken don’t touch it XD