Multiple behaviour instances

sissbruecker

  • Posts: 3
Hi,

is there a way to create multiple instances of the same behaviour?

I have created a behaviour that allows me to customize the anchors for a stick joint.

Now I would like to add an instance of the behaviour for several joints within a level.

Alexin

  • *
  • Posts: 3127
No. You have to create multiple behaviors.
"Find the fun"
alexin@stencyl.com

captaincomic

  • *
  • Posts: 6108
I too would like to add a behavior more than once to an actor sometimes.

For example I have have a "Do on Key Press"-behavior that sends a message using the "tell self to _ for all behaviors" when a key is pressed.
Let's say my spaceship fires a bullet when A is pressed and activates the shield with S is pressed. I would make sense to have the "Do on Key Press" attached twice to the spaceship, once for A, and once for S.

So whenever you have a behavior that can be modified with attributes, it can make sense to have two instances of it for the same actor.

coleislazy

  • *
  • Posts: 2607
Remember that everything is coded in AS3. If you were coding by hand, you wouldn't be able to have duplicate functions in a class, either.

You can always duplicate the behavior and name it something different, even if the contents are exactly the same.

sissbruecker

  • Posts: 3
@captaincomic:

Agreed. It would make it easier to build generic code pieces that can be reused. I was about to create a ragdoll by writing a custom revolute joint behavior and then connecting the different actors with mutliple instances of the behavior. Since this didn't work I ended up with a more specific behavior that creates all joints at once - which is only useful if you need a ragdoll.

I'd guess the current limitation is self-imposed to eliminate possible errors when creating a game. Also forcing specific behaviors makes it easier for beginners, as these work "out-of-the-box". Code-wise I don't see why you could't create multiple instances of the same script with different parameters.

@coleislazy

Each behavior is implemented as a class. Adding a behavior to an actor / a scene is merely creating an instance of that class.

coleislazy

  • *
  • Posts: 2607
Each behavior is implemented as a class. Adding a behavior to an actor / a scene is merely creating an instance of that class.

Yes, but think of attaching a behavior to an actor/scene in SW as declaring a variable (the name of the behavior) and creating an instance of the class (the behavior). If you want more than one, as I said before, you can duplicate it and just rename it.

You could also write a free-form class and use code to create instances of it, as you stated above. All blocks in SW have corresponding AS3 functions (in fact, all blocks are just "on top" of AS3 code).

ShivaFang

  • Posts: 248
This would react in so many bad ways with things like tell (behaviour name) to (text) and get (attribute) from (behaviour name).

I experienced similar frustration when I found out I couldn't do this, but my brain always immediatly goes and tries to figure out 'why' it is that way (which is why I tend to spot bugs people miss).  If it's not obviously a bug, or there's not a very good reason (as in this case) then I ask for it to be changed, but in this case there's a major reason.
Justin "ShivaFang" White
Aquamentos Games - The origin of challenging Strategy and Role-Playing Flash gaming!
Visit our Developer Blog and Google+ Page!

captaincomic

  • *
  • Posts: 6108
Duplicating and renaming is what I did as a workaround, but it really is not optimal. When you change one behavior a little bit you have to change the other as well everytime.

So attaching a behavior to an actor is like creating a variable with the same name as the behavior in the actor class? So let's say behavior B is a class called Design_300_300. When you attach it to actor A, actor A declares a varible of type Design_300_300 named B?

Wouldn't it be possible to just declase a second variable of type Design_300_300 named B_2 or something?

ShivaFang

  • Posts: 248
I get what you are saying.  I have the same problem with adding behaviours to actors I've already put on a scene.  I can add them to the base, but they don't auto-add to any other's Ive already placed, which is annoying if I want to make a change to all versions of (say) a button type.
Justin "ShivaFang" White
Aquamentos Games - The origin of challenging Strategy and Role-Playing Flash gaming!
Visit our Developer Blog and Google+ Page!

captaincomic

  • *
  • Posts: 6108
This would react in so many bad ways with things like tell (behaviour name) to (text) and get (attribute) from (behaviour name).

Hm. That's true. The "tell"-block wouldn't be so much of a problem, it could just send the message (call the function) to each instance of the behavior.
But "get attribute from" would really be a big problem.

sissbruecker

  • Posts: 3
Each behavior is implemented as a class. Adding a behavior to an actor / a scene is merely creating an instance of that class.

Yes, but think of attaching a behavior to an actor/scene in SW as declaring a variable (the name of the behavior) and creating an instance of the class (the behavior). If you want more than one, as I said before, you can duplicate it and just rename it.

You could also write a free-form class and use code to create instances of it, as you stated above. All blocks in SW have corresponding AS3 functions (in fact, all blocks are just "on top" of AS3 code).

SW would need to create distinct variable names then, no big issue. Though I have to admit I'm just getting started with SW so there may be issues with other features, like ShivaFang mentioned.

Your suggestion to put the generic code into a free-form class is a good workaround though, this way changes to the code only need to be done once. Thanks for the input!