Put strings together to make a name for a scene?

Hello. My game is having options for the city:

Time of Day

  • Morning
  • Noon
  • Evening
  • Night

Traffic Density

  • None
  • Low
  • Medium
  • High

Pedestrian Density

  • None
  • Low
  • Medium
  • High

Now. How do I make them make strings(like “Night, Low, High”)… and load the city name “FreeRoamNightLowHigh” ? I do now know how to do this in any way.

You can use string.Format, i.e.

string timeOfDay = "Night";
string traficDensity = "Low";
sting pedestianDensity = "High";
string prefix = "FreeRoam"

string levelName = string.Format("{0}{1}{2}{3}", prefix, timeOfDay, traficDensity, pedestianDensity);
// or just concate it 
string levelName = prefix+timeOfDay+traficDensity+pedestianDensity;

Usually it’s you’ll use string.Format as it as more options and is usually more readable (the aboves example is bit unfitting) and format or padding options.

string.Format is typically more readable, i.e

string name = "Tom";
float height = 1.524;
int age = 31;

string someString = string.Format("{0} is {1:F2}m tall and {2:smile:} old", name, height, age);

// vs 

string someString = name + "is " + height.toString("F2") + "m tall and " + age + "old";

Wow thats kind of an interesting question… assuming you havent figured it out on your own first… are you using javascript or c#.

Well in general terms, you could just have 3 String variables, Time of Day, Traffic Density, and Pedestrian Density, and just have their options be the morning noon evening night, or none low medium and high. then you could add the strings together like this, and i will do this in c# but its hardly any difference for javascript

public String timeofday = "Morning";
public String trafficdensity = "Medium";
public String pedestriandensity = "High";
String combinedwords;
void Update() {
    combinedwords = timeofday + trafficdensity + pedestriandensity;
    print(combinedwords);  
}

what this would do is print out “MorningMediumHigh” in the console, or you could erase all the values i set them = to and assign them in inspector or assign them in your code, but im not exactly sure what you want so that was just an example.

so yea, if you havent already figured this out then i hope this helps.

Why not associate the the three catagories members with ints.

If(timeofday == 1)
Morning = true;

I assume you have a premade level for each combination? It woukd be great if you had a basic level that could load any condition. That way if condition moring true, set it as such.

Here’s my script. But, it doesn’t seem to be adding the Strings to the name, or when clicked. Any ideas?

var pos : Rect;
var nativeHorizontalResolution = 1024.0;
var nativeVerticalResolution = 768.0;
var Cruise : Texture2D;
var Button_1 : AudioClip;
var Location : String;
var TimeOfDay : String = "Day";
var TrafficDensity : String = "Low";
var PedestrianDensity : String = "Low";
var levelName : String = prefix + TimeOfDay + TrafficDensity + PedestrianDensity;
var prefix : String = "FreeRoam";

function OnGUI () {
	GUI.matrix = Matrix4x4.TRS(Vector3(0,0,0), Quaternion.identity, Vector3(Screen.width / nativeHorizontalResolution, Screen.height / nativeVerticalResolution, 1));

	if (GUI.Button (Rect (pos), Cruise)) {
		 audio.PlayOneShot(Button_1);
	     Application.LoadLevel (Location);
	     PlayerPrefs.SetString("TimeOfDay", TimeOfDay);
	     PlayerPrefs.SetString("TrafficDensity", TrafficDensity);
	     PlayerPrefs.SetString("PedestrianDensity", PedestrianDensity);
	}				 
}

function Update(){
levelName == prefix + TimeOfDay + TrafficDensity + PedestrianDensity;
PlayerPrefs.SetString("FreeRoamLevelToLoad",levelName);
}

In your Button method you are using the “Location” variable, but you never set it. You set “levelName”.

    Application.LoadLevel(levelName);

However, generating the string in the Update Method makes no sense, save does neither.

You shouldn’t generate your levelName over and over again, because each time you concat one (or more) strings, a new string instance will be created and the reference to the previous one removed. If you do this every frame, you will soon have 1000s of orphaned strings in memory, which will eventually trigger the garbage collection, which results in small hiccup while the memory is being released.

Similar goes for PlayerPrefs being called every Update. Each time you use PlayerPrefs.SetString, the XML file containing the settings will be saved. This is quite performance costly and will significantly lower your fps.

Ignore the “Location”. That’s just for any button so I can type in a level it needs to load incase it needs one to load. :slight_smile: Also. how should I update the name?

Well, you can use Selection Grind for the 3 alternatives you have.

private string[] timeOfDayOptions = new string[] { "Night", "Day" };
private string[] traficOptions = new string[] { "Low", "Medium", "High" };
private string[] pedestianOptions = new string[] { "Low", "Medium", "High" };
private int timeOfDay;
private int trafic;
private int pedestian;

void OnGUI() {
    // Draw 3 selection grids with all options. Each Grid can only have one option active
    timeOfDay = GUI.SelectionGrid(new Rect(25,25,200,25), timeOfDay, timeOfDayOptions, 1);
    trafic = GUI.SelectionGrid(new Rect(25,50,200,25), trafic, traficOptions, 1);
    pedestian = GUI.SelectionGrid(new Rect(25,75,200,25), pedestian, pedestianOptions, 1);

    if(GUI.Button(new Rect(25, 100, 100,25), "Start Level")) {
        string levelName = "FreeRoam"+timeOfDayOptions[timeOfDay]+traficOptions[trafic]+pedestianOptions[pedestian];
        Application.Load(levelName);
    }
}

This way the string will only be created
a) shortly before needed
b) only when the users hits the Start Level button.

Edit:
Also, if you want to save the settings “live” as the user change it, you can use the GUI.changed property to see if any input has been changed since last UI draw/GUI operation

void OnGUI() {
    // Draw 3 selection grids with all options. Each Grid can only have one option active
    timeOfDay = GUI.SelectionGrid(new Rect(25,25,200,25), timeOfDay, timeOfDayOptions, 1);
    trafic = GUI.SelectionGrid(new Rect(25,50,200,25), trafic, traficOptions, 1);
    pedestian = GUI.SelectionGrid(new Rect(25,75,200,25), pedestian, pedestianOptions, 1);

    if(GUI.changed) {
        // User has changed some UI input/settings
        PlayerPrefs.SetString("Blah", timeOfDay);
        ....
    }
    if(GUI.Button(new Rect(25, 100, 100,25), "Start Level")) {
        string levelName = "FreeRoam"+timeOfDayOptions[timeOfDay]+traficOptions[trafic]+pedestianOptions[pedestian];
        Application.Load(levelName);
    }

I’ve formatted it to js. But it keeps saying the first line needs a semicolon and expecting a ‘:’, not a “,”.

private var TimeOfDayOptions : String[]{ "Night", "Day", "Noon", "Morning"};
private var TrafficOptions : String[]{ "Low", "Medium", "High" };
private var PedestrianOptions : String[]{ "Low", "Medium", "High" };

rolleyes
You know that you need to assign variables? That’s basic knowledge in every language.

private var TimeOfDayOptions : String[] = new String[] { "Night", "Day", "Noon", "Morning"};
private var TrafficOptions : String[] = new String[] { "Low", "Medium", "High" };
private var PedestrianOptions : String[] = new String[] { "Low", "Medium", "High" };

I have updated my code but get the same error.