Skip to content

Commit 8bc649c

Browse files
committed
New Command 2054 - Get ID By Name
Through it you can store the ID of a database asset through by searching for its name. Syntax (Maniacs): ```js @raw 2054, "", // name to search 0, 11, // type of database Asset 0, 5, // variable where it will be stored 0, 0, // Min Range 0, 0, // maxRange 1, 1 // use stringVar `` New Command 2054 - Get ID By Name Through it you can store the ID of a database asset through by searching for its name. Syntax (Maniacs): ```js @raw 2054, "", // name to search 0, 11, // type of database Asset 0, 5, // variable where it will be stored 0, 0, // Min Range 0, 0, // maxRange 1, 1 // use stringVar ``
1 parent b313163 commit 8bc649c

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/game_interpreter.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#include "baseui.h"
6969
#include "algo.h"
7070
#include "rand.h"
71+
#include <variant>
7172

7273
enum BranchSubcommand {
7374
eOptionBranchElse = 1
@@ -826,6 +827,8 @@ bool Game_Interpreter::ExecuteCommand(lcf::rpg::EventCommand const& com) {
826827
return CommandManiacControlStrings(com);
827828
case Cmd::Maniac_CallCommand:
828829
return CommandManiacCallCommand(com);
830+
case static_cast <Game_Interpreter::Cmd>(2054) : // Easyrpg_GetIdByName
831+
return CommandGetIdByName(com);
829832
default:
830833
return true;
831834
}
@@ -5107,6 +5110,49 @@ bool Game_Interpreter::CommandManiacCallCommand(lcf::rpg::EventCommand const& co
51075110

51085111
return true;
51095112
}
5113+
bool Game_Interpreter::CommandGetVarByName(lcf::rpg::EventCommand const& com) {
5114+
auto& targetData = lcf::Data::variables;
5115+
5116+
int stringVarParam = 6; // string var is detected at com.parameters[n]
5117+
const std::string targetName = ToString(CommandStringOrVariable(com, stringVarParam, stringVarParam + 1));
5118+
if (targetName.empty()) {
5119+
Output::Warning("The variable name to search is empty.");
5120+
return true;
5121+
}
5122+
5123+
const int outputVar = ValueOrVariable(com.parameters[0], com.parameters[1]);
5124+
if (outputVar <= 0) {
5125+
Output::Warning("This command requires a valid variable.");
5126+
return true;
5127+
}
5128+
5129+
int rangeMin = ValueOrVariable(com.parameters[2], com.parameters[3]);
5130+
int rangeMax = ValueOrVariable(com.parameters[4], com.parameters[5]);
5131+
5132+
if (rangeMin <= 0) rangeMin = 1;
5133+
if (rangeMin > static_cast<int>(targetData.size())) rangeMin = static_cast<int>(targetData.size());
5134+
if (rangeMax <= 0 || rangeMax >= static_cast<int>(targetData.size())) rangeMax = static_cast<int>(targetData.size());
5135+
5136+
if (rangeMax < rangeMin) std::swap(rangeMin, rangeMax);
5137+
5138+
auto it = std::find_if(targetData.begin() + rangeMin - 1, targetData.begin() + rangeMax,
5139+
[&](const auto& entry) { return entry.name == targetName; });
5140+
5141+
if (it != targetData.end()) {
5142+
const int id = std::distance(targetData.begin(), it) + 1;
5143+
5144+
if (id >= rangeMin && id <= rangeMax) {
5145+
Output::Debug(" Variable \"{}\" found at ID: {}. Storing ID in variable {}.", targetName, id, outputVar);
5146+
5147+
Main_Data::game_variables->Set(outputVar, id);
5148+
Game_Map::SetNeedRefresh(true);
5149+
return true;
5150+
}
5151+
}
5152+
5153+
Output::Warning("Variable \"{}\" not found within the range of variables {} to {}.", targetName, rangeMin, rangeMax);
5154+
return true;
5155+
}
51105156

51115157
Game_Interpreter& Game_Interpreter::GetForegroundInterpreter() {
51125158
return Game_Battle::IsBattleRunning()

src/game_interpreter.h

+2
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,8 @@ class Game_Interpreter
290290
bool CommandManiacControlStrings(lcf::rpg::EventCommand const& com);
291291
bool CommandManiacCallCommand(lcf::rpg::EventCommand const& com);
292292

293+
bool CommandGetVarByName(lcf::rpg::EventCommand const& com);
294+
293295
int DecodeInt(lcf::DBArray<int32_t>::const_iterator& it);
294296
const std::string DecodeString(lcf::DBArray<int32_t>::const_iterator& it);
295297
lcf::rpg::MoveCommand DecodeMove(lcf::DBArray<int32_t>::const_iterator& it);

0 commit comments

Comments
 (0)