6742462f25714

6742462f271fb
1 Guest is here.
 

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

6742462f27762voodoo47

6742462f277d1
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.


*one needs to be aware of a dml load quirk here - if the parent archetype of your TargetArchetype already has a scripts prop, and your TargetArchetype does not, then whichever script slot is occupied on the parent archetype but free on your TargetArchetype will get its script duplicated when the game loads the dml. meaning, if the parent archetype has vanillascript0 in slot0, and vanillascript1 in slot1, and you do a +ObjProp "Scripts" on your TargetArchetype just assigning MyCustomScript to slot0, then you will get duplication on vanillascript1 upon game load. to prevent this, either assign empty spaces to the slots that are supposed to stay clear;
Code: [Select]
+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).
« Last Edit: 04. October 2024, 07:31:22 by voodoo47 »
Acknowledged by 3 members: Nameless Voice, RoSoDude, sarge945

6742462f27ad2sarge945

6742462f27b2d
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!");
}

6742462f27fbfZylonBane

6742462f28012
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.

6742462f28105sarge945

6742462f28156
Just covering my bases in case someone screws up their game

Your name:
This box must be left blank:

How can you challenge a perfect, _____ machine? (Fill in the missing word):
1 Guest is here.
Welcome to the Jungle.... We got fun and games
Contact SMF 2.0.19 | SMF © 2016, Simple Machines | Terms and Policies
FEEP
6742462f295fa