Posted by: sarge945
« on: 12. June 2023, 03:59:29 »Just covering my bases in case someone screws up their game
I'm not sure why you would think that. Several of my mods dynamically create objects at runtime and assign scripts to them. Works perfectly fine, no issues.Adding scripts dynamically at runtime does unspeakable things to the game
//This is both the most amazing AND most disgusting thing I have ever made NewDark do
//I'm really sorry...
function DynamicScriptAdd(item,script)
{
local script1 = Property.Get(item,"Scripts","Script 0");
local script2 = Property.Get(item,"Scripts","Script 1");
local script3 = Property.Get(item,"Scripts","Script 2");
local script4 = Property.Get(item,"Scripts","Script 3");
if (script1 == "" || script1 == 0)
Property.Set(item,"Scripts","Script 0",script);
else if (script2 == "")
Property.Set(item,"Scripts","Script 1",script);
else if (script3 == "")
Property.Set(item,"Scripts","Script 2",script);
else if (script4 == "")
Property.Set(item,"Scripts","Script 3",script);
else
print ("Error: Object " + item + " (" + ShockGame.GetArchetypeName(item) + ") has no available script slots!");
}
DML1
+ObjProp "TargetArchetype" "Scripts"
{
"Script 0" "MyCustomScript"
}
and this will work just fine*.. until someone makes another mod that will overwrite the same script slot on the same archetype. to avoid this situation (especially if you just need to assign an additional custom script to an archetype and nothing else, like change or remove already existing scripts), create a custom metaproperty, assign your script to it instead, and add that metaprop to the target archetype.DML1
CreateArch "Misc Metaprops" "MyCustomMeta"
{
}
+ObjProp "MyCustomMeta" "Scripts"
{
"Script 0" "MyCustomScript"
}
+MetaProp "TargetArchetype" "MyCustomMeta"
this will decrease the chances of your mod having compatibility issues with other mods significantly, so unless there are metaprop limitations that are preventing you from being able to do this (need to apply the "don't inherit" flag, for example), it is highly recommended to assign custom scripts in this manner.+ObjProp "TargetArchetype" "Scripts"
{
"Script 0" "MyCustomScript"
"Script 1" ""
"Script 2" ""
"Script 3" ""
}
or even better, just use the custom metaprop, which does not have this issue (and doing the empty spaces is not necessary if going with the meta).