Make item consumption modify variable

PresaDePrata

Veteran
Veteran
Joined
Dec 4, 2014
Messages
61
Reaction score
7
First Language
Portuguese
Primarily Uses
RMMV
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.
 

Andar

Veteran
Veteran
Joined
Mar 5, 2013
Messages
29,567
Reaction score
6,892
First Language
German
Primarily Uses
RMMV
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)
 

PresaDePrata

Veteran
Veteran
Joined
Dec 4, 2014
Messages
61
Reaction score
7
First Language
Portuguese
Primarily Uses
RMMV
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,837
Reaction score
12,250
First Language
English
Primarily Uses
RMMV
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.
 

PresaDePrata

Veteran
Veteran
Joined
Dec 4, 2014
Messages
61
Reaction score
7
First Language
Portuguese
Primarily Uses
RMMV
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.
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,837
Reaction score
12,250
First Language
English
Primarily Uses
RMMV
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?
 

PresaDePrata

Veteran
Veteran
Joined
Dec 4, 2014
Messages
61
Reaction score
7
First Language
Portuguese
Primarily Uses
RMMV
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,837
Reaction score
12,250
First Language
English
Primarily Uses
RMMV
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?
 
Last edited by a moderator:

PresaDePrata

Veteran
Veteran
Joined
Dec 4, 2014
Messages
61
Reaction score
7
First Language
Portuguese
Primarily Uses
RMMV
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.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,837
Reaction score
12,250
First Language
English
Primarily Uses
RMMV
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.
 

PresaDePrata

Veteran
Veteran
Joined
Dec 4, 2014
Messages
61
Reaction score
7
First Language
Portuguese
Primarily Uses
RMMV
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
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,837
Reaction score
12,250
First Language
English
Primarily Uses
RMMV
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.
 

PresaDePrata

Veteran
Veteran
Joined
Dec 4, 2014
Messages
61
Reaction score
7
First Language
Portuguese
Primarily Uses
RMMV
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.
 
Last edited by a moderator:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,837
Reaction score
12,250
First Language
English
Primarily Uses
RMMV
yeah - trying to put a complex script on a single line has its drawbacks.  
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

So... I work for 8 hours a day, but apparently I'm "too noisy" for people staying home all day and I need to be quiet or they will escalate things. o_O People be dumb.
Had no time to work more on my Redwood tiles yesterday and no time yet still today--But! Somehow found time to make sit health potion props.
Today I learned that George Washington was not the first president of my country, and that there were technically over a dozen presidents before him. My own ignorance astounds me, I mean it always does but the sheer scope of this revelation astounds me. I guess George Santayana really was quite astute when he said:
Currently enjoying FF7 Remake :biggrin:
Day 753: I think im just about ready to finish the planning stage of my game creation and actually start to make it :0

Forum statistics

Threads
95,776
Messages
931,832
Members
126,186
Latest member
KyuKino
Top