playMaker

Author Topic: how to create objects in a foreach loop[SOLVED]  (Read 5306 times)

9TOFRIDAY

  • Playmaker Newbie
  • *
  • Posts: 39
how to create objects in a foreach loop[SOLVED]
« on: December 29, 2015, 05:58:05 AM »
hey peeps

I'm trying to create an object in a foreach loop but get this error:
"INTERNAL_CALL_Internal_InstantiateSingle can only be called from the main thread."

here is my code:
Code: [Select]
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
//using Facebook.Unity;
using Parse;

namespace HutongGames.PlayMaker.Actions {

[ActionCategory("DataParse")]
[Tooltip("Parse Scripts")]

public class DataParseSelect : FsmStateAction {
public FsmString UserID_, UserName_, UserGeoXLongitude_, UserGeoYLatitude_, UserVideoID_, UserVideoURL_;
string UserID, UserName, UserGeoXLongitude, UserGeoYLatitude, UserVideoID, UserVideoURL;

//luke

//public Transform prefab;


//playmaker stuff
[Tooltip("GameObject to create. Usually a Prefab.")]
public FsmGameObject gameObject;

[Tooltip("Position. If a Spawn Point is defined, this is used as a local offset from the Spawn Point position.")]
public FsmVector3 position;

[Tooltip("Optionally store the created object.")]
public FsmGameObject storeObject;




public FsmInt row;

public override void OnEnter()
{
row = 0;
DataParseQuerySelect ();

Finish ();

}

void DataParseQuerySelect()
{
try{
var query = ParseObject.GetQuery("hwsh_user_data");
query.FindAsync().ContinueWith(t =>
                               {
IEnumerable<ParseObject> results = t.Result;
foreach (var obj in results)
{
UserID = obj.Get<string>("User_ID");
UserName = obj.Get<string>("User_Name");
UserGeoXLongitude = obj.Get<string>("User_GeoX_Longitude");
UserGeoYLatitude = obj.Get<string>("User_GeoY_Latitude");
UserVideoID = obj.Get<string>("User_Video_ID");
UserVideoURL = obj.Get<string>("User_Video_URL");

Debug.Log(UserID.ToString() + " - " + UserName.ToString() + " - " + UserGeoXLongitude.ToString() + " - " + UserGeoYLatitude.ToString() + " - " + UserVideoID.ToString());

UserID_.Value = UserID;
UserName_.Value = UserName;
UserGeoXLongitude_.Value = UserGeoXLongitude;
UserGeoYLatitude_.Value = UserGeoYLatitude;
UserVideoID_.Value = UserVideoID;
UserVideoURL_.Value = UserVideoURL;
CreateMyMarkerObject();




//row.Value = row.Value + 1;
}
});
}
catch
{
Debug.Log ("Database query failed.");
}
}

public void CreateMyMarkerObject()
{
Debug.Log ("create my marker object");

var go = gameObject.Value;

if (go != null) {
var spawnPosition = Vector3.zero;
var spawnRotation = Vector3.zero;

var newObject = (GameObject)Object.Instantiate(go, spawnPosition, Quaternion.Euler(spawnRotation));


}
row.Value = row.Value + 1;

}
}
}

So the idea is that i am pulling information from parse. i then want to create a game object with the values. so where i am stuck at the moment is just creating a object for each entry.

please can anyone assist.

« Last Edit: March 24, 2016, 06:58:51 AM by jeanfabre »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3987
  • Official Playmaker Support
    • LinkedIn
Re: how to create objects in a foreach loop
« Reply #1 on: December 29, 2015, 12:44:07 PM »
It seems like this is an issue with async which runs in a separate thread. Instantiate can only be called from the main thread.

Some more info:
http://stackoverflow.com/questions/24441501/main-thread-issue-with-parse-com-queries

EDIT: This might mean you need a Monobehaviour proxy to run the Coroutine. The action could then call that Monobehaviour as needed...
« Last Edit: December 29, 2015, 01:13:22 PM by Alex Chouls »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: how to create objects in a foreach loop
« Reply #2 on: December 29, 2015, 04:40:49 PM »
Hi,

 Have you tried to base your code on my Parse support for PlayMaker on the wiki?

 I think I bumped on this as well, and so I would suggest you study the actions I created and maybe you'll find a good base to expand on the missing features on my package.


 Bye,

 Jean

9TOFRIDAY

  • Playmaker Newbie
  • *
  • Posts: 39
Re: how to create objects in a foreach loop
« Reply #3 on: December 30, 2015, 04:07:01 AM »
It seems like this is an issue with async which runs in a separate thread. Instantiate can only be called from the main thread.

Some more info:
http://stackoverflow.com/questions/24441501/main-thread-issue-with-parse-com-queries

EDIT: This might mean you need a Monobehaviour proxy to run the Coroutine. The action could then call that Monobehaviour as needed...

thanks for getting back to me. i will looks at your suggestion today.

9TOFRIDAY

  • Playmaker Newbie
  • *
  • Posts: 39
Re: how to create objects in a foreach loop
« Reply #4 on: December 30, 2015, 04:08:29 AM »
Hi,

 Have you tried to base your code on my Parse support for PlayMaker on the wiki?

 I think I bumped on this as well, and so I would suggest you study the actions I created and maybe you'll find a good base to expand on the missing features on my package.


 Bye,

 Jean

actually what happened was i wrote the parse stuff myself, and only discovered your parse support yesterday. i will take a look at them and see if how you solved it. thanks so much for getting back to me.

Margaret52

  • Playmaker Newbie
  • *
  • Posts: 2
Re: how to create objects in a foreach loop
« Reply #5 on: December 30, 2015, 06:02:02 AM »
Good topic and thank you jeanfabre for your hints - they helped me.
« Last Edit: December 30, 2015, 06:03:42 AM by Margaret52 »
Grafika do kuchni to mój wybór jako dekoratora wnętrz. Również do łask wracają fototapety i coraz częściej można je spotkać na ścianach. Ludzie wybierają lacobel zamiast płytek do kuchni, któe wygląda bardzo fajnie.

Elizabeth73

  • Playmaker Newbie
  • *
  • Posts: 1
Re: how to create objects in a foreach loop
« Reply #6 on: March 07, 2016, 01:59:30 PM »
Thank you for your advices, i definitely use them
Hey guys! I am a citizen of a great town called New York. I am a huge fan of entire New York county.  If you are looking for good school check it :  Szkoły Policealne Nowy Sącz