playMaker

Author Topic: [SOLVED] Last Item Skipped When Setting NGUI Label Text via Loop  (Read 3979 times)

memetic arts

  • Full Member
  • ***
  • Posts: 141
[SOLVED] Last Item Skipped When Setting NGUI Label Text via Loop
« on: November 11, 2015, 03:39:51 AM »
I think I may have uncovered a bug, here's the issue (and please refer to the attached image, relevant items are outlined in yellow or red).

I'm running through a GetNext loop on a hashtable, which adds children to an NGUI Scrollable Panel. The items that appear are determined by clicking on a menu item (not shown), in this case an item called "Central Park". That's working.

The next stop during the loop is to set the relevant text labels for each item just after the item is added. That is working for the most part, EXCEPT for the last item. If you look at the attached screenshot, you can see that the labels are set -- in this case the "Track Name" label --, except for the one at the top of the list (which is the last one created) which appears incorrect. Not only does it conflict with the what is sent to the Console *after* the label was to have been written, but also with what is displayed in the Action Inspector (hard to see because the scene was running).

The even more confounding thing is that the incorrectly displayed text is actually the text for the first item in the list from the previously selected menu option. This value, whatever it happened to be,  is included EVEN AFTER STOPPING THE SCENE AND RESTARTING, e.g. it appears to be retained in memory somehow, even though it is dynamically generated.

I've tried separating actions out into discrete states (originally they were all in one state), but that hasn't helped.  And again, it seems that the data and flow are correct, because the console is showing the label text as expected -- it's just not getting to the last label in the list.

ALSO NOTE: You may notice that the Keys in the Hashtable are numbered from 1-10 rather than from 0-9.  That was a debugging step on my part to see if indexing from 1 rather than 0 made a difference -- so that's not the issue!  Same result if you go from 0 to 9 as with proper indexing.  :-)

As always, any/all input is very much appreciated!

Thanks!

Richard

« Last Edit: November 14, 2015, 09:41:16 AM by memetic arts »

memetic arts

  • Full Member
  • ***
  • Posts: 141
Re: Last Item Skipped When Setting NGUI Label Text via Loop
« Reply #1 on: November 11, 2015, 10:29:22 PM »
Kind of sad replying to my own post, I know, but here's a bit of an update, or at least something that I just noticed, which also reveals a detail that I apparently left out (sorry) . . . though maybe it was implied.

As I'm cycling through the GetNext loop in the Hashtable, the children being added to the list are instantiations of prefabs. And this seems to be working, for the most part -- the game object instances are appearing as expected. 

But what seems to be happening with the mysterious last item is that it is writing *directly to the prefab itself*, not the instantiation.  And then it just sits there in the prefab after you stop the gameplay, as if you had typed the value in manually, e.g. it becomes the "default" value, which is then (apparently) applied the next time the list generator is called.

So now the question is: in the "NGUI Tools Add Grid Child" Action, should I be using the "child instance" parameter, and if so, what goes in there? For "Child Reference" I've selected the Prefab that is being instantiated . . . is that the right thing to do?

Thanks.



« Last Edit: November 11, 2015, 10:48:09 PM by memetic arts »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Last Item Skipped When Setting NGUI Label Text via Loop
« Reply #2 on: November 12, 2015, 01:48:47 AM »
Hi,

 You need to create an instance of the prefab before adding that instance as a child a Gameobject.
 
Have you tried that?

this action doesn't instanciate a prefab, it expects you give it a valid object from the hierarchy.


 Bye,

 Jean

memetic arts

  • Full Member
  • ***
  • Posts: 141
Re: Last Item Skipped When Setting NGUI Label Text via Loop
« Reply #3 on: November 12, 2015, 04:42:43 PM »
Hi Jean, thanks for the input.

No, I didn't do that . . . and only because I'd been using a script to hold arrays and generate the list before now, where I'm trying to make an honest go of using ArrayMaker and no scripts. And it seems that the version with the script works without fail, while my ArrayMaker construct is failing on the last item.

Is there an InstantiatePrefab action somewhere? I know in Unity, there is an Instantiate method . . . is that what should be used, or is it simply a matter of declaring a game object (variable), and then adding the children to it, as in the script below:

Code: [Select]

using UnityEngine;
using System.Collections;


public class addListItem : MonoBehaviour
{
private string[] locName = new string[10]{...};
private string[] locImg = new string[10]{...};
private string[] locDesc = new string[10]{...};

public PlayMakerFSM pmFSM;
public GameObject listItem; // this is the name of the preFab
public GameObject gridRef; // this is the name of the scrollable grid

void Start () {
pmFSM = GameObject.Find("_SceneMgr").GetComponent<PlayMakerFSM>(); // initiate the FSM reference so we can talk to it later
Invoke("genList", 1);
}
   
void genList() {
for(int i=0;i<10;i++){
GameObject go = NGUITools.AddChild(gridRef, listItem);
go.transform.FindChild("locID").GetComponent<UILabel>().text = i.ToString();
go.transform.FindChild("locName").GetComponent<UILabel>().text = locName[i];
go.transform.FindChild("locDesc").GetComponent<UILabel>().text = locDesc[i];
UISprite art = go.transform.FindChild("locImg").GetComponent<UISprite>();
art.spriteName = locImg[i];
art.depth = 115;
gridRef.GetComponent<UIGrid>().repositionNow = true;
}
    pmFSM.Fsm.Event("listGenerated"); // tell the FSM that the list has been created
   }
}


Thanks again!!

memetic arts

  • Full Member
  • ***
  • Posts: 141
Re: Last Item Skipped When Setting NGUI Label Text via Loop
« Reply #4 on: November 14, 2015, 09:40:58 AM »
Remember the monkey that kept typing, and eventually he wrote a Shakespeare Sonnet?  That would be me.  Finally cracked this, though I give full credit to the law of odds.

There were three things that I hadn't done in the above scenario . . . two kind-of-obvious, and one not-so-obvious.

First the not-so-obvious:

My goal was to dynamically create a list of menu items via a single prefab menu item, as part of an NGUI scrollable panel. Jean had suggested that I needed to instantiate the prefab before I could use the NGUI Tools Add Grid Child action, which was probably correct . . . but what I didn't realize is that this could be handled right from within the action.  There is an option for a thing called "Child Instance", which I had left unused. MISTAKE!  Because this is the "hook" into the currently iterated instance of the prefab (this was all happening via a GetNext loop of Hashtable entries). So what must have been happening is the "identity" of the prefab was getting lost somehow, without a direct reference (e.g. the instantiation itself, I guess) to the object.

Jean, I'm not so sure that this is what you meant by instantiation, but I did find this thread over on the Tasheran site, where Aron Mook actually says that using the NGUI Add Child method is the way to go . . .

http://www.tasharen.com/forum/index.php?topic=11208.0

The other two missing pieces were pretty stupid on my part . . . having added the Child to the list, I forgot that I had to then FindChild, and get the Label component that belonged to it, so that I could then set the Label's text property (via a global variable set to Unity.object>GUI Label).

Anyway, that took care of it, and it only took me FOUR DAYS to figure it out.  Sheesh.  But onward and upward from here.

As a side point, I'll also say that there isn't a whole lot of reference material out there on dynamically instantiating prefabs in PM, and while I'm still not sure if this is *the* way, it certainly is *a* way, and I'm happy to share more detail if anyone wants it.

Sleeping much better now . . .

Cheers

memetic arts

  • Full Member
  • ***
  • Posts: 141
Re: [SOLVED] Last Item Skipped When Setting NGUI Label Text via Loop
« Reply #5 on: November 14, 2015, 09:44:50 AM »
Sorry, one more thing . . .

The other thing about this issue that was confounding was that the routine was largely working -- the only point of failure was that the last item was getting lost. It would have been MUCH easier to solve, I think, had the whole thing just failed entirely, e.g. had NONE of the items been able to render properly due to lack of an adequate reference point.

Not faulting any programming here . . . just not sure how or why partial success of an incorrect structure would be permitted . . .

Anyway, as I said, onward and upward . . .


jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: [SOLVED] Last Item Skipped When Setting NGUI Label Text via Loop
« Reply #6 on: November 16, 2015, 07:05:46 AM »
Hi,

 indeed you must be instantiating first and then you can add this instance as a child of a GameObject in the Hierarchy.

http://docs.unity3d.com/Manual/InstantiatingPrefabs.html

Let me know if that helps. Also, have you checked the ngui sample, I provided a scene where it fills up a list with prefab instances.

the scene is called "Example 9 PlayMaker - Quest Log"


Bye,

 Jean

memetic arts

  • Full Member
  • ***
  • Posts: 141
Re: [SOLVED] Last Item Skipped When Setting NGUI Label Text via Loop
« Reply #7 on: November 16, 2015, 11:33:52 AM »
Thanks, Jean. I understand the Unity docs -- it's just that AronMook has been pretty adamant about using the NGUI Add Child explicitly -- he actually says it's more efficient that Unity's Instantiate method.

I started looking through the Quest Log example (not sure how I missed it before!), and while it runs, I am getting two errors on it, not sure how to resolve.  I'm guessing I need to drag "Table" in as the parent, and the "Quest Table Row" prefab in as the child, but would appreciate if you could confirm. Here's a screenshot below. 

Thanks again for your help on this!!




memetic arts

  • Full Member
  • ***
  • Posts: 141
Re: [SOLVED] Last Item Skipped When Setting NGUI Label Text via Loop
« Reply #8 on: November 16, 2015, 11:53:37 AM »
Hello again -

So went ahead and ran the Quest Log with the errors, clicked "Build", and it seems to work . . .which doesn't make sense to me.  Without defining the parent and the child prefab to add (in the AddChild action), how is it possible that items get added?

(see attached)

Thanks!