In my game, I want there to be an NPC that tells you how many of a certain enemy was killed. I have set up a variable that increases by 1 every time the enemy in question is killed. what should i do here?
can someone help me write a script
RPG Maker MZ SupportOK, I'm assuming that you are very new to the makers and to game development.
As such there are two things you should do first, before continuing with that quest and its events.
those two things are following the build-in tutorial of MZ available in the editor's help section, and follow the link to the "starting point" tutorial in my signature.
The starting point tutorial explains a few very basic things that you should know even before making your very first game, but it also has links to several tutorials and tutorial games.
one of those tutorial games is the "variables guide", a game that explains a lot of uses of variables and how to implement quests. That is knowledge that you need before you can handle your quest.
that said, there are a lot of possible places where your variable can have gone wrong. for example if you placed it inside a troop event, the last enemy killed will not count due to a quirk of the combat engine (battle victory is processed without troop events).
there are two ways around that, either using an immortal state on the troop events (which requires a bit more eventing) or using "bounty items", e.g. having all rats drop rat tails and then check (not count) the rat tails for the quest.
in all cases you'll need an event as the quest check that uses conditional branches to see if the quest is fulfilled or not.
As such there are two things you should do first, before continuing with that quest and its events.
those two things are following the build-in tutorial of MZ available in the editor's help section, and follow the link to the "starting point" tutorial in my signature.
The starting point tutorial explains a few very basic things that you should know even before making your very first game, but it also has links to several tutorials and tutorial games.
one of those tutorial games is the "variables guide", a game that explains a lot of uses of variables and how to implement quests. That is knowledge that you need before you can handle your quest.
that said, there are a lot of possible places where your variable can have gone wrong. for example if you placed it inside a troop event, the last enemy killed will not count due to a quirk of the combat engine (battle victory is processed without troop events).
there are two ways around that, either using an immortal state on the troop events (which requires a bit more eventing) or using "bounty items", e.g. having all rats drop rat tails and then check (not count) the rat tails for the quest.
in all cases you'll need an event as the quest check that uses conditional branches to see if the quest is fulfilled or not.
dude i know how to use variables... i just want to know how to do one script. IDK how to make the variable number show up in a text box is all.
I can simplify this all for you 🙂
With my custom script anywhere plugin you can do this quite easily without needing to add any battle events... then with this you can easily setup troops with multiple enemies and it will only add to the variable if the specific enemy is killed (aka. collapsed) and best part is without having to setup any battle events 😉
Use this:
[RMMV-MZ] Custom Script Anywhere by CT_Bolt
ct-bolt.itch.io
...Like this:
Class: Game_Enemy
Property of Function: prototype
Function: performCollapse
Post Script:
JavaScript:
var enemyId = 1; /* Change this to your specific ID */
var varId = 1; /* Change this to your desired Varaible */
if (this._enemyId === enemyId){
var value = $gameVariables.value(varId);
$gameVariables.setValue(varId, value+1);
};
Then to display the variable in a text box is like this:
Hope that helps?
If you hover over the cursor line in the text box it will display all the text commands, including how to display variables (which as ct_bolt said, it's \v[x] where x = the id of the variable).
Adding on to my previous post...
found another way to do it and also display in real-time onto the battle log 😉
Instead of what I had done in the previous post this time use the Function "displayActionResults" from the Class "Window_BattleLog"
Sooo...
Use this:
[RMMV-MZ] Custom Script Anywhere by CT_Bolt
This time though... Use it like this:
Class: Window_BattleLog
Property of Function: prototype
Function: displayActionResults
Post Script:
That will add to the variable and also display a message in the Battle Log window 😉
Really nice results from doing it that way looks like this:
found another way to do it and also display in real-time onto the battle log 😉
Instead of what I had done in the previous post this time use the Function "displayActionResults" from the Class "Window_BattleLog"
Sooo...
Use this:
[RMMV-MZ] Custom Script Anywhere by CT_Bolt
This time though... Use it like this:
Class: Window_BattleLog
Property of Function: prototype
Function: displayActionResults
Post Script:
JavaScript:
if (!target.isActor()){
var enemyId = 1; /* Change this to your specific ID */
var varId = 1; /* Change this to your desired Varaible */
if (target._enemyId === enemyId){
if (target._states.includes(target.deathStateId())){
var value = $gameVariables.value(varId);
$gameVariables.setValue(varId, value+1);
this.push("addText", $dataEnemies[enemyId].name + "'s killed: " + $gameVariables.value(varId));
};
};
};
That will add to the variable and also display a message in the Battle Log window 😉
Really nice results from doing it that way looks like this:
I think the easiest place to start is with the enemy collapse function in the rpg_objects.js
Meaning the variable will increase at the same time the enemy goes down.
If there's only 1 enemy you want to keep track of, this will be easier
You can just stick this line in there.
if (this._enemyId == *the enemy id you're tracking* ){ $gameVariables.setValue(variableId, oldValue + 1)};
The "variableId" being the variable number you're storing this in.
And the oldValue = $gameVariables.value(variableId)
Although, I'm guessing you'll probably want to track as many enemy kills as there are enemies.
$gameVariables can store object data like {enemy1: 0, enemy2: 0...}.
As far as I know there's not a limit.
And, imo, it's better to use just 1 Game Variable, than one fore every possible enemy.
The catch is, you can't read this data in the Debug menu or with the show message \v[] characters
It'll just display "[object object]"
So if you want to read and display the value of "Enemy2" you'll have to do a manual script call to read and write a specific value to another game Variable to be displayed
$gameVariables.setValue(24, $gameVariables._data[23].enemy2 )
Show Message: "Enemy2 defeated: \v[24]"
Right. So. Going back to the Enemy collapse function. What you really want to do is plug something like this in there above: switch (this.collapseType()).
Translation: If it's a new game then this Variable, #23, needs to be defined first.
And, if it's a new enemy we've never defeated before, it doesn't have a key:value, we also need to define that too in order to add to it next.
Then we can add +1 to the key:value. In this case the key is "this._enemyId". "This" is understood as the defeated enemy that is collapsing.
So if we defeated the first enemy in the database for the first time, the data might look like this
{"1":1}
And if we won another battle against it and the second enemy in the database, it would change to this
{"1":2, "2":1}
We could assign the keys by ._name instead of ._enemyId to make this a little easier to read
{"Goblin": 2, "Hornet":1 }
So our defeated enemy data is stored in Variable23 as this object data.
Like I mentioned above, to retrieve it we need to assign the value of, say, "Goblin" to Variable24
Script: $gameVariables.setValue(24, $gameVariables._data[23].Goblin)
-Or- if we use ._enemyID for our keys
Script: $gameVariables.setValue(24, $gameVariables._data[23].1)
Show Message: "Goblins defeated: \v[24]"
But I must point out again that the script can't work with something that isn't there. Variable23 and the Key:Value need to be defined. When and Where? I can't say. The "if statements" above partially solve this. But that's on the enemy collapse. To prevent errors, you might repeat those lines anywhere you'll report "goblins defeated." So that you can at least report "0" if you've never been in battle or an encounter.
Meaning the variable will increase at the same time the enemy goes down.
JavaScript:
Game_Enemy.prototype.performCollapse = function() {
Game_Battler.prototype.performCollapse.call(this);
switch (this.collapseType()) {
case 0:
this.requestEffect("collapse");
SoundManager.playEnemyCollapse();
break;
case 1:
this.requestEffect("bossCollapse");
SoundManager.playBossCollapse1();
break;
case 2:
this.requestEffect("instantCollapse");
break;
}
};
If there's only 1 enemy you want to keep track of, this will be easier
You can just stick this line in there.
if (this._enemyId == *the enemy id you're tracking* ){ $gameVariables.setValue(variableId, oldValue + 1)};
The "variableId" being the variable number you're storing this in.
And the oldValue = $gameVariables.value(variableId)
Although, I'm guessing you'll probably want to track as many enemy kills as there are enemies.
$gameVariables can store object data like {enemy1: 0, enemy2: 0...}.
As far as I know there's not a limit.
And, imo, it's better to use just 1 Game Variable, than one fore every possible enemy.
The catch is, you can't read this data in the Debug menu or with the show message \v[] characters
It'll just display "[object object]"
So if you want to read and display the value of "Enemy2" you'll have to do a manual script call to read and write a specific value to another game Variable to be displayed
$gameVariables.setValue(24, $gameVariables._data[23].enemy2 )
Show Message: "Enemy2 defeated: \v[24]"
Right. So. Going back to the Enemy collapse function. What you really want to do is plug something like this in there above: switch (this.collapseType()).
if ($gameVariables._data[23] == undefined){$gameVariables._data[23] = {}};
if ($gameVariables._data[23][this._enemyId] == undefined){$gameVariables._data[23][this._enemyId] = 0};
Object.assign($gameVariables._data[23], {[this._enemyId]: $gameVariables._data[23][this._enemyId] +1});
Translation: If it's a new game then this Variable, #23, needs to be defined first.
And, if it's a new enemy we've never defeated before, it doesn't have a key:value, we also need to define that too in order to add to it next.
Then we can add +1 to the key:value. In this case the key is "this._enemyId". "This" is understood as the defeated enemy that is collapsing.
So if we defeated the first enemy in the database for the first time, the data might look like this
{"1":1}
And if we won another battle against it and the second enemy in the database, it would change to this
{"1":2, "2":1}
We could assign the keys by ._name instead of ._enemyId to make this a little easier to read
{"Goblin": 2, "Hornet":1 }
So our defeated enemy data is stored in Variable23 as this object data.
Like I mentioned above, to retrieve it we need to assign the value of, say, "Goblin" to Variable24
Script: $gameVariables.setValue(24, $gameVariables._data[23].Goblin)
-Or- if we use ._enemyID for our keys
Script: $gameVariables.setValue(24, $gameVariables._data[23].1)
Show Message: "Goblins defeated: \v[24]"
But I must point out again that the script can't work with something that isn't there. Variable23 and the Key:Value need to be defined. When and Where? I can't say. The "if statements" above partially solve this. But that's on the enemy collapse. To prevent errors, you might repeat those lines anywhere you'll report "goblins defeated." So that you can at least report "0" if you've never been in battle or an encounter.
Original thread: https://forums.rpgmakerweb.com/threads/can-someone-help-me-write-a-script.147483/