Resource Manager Plugin – Arma Reforger
Jump to navigation
Jump to search
No edit summary |
Lou Montana (talk | contribs) m (Text replacement - "[[OFPEC_tags" to "[[Scripting Tags") |
||
(One intermediate revision by the same user not shown) | |||
Line 8: | Line 8: | ||
* Open [[Arma_Reforger:Script_Editor|Script Editor]] | * Open [[Arma_Reforger:Script_Editor|Script Editor]] | ||
* In an addon, create a new script in {{hl|WorkbenchGame/ResourceManager}} - name it {{hl|[[ | * In an addon, create a new script in {{hl|WorkbenchGame/ResourceManager}} - name it {{hl|[[Scripting Tags|TAG_]]TutorialPlugin.c}} (must end with {{hl|Plugin}} by convention) | ||
* Double-click the file to open it | * Double-click the file to open it | ||
* Press {{Controls|Ctrl|T}} to use the | * Press {{Controls|Ctrl|T}} to use the {{Link|Arma Reforger:Script Editor: Fill From Template Plugin|Script Template plugin}} | ||
** In its window, select "Class Type: WorkbenchPlugin", '''set the parent class to {{Link/Enfusion|armaR|ResourceManagerPlugin}}''' and leave the other fields blank/default | ** In its window, select "Class Type: WorkbenchPlugin", '''set the parent class to {{Link/Enfusion|armaR|ResourceManagerPlugin}}''' and leave the other fields blank/default | ||
** A Workbench plugin skeleton is inserted.<!-- | ** A Workbench plugin skeleton is inserted.<!-- | ||
Line 16: | Line 16: | ||
* In the <enforce inline>Run()</enforce> method, write <enforce inline>Print("It works!");</enforce> and save the file | * In the <enforce inline>Run()</enforce> method, write <enforce inline>Print("It works!");</enforce> and save the file | ||
* Reload Workbench scripts via '''Reload WB Scripts''' option located in ''Plugins→Settings'' menu (default shortcut: {{Controls|Ctrl|Shift|R}}) | * Reload Workbench scripts via '''Reload WB Scripts''' option located in ''Plugins→Settings'' menu (default shortcut: {{Controls|Ctrl|Shift|R}}) | ||
* The {{hl|[[ | * The {{hl|[[Scripting Tags|TAG_]]TutorialPlugin}} plugin should appear in the Resource Manager's Plugins list, available in the top bar - click on the plugin entry | ||
* "It works!" gets printed in the output console. | * "It works!" gets printed in the output console. | ||
Latest revision as of 12:01, 2 October 2024
This tutorial teaches how to create a Resource Manager-specific plugin.
Setup
- Open Script Editor
- In an addon, create a new script in WorkbenchGame
/ResourceManager - name it TAG_TutorialPlugin.c (must end with Plugin by convention) - Double-click the file to open it
- Press Ctrl + T to use the Script Template plugin
- In its window, select "Class Type: WorkbenchPlugin", set the parent class to ResourceManagerPlugin and leave the other fields blank/default
- A Workbench plugin skeleton is inserted.
- In the Run() method, write Print("It works!"); and save the file
- Reload Workbench scripts via Reload WB Scripts option located in Plugins→Settings menu (default shortcut: Ctrl + ⇧ Shift + R)
- The TAG_TutorialPlugin plugin should appear in the Resource Manager's Plugins list, available in the top bar - click on the plugin entry
- "It works!" gets printed in the output console.
Contextual Menu Option
The Resource Browser's Contextual Menu can provide a Plugin option allowing to run code on the selected resource(s) of the defined type(s).
In the WorkbenchPluginAttribute, the resourceTypes parameter must be filled, e.g resourceTypes: { "et", "c" }.
The OnResourceContextMenu method must be overridden to work with said resources.
[WorkbenchPluginAttribute(name: "Tutorial Plugin", wbModules: { "ResourceManager" }, resourceTypes: { "et", "c" })]
class TAG_TutorialPlugin : ResourceManagerPlugin
{
//------------------------------------------------------------------------------------------------
override void OnResourceContextMenu(notnull array<ResourceName> resources)
{
Print("Resource context menu action has been called! Here are the selected resources:");
foreach (ResourceName resource : resources)
{
if (resource.EndsWith("c"))
Print("- Script File: " + resource);
else
Print("- Prefab File: " + resource);
}
}
}
ResourceManager Module API
The Resource Manager API allows to register resources, rebuild them, get the selected ones or get their meta file content. In the below code:
- the GetResourceBrowserSelection method is used to temporarily store Resource Browser-selected resources in m_aResourceNames and their file path in m_aFilePaths
- the GetMetaFile method is used to get the current resource's .meta file as a MetaFile instance.
[WorkbenchPluginAttribute(name: "Tutorial Plugin", wbModules: { "ResourceManager" }, awesomeFontCode: 0xF188)]
class TAG_TutorialPlugin : ResourceManagerPlugin
{
protected ref array<ResourceName> m_aResourceNames;
protected ref array<string> m_aFilePaths;
//------------------------------------------------------------------------------------------------
override void Run()
{
ResourceManager resourceManager = Workbench.GetModule(ResourceManager);
m_aResourceNames = {};
m_aFilePaths = {};
resourceManager.GetResourceBrowserSelection(WorkbenchSearchResourcesCallbackMethod, false); // non-recursive search
if (m_aFilePaths.IsEmpty())
{
Print("No Resources selected in Resource Browser");
return;
}
MetaFile metaFile;
foreach (int i, ResourceName filePath : m_aFilePaths)
{
metaFile = resourceManager.GetMetaFile(filePath);
if (!metaFile) // e.g directory
continue;
PrintFormat(
"#%1: %3 (dir %2)",
i,
metaFile.GetSourceFilePath(), // returns the directory tree, e.g $MyMod:Path/To/
metaFile.GetResourceID()); // returns the file's ResourceName, equal to 'resName' below
}
}
//------------------------------------------------------------------------------------------------
// \param[in] resName
// \param[in] filePath is in format $MyMod:Path/To/File.ext
protected void WorkbenchSearchResourcesCallbackMethod(ResourceName resName, string filePath = "")
{
m_aResourceNames.Insert(resName);
m_aFilePaths.Insert(filePath);
}
}