Playmaker Forum

PlayMaker Help & Tips => PlayMaker Help => Topic started by: pippocao on February 02, 2014, 03:58:45 AM

Title: How can I load a FsmTemplate from the template file at runtime
Post by: pippocao on February 02, 2014, 03:58:45 AM
Hi, everyone, I've encounterd a problem and cannot find the solution in this forum, so I post this thread to request some helps.
I am developing a network game, and I need to load different template files to specify my character behaviours. the key point is that the choosing of  the right template which is decided by server side. For example, the server told client that it need to place a enemy on the scene, and the enyme will attack player automatically, so I will attach a PlayMakerFSM component to the character gameObject and invoke SetTemplate(FsmTemplate template) method of the PlayMakerFSM component. The FsmTemplate is loaded from a prepared playmaker template file names "AutoAttack.asset" . Below is my code:

Code: [Select]
string templateFileName = getBehaviourFromServer();  //the template file name is got from server at runtime.
PlayMakerFSM fsmComponent = gameObject.addComponent<PlayMakerFSM>();
FsmTemplate template = LoadTemplateFromFile(templateFileName);
fsmComponent.SetTemplate(template);

So my question is that how can I implement LoadTemplateFromFile method, is there any API I can use?
Title: Re: How can I load a FsmTemplate from the template file at runtime
Post by: Alex Chouls on February 02, 2014, 10:37:25 AM
Are the templates on the server or in your build?

If you want them on the server you should use AssetBundles:
http://docs.unity3d.com/Documentation/Manual/AssetBundlesIntro.html

If they're in the build, put them is a Resources folder and use Resources.Load:
http://docs.unity3d.com/Documentation/ScriptReference/Resources.Load.html

Either way you should be able to load the resource by name, cast it to an FsmTemplate, then use SetTemplate.
Title: Re: How can I load a FsmTemplate from the template file at runtime
Post by: pippocao on February 02, 2014, 11:20:30 AM
Thanks very much! this will help me