ADD behaviours at runtime? -HELP

megagewinnspiel

  • Posts: 396
Hey guys,

I pretty much need to add a behaviour to an actor at runtime but this doesn't work:
Code: [Select]
actor.addBehavior(com.stencyl.behavior.FollowanotherActor); it gives out this error:
Code: [Select]
Map_Werte: Unknown identifier : com
from scripts.Design_1181_1181_MapWerte
line: 148
columns: 23-26
What exactly am I doing wrong?

mdotedot

  • Posts: 1654
Hello MegaGewinnSpiel,

Since I was looking for a similar effect I investigated the generated code from Stencyl and tried to do a runtime or dynamic load and attach of a behavior.
Previously I used a selfmade extension but I always thought that there should/could be a better approach.
The thing I tested is fairly simple so I could have overlooked some things.

 The behavior you need to add with the Actor.hx addBehavior code needs to be of type Behavior.
The Behavior new initialisation code needs several things to be able to run. I'm not too sure about the input values I selected, but my test worked.

 Actor -Behavior: MyTest


In behaviors.xml  ( Location: %AppData%\Stencyl\stencylworks\games-generated\YourGame\Assets\data )


snippet attachedevent="false" class="scripts.Design_6_6_MyTest" classname="scripts.Design_6_6_MyTest" desc="" description="" design="true" drawOrder="0" id="6" name="MyTest" oid="-1" package="scripts" scenespecific="false" shared="false" type="actor">


So I take name: MyTest, Class scripts.Design_6_6_MyTest and type "actor" from the xml reference.

In the example I created an actor using the [create actor at x: y: [front] ]


You don't need to investigate your generated behavior.xml file. This code will list all the behaviors of the game with the classname and the ID.

Code: [Select]
for(key in com.stencyl.Data.get().behaviors.keys()){
trace("Behavior Name: "+com.stencyl.Data.get().behaviors[key].name);
trace("Behavior ID: "+com.stencyl.Data.get().behaviors[key].ID);
trace("Behavior ClassName: "+com.stencyl.Data.get().behaviors[key].classname);
trace("===");
}

This is a hardcoded example, but you could use the code above to get the classname from the array.
Code: [Select]
var abcd:com.stencyl.behavior.Behavior=new com.stencyl.behavior.Behavior(getLastCreatedActor(), Engine.engine,6,"MyTest","scripts.Design_6_6_MyTest",true,true,new Map<String,com.stencyl.behavior.Attribute>(),"actor",true);
abcd.initScript();
getLastCreatedActor().addBehavior(abcd);

Again, I'm not sure what variables can be ignored/changed since I am doing pretty much everything with trial and error.
The attributes is something I don't need but that could be a challenge to get right as well!

Hope it helps you.

Best regards from
M.E.

« Last Edit: September 18, 2015, 09:54:33 am by mdotedot »
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.

Hectate

  • *
  • Posts: 4643
Wouldn't it be easier to have a behavior attached already but wrap it in a Boolean so you can turn it on or off with a simple true/false change?
:
:
Patience is a Virtue,
But Haste is my Life.
Proud member of the League of Idiotic Stencylers; doing things in Stencyl that probably shouldn't be done.

Natrium

  • *
  • Posts: 111
Or you could just attach them like Hectate suggested, but instead of wrapping them in a Boolean, use the activate/deactivate behavior block (you deactivate them in the When Created Event, and activate them when needed).

yoplalala

  • *
  • Posts: 1632
Or you could put all the code in a dummy actor, which you creat when you need it.

mdotedot

  • Posts: 1654
All true!

I like to have options.

In my case I have currently about 30 different behaviors and that number probably will be growing.
For instance an actor will be able to be pushed and another can follow the player around.
All levels will be dynamic and can be changed on-line so the actors will be created dynamically as well.
I have only two actor-types in Stencyl : Player and Object. The Object actor gets animaton and collision shapes dynamically as well.

It could be that the addBehavior implementation doesn't really deliver what I'm after and that I'm better off with an Animation Manager in combination with a property-system which I made before as well.

In either case the original poster has a lot of options to choose from now !

Best regards from
M.E.

« Last Edit: September 17, 2015, 11:01:46 pm by mdotedot »
Proud member of the League of Idiotic Stencylers! Doing things in Stencyl that probably shouldn't be done.