If you are using sprites puppetted into empy channels then by default the scriptInstanceList is empty. The scriptInstanceList is the same as the list of behaviours that you see in the behaviour inspector. You can add a behaviour at runtime like so:
behaviour = (script "my behaviour").new( spriteNum ) -- (1)
sprite( spriteNum ).scriptInstanceList.Add( behaviour ) -- (2)
(1) The first line assumes that the behaviour has a new handler that can accept the sprite channel as a parameter. Since dynamically generated sprites do not get assigned a spriteNum value automatically, and the beginSprite and endSprite handlers are never called, you may need to do some of this manually. An example dual-pupose new handler is this:
property spriteNum, MySprite
on new me, sp
if( not VoidP( sp ) ) then -- This won't happen if the behaviour is attached in the score
spriteNum = sp -- Set the sprite number
end if
MySprite = sprite( spriteNum ) -- Just an example, this is quite common
-- Other initialisation code...
-- Call beginSprite if necessary
if( not VoidP( sp ) ) then call( #beginSprite, [me] )
return me -- Essential for the behaviour to work as an object at runtime
end
Note that if you put a new handler in a behaviour script then there is no difference that I am aware of between a behaviour script and a parent script, except that parent scripts do not appear in the behaviour inspector's drop-down list.
(2) You have to be careful with the second line, because if when you add to the scriptInstanceList for a puppet, the behaviour stays there forever. This is why my "destructor" code always sets the list to []. Even if you disable the sprite, having behaviours attached can have unexpected results elsewhere in the movie. The same goes for the cursor property.
What is often safer, but you have to be careful of more than one part of your movie assigns behaviours to the same sprite, is to use this format for line (2):
sprite( spriteNum ).scriptInstanceList = [ behaviour ] -- (2[b])
There is no way that this can add infinite numbers of the same behaviour (which can cause some really strange effects and is very easy to do by mistake). However, it will remove any other behaviours that have been attached to the sprite.
IMPORTANT NOTE: Changing scriptInstanceList affects the score! Behaviours attached in the score will appear in the list, so if you modify the list for a sprite in the score it can have very unwanted effects. Be very careful about this, because the results can be hard to predict. I normally use a special behaviour that stores a duplicate of the scriptInstanceList, and has a handler to return it to default condition, rather than simply erasing the list with [].