- Home /
Create a function that spawn player (wich datas are stored in an array) randomly
I'm trying to make a function that spawn player stored in an array randomly but I'm having this problem": i don't know how to take each value ( for ex. each firstname, lastname) of each user. The array is like this:
[{"firstname":"daniele","lastname":"daveri","username":"dany","gender":"male"},{"firstname":"admin","lastname":"admin","username":"admin","gender":"male"},{"firstname":"prova3","lastname":"prova3","username":"prova3","gender":"male"},{"firstname":"pp","lastname":"pp","username":"pp","gender":"male"}]
So for example for the player : "daniele" the system spawn a male player prefab and so for all player stored in the array.
Thanks!
This site is not meant for "make the code for me" questions.
This site is meant for questions like "I'm trying to make a function that spawn player stored in an array randomly but I'm having this problem"
Please update your question with your problem or learn to code.
@dorpeleg, I didn't take his question as "write this for me", though it was a bit vague. Giving him a starting point is certainly a do-able thing. The comment about "learn to code" is exactly what we don't need here.
Answer by Yokimato · Jul 17, 2013 at 01:57 PM
You can do random spawning many ways however having learned my lesson from doing it all programmatically, I'd suggest doing it only partially.
In your scene you could make a game object for each valid spawn location and tag it as "spawnLocation". Then using GameObject.FindGameObjectsWithTag("spawnLocation") you can an array of valid locations. Grab a random index from this array and use the object's transform.position.
This allows for more control of what's a valid location and avoids issues where the player could spawn in the terrain for instance.
Answer by gamesbyangelina · Jul 17, 2013 at 03:08 PM
If you're using the first name to look up the rest of the information, you might like to use a Map. Maps are data structures that associate one piece of data (called a Key) with another (called a Value). So you would have a map like this:
PlayerData daniele = PlayerDataMap.get("Daniele");
//More code here
I know this isn't a complete answer for you but hopefully it might help! Here's some info on Dictionaries in C#, which have this feature: http://www.dotnetperls.com/dictionary
Answer by dany88 · Jul 17, 2013 at 03:05 PM
ok thanks ! however i don't know how to take each values for each player in the array. thanks!
Answer by inkspot · Jul 17, 2013 at 03:16 PM
You can do this by inserting an array into an array, here's a small example of how the idea works (if i understood you correctly);
var mainarray : new Array();
mainarray.Add("firstname","daniele","lastname","daveri","username","dany","gender","male");
var playertospawn = mainarray[0]; /// gets first entry of the mainarray //
var playername = playertospawn[1]; /// gets 2th entry which is daniele //
thanks so much! I need to put in the array the values automatically, but i dnt know how to automatically select for ex the value :"daniele" from the complete string..
re thanks!! :)