6647ec182d422

6647ec182e10c
1 Guest is here.
 

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

6647ec182e66fvoodoo47

6647ec182e6f4
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

6647ec182ea79sarge945

6647ec182eaff
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!");
}

6647ec182ef0bZylonBane

6647ec182ef70
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.

6647ec182f00asarge945

6647ec182f063
Just covering my bases in case someone screws up their game

Your name:
This box must be left blank:

Look at you, hacker: a ____ creature of meat and bone!  (Fill in the missing word):
1 Guest is here.
Hey, I think its eyes have rott... FUCK IT'S STILL ALIVE!
Contact SMF 2.0.19 | SMF © 2016, Simple Machines | Terms and Policies
FEEP
6647ec182f1ae