Using MP spent during turn to return MP to actor

Point08

Veteran
Veteran
Joined
Feb 10, 2015
Messages
123
Reaction score
119
First Language
English
First, the problem. I have a bit of code that based on my thought process, with an MP cost of 10, should result in the user getting back 6 MP. The result though, is always just 5 MP. Here is the code:

Code:
var tempMP = ($gameVariables.value(12[i]) - $gameVariables.value(11[i]) * -0.1) + 5;
			$gameParty.members()[i].gainMp(tempMP);
If you're still looking at this, then here's the longer explanation. All mages (determined by a passive state, 11)have their MP set to 5 (first 0, then with 5 added, done this way as the 5 will later be a variable amount) at the start of battle. The idea is that then each mage party member has their MP stored in an array at the start of the turn. They cast their spells or attack or whatever, and at the end of the turn, they get back 5 MP + 10% of whatever MP they used during that turn. Unfortunately, if a party member uses a spell that costs 10 MP, instead of the 6 MP I want to be returned at the end of the turn, they only get 5 MP returned. I have the code for returning MP set to run at the end of turn. Any help with this would be great, as I've tried changing around multiple things, and I can't seem to get it to work how I want.

Quick note: I know I could return the MP directly through each magic skill in the damage formula, but I'm planning on adding more variance to this in the future, and eventually hoping to move it from the troop events to a plugin, so I'd like to get this to work, if at all possible. Also of note, I'm (probably obvious by how awful this code is) new to js, so please excuse any poor design in this.

Code in Troop Battle Event
Page: 1
Condition: Turn 0
Span: Battle
Code:
function battleStartMP(){
	
	for(var i = 0; i < $gameParty.members().length; i++){
		if ($gameParty.members()[i].isStateAffected(11)){
			$gameParty.members()[i].gainMp((-$gameParty.members()[i].mp) + 5);
		}
	}
	return
}
battleStartMP();

Page: 2
Condition: Turn 1*X
Span: Turn
Code:
$gameVariables.setValue(11, []);
function setInitialBattleMP (){
	for(var i = 0; i < $gameParty.members().length; i++){
		if ($gameParty.members()[i].isStateAffected(11)){
			$gameVariables.setValue(11[i], $gameParty.members()[i].mp);
		}
	}
return;
}
setInitialBattleMP();

Page: 3
Condition: End Turn, Turn 1+1*X
Code:
$gameVariables.setValue(12, [])
function setTurnEndBattleMP (){
	for(var i = 0; i < $gameParty.members().length; i++){
		if ($gameParty.members()[i].isStateAffected(11)){
			$gameVariables.setValue(12[i], $gameParty.members()[i].mp);
			var tempMP = ($gameVariables.value(12[i]) - $gameVariables.value(11[i]) * -0.1) + 5;
			$gameParty.members()[i].gainMp(tempMP);
		}
	}
return;
}
setTurnEndBattleMP();
 

Wavelength

MSD Strong
Global Mod
Joined
Jul 22, 2014
Messages
5,624
Reaction score
5,104
First Language
English
Primarily Uses
RMVXA
I've done less work with JS than with Ruby (and I'm too tired to test my guesses by trying the troop events myself), but my guess is that it's returning 5MP because ($gameVariables.value(12) - $gameVariables.value(11) * -0.1) is always returning 0 - and that the reason that is happening is because your arrays are never actually populated correctly. I think one of these two things is going wrong: either you can't set the value of 12 without first pushing in a value so that 12 exists (push in a value of 0, then set it), or the form $gameVariables.setValue(12... won't work because of the way that setValue works (set the value or the array item directly rather than using the method). You can test whether my assumptions are right by printing out the value of Variable #11 or Variable #12 to a message box (\V[11]) using an event command - or to the console (I think it's $gameVariables.getValue(12) ) after you run that script block.

If I were to try to create this kind of mechanic, I would track the MP consumption of each actor directly, from the methods responsible for paying skill costs (this will prevent stuff like MP damage or Item-based MP heals from counting), and I would create a property of GameBattler to store it in.
 

Point08

Veteran
Veteran
Joined
Feb 10, 2015
Messages
123
Reaction score
119
First Language
English
@Wavelength You're right. One, it was never actually creating an array, because if I logged the value of variable 11, I got 0. I changed the first line to:
Code:
$gameVariables.setValue(11, [0, 0, 0, 0]);
You're also right that setValue isn't actually populating the array with my actor's MP, as with the above line, logging variable 11 after the code executes gets me [0, 0, 0, 0].

I'm not sure how to get my data into the array without using setValue in this case, but either way you brought up a good point about things other than just direct MP usage possibly changing the returned value. Now that you've pointed out the problems, I guess I'll have to do some more digging to figure out how to solve them. Thanks for the help!

Edit: I was able to pass the actor's MP into variable 11 by changing the code to:
Code:
function setInitialBattleMP (){
	for(var i = 0; i < $gameParty.members().length; i++){
		if ($gameParty.members()[i].isStateAffected(11)){
			if (i == 0){
				$gameVariables.setValue(11, $gameParty.members()[i].mp);
			}
		}
	}
return;
}
setInitialBattleMP();
This would work if I made a separate variable for each party member, but I'd have to do that for their starting MP and ending MP, which would mean 8 variables, which while certainly workable, seems wasteful. Not too mention it still doesn't solve the problem you brought up of MP damage attacks, or MP regen abilities or anything else changing the numbers. I'm also still unsure why I'm able to pass value this way, but not pass it into an array in variable 11.
 
Last edited:

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

Latest Threads

Latest Posts

Latest Profile Posts

so hopefully tomorrow i get to go home from the hospital i've been here for 5 days already and it's driving me mad. I miss my family like crazy but at least I get to use my own toiletries and my own clothes. My mom is coming to visit soon i can't wait to see her cause i miss her the most. :kaojoy:
Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD

Forum statistics

Threads
105,868
Messages
1,017,074
Members
137,578
Latest member
JamesLightning
Top