6649a0c6d1a7b

6649a0c6d1f27
1 Guest is here.
 

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

6649a0c6d23f6voodoo47

6649a0c6d2458
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

6649a0c6d2775sarge945

6649a0c6d27d4
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!");
}

6649a0c6d2bc5ZylonBane

6649a0c6d2c19
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.

6649a0c6d2cb2sarge945

6649a0c6d2cff
Just covering my bases in case someone screws up their game

Your name:
This box must be left blank:

Replicator restrictions are in place for the good of whom?:
1 Guest is here.
“You are the type of thing demons are afraid of.”
Contact SMF 2.0.19 | SMF © 2016, Simple Machines | Terms and Policies
FEEP
6649a0c6d2e03