I am trying to copy the scripts from a form to another, however I am having an issue that the script I want to copy is not appearing, I have the hypothesis that this is because it is an object reference script.
I have a misc object I created in CK, with this script attached:
It prints a form that it takes as a property when you click on the item in your inventory, mostly to see if the properties are being copied correctly
The following code is an attempt of copying the scripts, it is not getting the script I want to copy. It is instead getting only a script I don't want to copy (some kind of base scripts the object have), however I can ignore this script later when I'm getting the actual script
auto* virtualMachine = RE::BSScript::Internal::VirtualMachine::GetSingleton();
if (virtualMachine) {
auto* handlePolicy = virtualMachine->GetObjectHandlePolicy();
auto* bindPolicy = virtualMachine->GetObjectBindPolicy();
if (handlePolicy && bindPolicy) {
auto baseHandler = handlePolicy->GetHandleForObject(baseForm->GetFormType(), baseForm);
auto newHandler = handlePolicy->GetHandleForObject(newForm->GetFormType(), newForm);
print("form", baseForm->GetName());
if (baseHandler != handlePolicy->EmptyHandle() && newHandler != handlePolicy->EmptyHandle()) {
auto baseHandlerScripts = virtualMachine->attachedScripts.find(baseHandler);
if (baseHandlerScripts != virtualMachine->attachedScripts.end()) {
auto* scripts = &(*baseHandlerScripts).second;
auto scriptNumber = scripts->size();
for (uint32_t i = 0; i < scriptNumber; ++i) {
auto script = (*scripts)[i].get();
auto ptr = new RE::BSTSmartPointer(script);
bindPolicy->BindObject(*ptr, newHandler);
print("script", script->type->GetName());
}
}
}
}
}
Both newForm and baseForm are RE::TESObjectMISC
here is the code for the print function, it is mostly for debugging so i did not put much thought into that
I have also tried doing this but no success
auto baseHandler = handlePolicy->GetHandleForObject(RE::FormType::Reference, baseForm);
auto newHandler = handlePolicy->GetHandleForObject(RE::FormType::Reference, newForm);
If I find a solution to this ill post here, but maybe someone is able to help me.