6646d9e6cc6cb

6646d9e6ccb07
1 Guest is here.
 

Topic: How to assign custom scripts to archetypes Read 26000 times  

6646d9e6cd151voodoo47

6646d9e6cd1bf
if you are creating a mod, chances are you will find yourself in a situation where you need to assign your custom script to an archetype. this can be done by simply adding the prop to the target archetype;
Code: [Select]
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.
Code: [Select]
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.
Acknowledged by 3 members: Nameless Voice, RoSoDude, sarge945

6646d9e6cd4f7sarge945

6646d9e6cd563
An additional note:

If for some reason you need to add a script to a bunch of objects which do have the don't inherit flag set, and want to be as compatible as possible, this squirrel function will add the specified script to the first available script slot on a given object. When a script is added at runtime like this, it's OnBeginScript function will run the moment it's added, and it will behave like normal in all other regards. DO NOT USE THIS UNLESS ABSOLUTELY NECESSARY. Adding scripts dynamically at runtime does unspeakable things to the game and I wouldn't recommend it unless you really have no other choice.

I use this in my deterioration mod to add the deterioration script to objects which can't use the metaproperty, so that all items in the game (including from other mods) are all supported automatically regardless of the flag.

Code: [Select]
//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!");
}

6646d9e6cd96fZylonBane

6646d9e6cd9c4
Adding scripts dynamically at runtime does unspeakable things to the 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.

6646d9e6cda65sarge945

6646d9e6cdab6
Just covering my bases in case someone screws up their game

Your name:
This box must be left blank:

In which year was System Shock 2 released:
1 Guest is here.
It's so much fun playing hide and seek with girls.
Contact SMF 2.0.19 | SMF © 2016, Simple Machines | Terms and Policies
FEEP
6646d9e6cdc53