Hello, I'm working on a bus simulator.
What I Want:
The player talks to someone at a station, and then chooses the passengers he wants to carry. Then after the player returns to the bus, he can see the passengers he just chose sitting on the chairs. The player can even interact and talk with the passengers.
Current Issue
How to store the passengers the player has chosen?
The station and bus interior are two scenes, How to pass the data from one scene to another? I know I can use variables, but in the case of my game, it would be a mess to rely on variable.
How to instantiate the passengers in the bus interior scene?
Best Practice to Instantiate NPC
Re: Best Practice to Instantiate NPC
Hi,
Let's look at the bus scene first. In the bus scene, what would be a good way to know which passengers to instantiate, and then to instantiate them? Here's one idea:
1. Put each passenger prefab in a Resources folder. (Or Addressables or asset bundle, or whatever. I'll use Resources because it makes the code simpler to read here. I'll also make a few other decisions below that make the code shorter.) So your Resources folder might contain prefabs named Ann, Bob, Carl, Donna, etc.
2. Add a script to the Dialogue Manager GameObject that maintains a list of passengers to instantiate. Example:
3. In the bus scene, add a script that instantiates the passengers:
In the station scene, you'll want to populate the PassengerManifest's passengerNames list. One way to do this is to add a C# method:
Since you'll want to call the AddPassenger() method in the station's conversation, register it with Lua:
Then in your dialogue entries' Script fields, call AddPassenger:
Let's look at the bus scene first. In the bus scene, what would be a good way to know which passengers to instantiate, and then to instantiate them? Here's one idea:
1. Put each passenger prefab in a Resources folder. (Or Addressables or asset bundle, or whatever. I'll use Resources because it makes the code simpler to read here. I'll also make a few other decisions below that make the code shorter.) So your Resources folder might contain prefabs named Ann, Bob, Carl, Donna, etc.
2. Add a script to the Dialogue Manager GameObject that maintains a list of passengers to instantiate. Example:
Code: Select all
public class PassengerManifest : MonoBehaviour
{
public List<string> passengerNames = new List<string>();
}
Code: Select all
public class Bus : MonoBehaviour
{
public List<Transform> seats; //<-- Assume these are assigned in the inspector.
private void Start()
{
var passengerManifest = FindObjectOfType<PassengerManifest>();
for (int i = i; i < passengerManifest.passengerNames.Count; i++)
{
GameObject prefab = Resources.Load<GameObject>(passengerManifest.passengerNames[i]);
Instantiate(prefab, seats[i]);
}
}
}
Code: Select all
public class PassengerManifest : MonoBehaviour
{
public List<string> passengerNames = new List<string>();
public void AddPassenger(string passengerName)
{
passengerNames.Add(passengerName);
}
}
Code: Select all
public class PassengerManifest : MonoBehaviour
{
public List<string> passengerNames = new List<string>();
private void Awake()
{
Lua.RegisterFunction("AddPassenger", this, SymbolExtensions.GetMethodInfo(() => AddPassenger(string.Empty)));
}
public void AddPassenger(string passengerName)
{
passengerNames.Add(passengerName);
}
}
- Dialogue Text: "Hi! I'm Bob. Here's my bus ticket."
- Script: AddPassenger("Bob")
- DearDeerDee
- Posts: 30
- Joined: Mon Mar 22, 2021 10:00 am
Re: Best Practice to Instantiate NPC
Hi Tony, thanks for your response! It is super informative!
Re: Best Practice to Instantiate NPC
Glad to help!