Make item consumption modify variable

Hi,


i'd like when an item is used for it to modify a variable, while cheking on which actor the item was used. I know it's possible to trigger a common event when an item is used, but not to check on which actor the item was used. And this is crucial to the little system i'm developing for my game. Think as each actor has a hunger associated variable and each time an item is used it removes some of this variable, while on each day that passes the hunger build up a little.


I checked the game source code and found this


Game_Battler.prototype.useItem = function(item) {
    if (DataManager.isSkill(item)) {
        this.paySkillCost(item);
    } else if (DataManager.isItem(item)) {
        this.consumeItem(item);
    }
};


But can't seem to find where exactly i can recover on which actor was the item used. That way i could make a simple conditional to check and redcue the hunger meter.
don't bother to go to scripting for something as simple as this.


Have the item apply an invisible target state as well as calling the common event.


In the common event, for each possible actor check if the state is applied and if yes, change the variables and remove the state (so that it won't trigger when the item is used again)
don't bother to go to scripting for something as simple as this.


Have the item apply an invisible target state as well as calling the common event.


In the common event, for each possible actor check if the state is applied and if yes, change the variables and remove the state (so that it won't trigger when the item is used again)



@Andar You're the man (or the gal)! Holy damn, it worked. It's still a lot of if elses, specially when different items heals different amount, but it solved the problem. Thanks.


I'm thinking on creating an item script later. Even though Yanfly's item script is very good, i still found that some utilities are missing.
You could also just do the whole thing in the damage formula.  However, I can't tell you how, as you haven't given any information other than "modify a variable, while checking on the actor id" - you haven't given any rules as to why you're checking on the actor id, and what should happen if it's actor 1 or actor 10.
You could also just do the whole thing in the damage formula.  However, I can't tell you how, as you haven't given any information other than "modify a variable, while checking on the actor id" - you haven't given any rules as to why you're checking on the actor id, and what should happen if it's actor 1 or actor 10.



Well, like i said on the first post, think it like a hunger system. Each actor has a hunger meter controlled by in game variables. There are some items that can fill up this meter. Ex: a loaf of bread would "heal" 10 hunger, while a roasted seed would fill 8 and so on. Since each actor has a different hunger meter i need to check on which actor the hunger item is being used. Therefore i can heal the appropriate hunger meter.


I'm also using this for a stress and addiction system. It's pretty much the same thing but with different effects and different "heal" items.
How many actors do you have?  What's the range of variable numbers?  If you use actors 1-10, are you using variables 1-10, or 21-30, or 8, 17, 23, and other numbers with variables for other purposes between them?
How many actors do you have?  What's the range of variable numbers?  If you use actors 1-10, are you using variables 1-10, or 21-30, or 8, 17, 23, and other numbers with variables for other purposes between them?



Well, you don't have to be that specific, Shaz 🙂


But to answer your question i have 8 actors and 5 individual variables for each one of them. The range would be 40 to 80.
lol - yeah, if you want the formula to use, you kinda do have to be that specific.


What's the range of variables for hunger?
lol - yeah, if you want the formula to use, you kinda do have to be that specific.


What's the range of variables for hunger?



It can be all tighted or loose up. Right now i'm using 5 variables slots for each actor. So, var 40-45 Actor 1, and so on. Hunger would the first variable, so 40, 45, 50 and on. But you can separate all of them by type instead of actor. So all hunger variables would be 40-45.
Having them grouped by type would make things easier.


Here's your damage formula - let's say if you give bread to an actor, it gives them 15HP and adds 10 to the variable.  Starting variable is 40 for actor 1, and 47 for actor 8.

Code:
var n = b.actorId() + 39; var o = $gameVariables.value(n); $gameVariables.setValue(n, o + 10); 15

var n = b.actorId() + 39 - this is getting the variable to be modified (actor 1's variable is 39 + 1 = 40; actor 8's variable is 39 + 8 = 47).  This number will be 1 less than the starting variable for that group (so for the 48-57 group, the number will be 47 instead of 39)


var o = $gameVariables.value(n) - this is getting the current value of the variable


$gameVariables.setValue(n, o + 10) - this is adding 10 to that variable - the 10 will change depending on how you want to increment (or decrement) the variable for each item


15 - this is the actual result of the damage formula - if your damage type is set to HP Recover, it will give the target 15HP


This allows you to do all the above without using states or common events.
Having them grouped by type would make things easier.


Here's your damage formula - let's say if you give bread to an actor, it gives them 15HP and adds 10 to the variable.  Starting variable is 40 for actor 1, and 47 for actor 8.

Code:
var n = b.actorId() + 39; var o = $gameVariables.value(n); $gameVariables.setValue(n, o + 10); 15

var n = b.actorId() + 39 - this is getting the variable to be modified (actor 1's variable is 39 + 1 = 40; actor 8's variable is 39 + 8 = 47).  This number will be 1 less than the starting variable for that group (so for the 48-57 group, the number will be 47 instead of 39)


var o = $gameVariables.value(n) - this is getting the current value of the variable


$gameVariables.setValue(n, o + 10) - this is adding 10 to that variable - the 10 will change depending on how you want to increment (or decrement) the variable for each item


15 - this is the actual result of the damage formula - if your damage type is set to HP Recover, it will give the target 15HP


This allows you to do all the above without using states or common events.



Niiice! It worked! Man, how do you know these extra function on damage calc? I thought it could only access actors stats, be them common or ex. The fact that you can use the namespace $ and all its aggregated functions is something really good.


Thanks, @Shaz
Calculating the damage is done by eval'ing the damage formula.  So it's just running a script.  So anything you can do in a script, you can do in a damage formula.  And in MV, the damage formula is MUCH longer than in Ace - I don't know if there's a limit, there probably is, but I ran out of patience counting the characters before I reached the limit - made it to several hundred before I stopped 🙂


FYI, there is an entire pinned thread dedicated to doing fancy stuff in the damage formula.  You might be interested in checking it out.
Calculating the damage is done by eval'ing the damage formula.  So it's just running a script.  So anything you can do in a script, you can do in a damage formula.  And in MV, the damage formula is MUCH longer than in Ace - I don't know if there's a limit, there probably is, but I ran out of patience counting the characters before I reached the limit - made it to several hundred before I stopped 🙂


FYI, there is an entire pinned thread dedicated to doing fancy stuff in the damage formula.  You might be interested in checking it out.



Thanks, i'll check it out. 


Juts tested and it accepts everything from if's to function. Unfortunate it's not really readable so it would be better if the notetag could eval things too. It is what yanfly does in his scripts but it would be nice for this to be packed on mv's core code.
yeah - trying to put a complex script on a single line has its drawbacks.  

Original thread: https://forums.rpgmakerweb.com/threads/make-item-consumption-modify-variable.63163/