playMaker

Author Topic: [SOLVED] How to run script as part of a loop (Hashtable GetNext) ?  (Read 1792 times)

memetic arts

  • Full Member
  • ***
  • Posts: 141
Greetings -

I've been successfully setting an FSM global var, calling a C# script (via "enable") and passing the var to the script.

I now need to do this by looping through a set of Hashtable values, and thought that disabling the script after running it, connecting back to "GetNext", and then re-enabling would work . . . but it doesn't . . . it just processes the first run.

The FSM global var is successfully being reset, but is not getting passed to/processed by the script. I've tried inserting wait states / sending events from the script to the FSM, which hasn't worked.

There also seems to be a problem with even getting the Debug.Log action to work - the only notifications in the Log are the ones inserted by the script on the first run.

Any ideas?  Thanks in advance. 

Screenshot of the basic FSM is attached, code from the script inserted below.

Code: [Select]
using UnityEngine;
using System.Collections;
using HutongGames.PlayMaker;

public class LoadAB : MonoBehaviour {
private string assetbundleName;
private string assetName ;

       // locSym is the variable that is being set by the FSM.
       // It is a 3-letter value pulled from the hashtable
       // that is concatenated with other strings to form various asset names.

       private string locSym;

       IEnumerator Start () {

// This is simply to get the elapsed time for this phase of AssetLoading.
float startTime = Time.realtimeSinceStartup;
// Start a download of the given URL
locSym = FsmVariables.GlobalVariables.GetFsmString("curBundle").Value;
assetbundleName = "loc-" + locSym;
assetName = locSym + "_list";
Debug.Log("got bundle name from FSM: " + assetbundleName + "; assetName is: " + assetName);
var urlString = "http://www.mywebaddress.com/pathToAssetBundles/iOS/" + assetbundleName;

WWW www = WWW.LoadFromCacheOrDownload (urlString, 1);
                             
// Wait for download to complete
yield return www;

// Load and retrieve the AssetBundle
AssetBundle bundle = www.assetBundle;

// Load the object asynchronously
AssetBundleRequest request = bundle.LoadAssetAsync (assetName, typeof(Texture2D));

// Wait for completion
yield return request;

// Get the reference to the loaded object
Texture2D tex = request.asset as Texture2D;

if (tex != null)
Texture2D.Instantiate(tex);

// Calculate and display the elapsed time.
float elapsedTime = Time.realtimeSinceStartup - startTime;
Debug.Log(assetName + (tex == null ? " was not" : " was")+ " loaded successfully in " + elapsedTime + " seconds" );

                // we had been using this event to insure the image was loaded
                // before proceeding with the loop, but does not seem to be effective

                //PlayMakerFSM.BroadcastEvent("imgLoaded");


// Unload the AssetBundles compressed contents to conserve memory
bundle.Unload(false);

// Frees the memory from the web stream
www.Dispose();
}
}
« Last Edit: January 27, 2016, 01:18:55 AM by memetic arts »

memetic arts

  • Full Member
  • ***
  • Posts: 141
[SOLVED] How to run script as part of a loop (Hashtable GetNext) ?
« Reply #1 on: January 27, 2016, 01:18:28 AM »
Finally figured this out.

Since, admitedly, the script I'm using was based on that provided in Unity's documentation, I took it for granted that I didn't *have* to use "Start()" as the name of the function. By using Start, though, you are limited to enable/disable as the mechanism for running or not running, and clearly that wasn't working in the loop scenario.

By changing the name of the function, I was then able to call it directly via the StartCoroutine action, and voila~, works like a charm.