playMaker

Author Topic: instantiate prefabs with different values  (Read 6725 times)

elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
instantiate prefabs with different values
« on: June 07, 2016, 05:44:50 PM »
Hi there!

I have a background and a text on it as a prefab. I also have different text values in xml file. I need to load xml file, and then instantiate same number of prefabs with each of those values in as text in specific places. Once clicked on, they will jump to text area above and line up in a sentence.

Just like in the duolingo app, I posted image below.



Any help appreciated, thanks!!

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: instantiate prefabs with different values
« Reply #1 on: June 07, 2016, 08:05:09 PM »
Hi,
To have different values you mean the text?

if so, you can make a fsm on the prefab and some string variables.
Then when you create the prefab, store it in variable, then get your xml value and use "set fsm string" to set the variables, when that's done you can use 'Send event by name' to send a global event to the prefab.

then in the fsm on the prefab you can set the text.

btw you if you search 'fsm' in the actions there are more than only strings.

Also if you don't know yet how globals work you can check this video :


elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: instantiate prefabs with different values
« Reply #2 on: June 08, 2016, 06:17:15 AM »
I placed everything on a different FSM, one that manages this mini-game, I made it so the prefab spawns x amount of times, but then the text only works on one. I've tried doing similar logic on the prefab itself, but it would just create unlimited number of copies. I'm not sure how to get this right.. All I need is to load a few unique ids with text from xml file into x amount of prefabs. I'm completely braindead now, too much thinking about this, I feel dumb  :D
« Last Edit: June 08, 2016, 11:36:25 AM by elusiven »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: instantiate prefabs with different values
« Reply #3 on: June 08, 2016, 03:48:00 PM »
Hi,
I think you need to loop this : get info from xml, then create the prefab, then set variables on the prefab, loop back and  get the next info from xml, and so on.

if you don't have much experience with datamaker you can check out this tutorial


elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: instantiate prefabs with different values
« Reply #4 on: June 08, 2016, 04:43:56 PM »
Okay I've got creating prefabs in loop until certain amount has been reached, datamaker won't let me link the xpath result (a number) as a global variable because I tried creating a global variable wordNumber and increase it every time when prefab is being created so that it will pull different text from xml but it can only be local variable assigned, and with local variable on each prefab clone I'm not sure how I can change each prefab texts to the one from xpath query. How would I make each clone assign different value of "word" from xml file ? Are there any specific playmaker tutorials on this or examples ?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: instantiate prefabs with different values
« Reply #5 on: June 08, 2016, 05:46:51 PM »
Hi,
I will try to make a sample tomorrow.

The local variable you can easily make it global by using "set String value"
I will ask to jean (hes the maker from datamaker) if he can update it so it can be global



elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: instantiate prefabs with different values
« Reply #6 on: June 08, 2016, 07:25:40 PM »
Ok, thanks :)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: instantiate prefabs with different values
« Reply #7 on: June 09, 2016, 04:40:30 AM »
Hi,
Here you go, i hope this helps :


elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: instantiate prefabs with different values
« Reply #8 on: June 09, 2016, 07:03:37 AM »
Nice work! Thanks, it works now:) I completely forgot about using lists.

Sorry to bother you with questions but I have one more... Looking at the picture above (the word game), how would I check if the sentence made up of those words is correct? So for example 5 words generated from xml, and they can only be marked as "correct" if they are clicked on/placed in the correct order

Would I have to add "correct" property to my words in xml, and then somehow check if the "correct" "words" have been clicked on in correct order?

Thanks
« Last Edit: June 09, 2016, 07:05:35 AM by elusiven »

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: instantiate prefabs with different values
« Reply #9 on: June 09, 2016, 04:56:52 PM »
Hi,
i think with string build
and string compare

when a word placed in position 1 save it in string 1 and position 2 in string 2 and so on
When all placed , build the string from string 1 , 2 ,3 ... and then compare the string to the solution

And no problem to bother with questions :)
« Last Edit: June 09, 2016, 05:34:13 PM by djaydino »

elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: instantiate prefabs with different values
« Reply #10 on: June 10, 2016, 09:36:59 AM »
Ah ok, so the string build action.. It looks like that's what I need, thanks.

sorry to bother you again, but would you have anything in mind on how to layout those "words" in rows with equal spacing? This is the worst thing that I'm struggling with now, been thinking for a few days and can't get my head around it..

Just like that:


Thank you for all the help...

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: instantiate prefabs with different values
« Reply #11 on: June 10, 2016, 11:01:48 AM »
Hi,

 Grid layouts are very limited in Unity, but you can achieve it nonetheless.

 you can use "gridLayout"; http://docs.unity3d.com/Manual/script-GridLayoutGroup.html

or if you truly want a relative size that takes the whole width, I would simply use Anchors, so you create yourself the grid using relative sizing with reach RectTransform anchor value ( which guarantee a resolution independant grid).

Bye,

 Jean

elusiven

  • Full Member
  • ***
  • Posts: 233
  • Debt we all must pay...
Re: instantiate prefabs with different values
« Reply #12 on: June 10, 2016, 12:10:01 PM »
Thanks Jean, grid seems to solve part of the problem, Content size fitter doesn't work correctly now.. Grid seems to control the size of the boxes, and they go out of bounds when there is more text.. How can I make them fit the text in, and keep the grid?  They also seem to be not in the center of the box...
« Last Edit: June 12, 2016, 07:01:22 PM by elusiven »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: instantiate prefabs with different values
« Reply #13 on: June 27, 2016, 04:00:25 AM »
Hi,

 you need to fiddle around your text settings ( center them vertically and horyzontally, etc etc).

contentSizeFitter is difficult to get, so don't give up and again, you'll need to mess with values. Usually, I work with non prefabs until I have the layout right, then I use prefabs. It's easier and everything can be done in editor for quicker workflow while figuring out how UI layout works. OR play the scene and then as it plays change settings around until you achieve the desire result ( but you need to edit the 6 instances...)

Bye,

 Jean


 Bye,

 Jean