Trying to get information from a inputted string

Hello, I was wondering if you guys could help me with a simple problem. I’m working on my game and am in the process of creating a console system so I can debug certain parts during runtime. Let me give you a run down of the usage of what I want to do. The game I’m working on is multiplayer and the clients get a network object (the sun) that is controlled by the server. It works but I want to be able to input a command from the server console (That is set up) that changes the float of the time on the script that I have attached to the sun that controls the time. I will attach examples of both the server console script and the sun script.

using UnityEngine;
using System.Collections;
//this is the console system that I need to be able to input a integer after a sting like if (command == "Server.Time" + any integer that I want to save to a var and send to the time script.)
public class ConsoleSystem : MonoBehaviour {
public string command = "234";
	bool activated = false;
	// Use this for initialization
		int a = 1;
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {

	}
	public void RunCommand2(){
		if (command == "test") {
			Debug.Log ("yeahh buddy!");
		}
	}
	public void RunCommand(){
		if (command == "Server.Quit")
			Application.Quit ();
		if (command == "Server.TargetPlayer") {
			RunCommand2();
		}
		if (command == "Server.DisconnectPlayer"+a) {
			Debug.Log ("Disconnecting: " + Network.connections [a-1].ipAddress + ":" + Network.connections [0].port);
			Network.CloseConnection (Network.connections [a-1], true);
			
		}

	}

}

the script below this is my daylight script.
using UnityEngine;
using System.Collections;

public class DayNightServer : MonoBehaviour {
	
	public Light sun;
	public float secondsInFullDay = 120f;
	[Range(0,1)]
	//the float below this is the one that I want to be able to change from the console system.
	public float currentTimeOfDay = 0;
	[HideInInspector]
	public float timeMultiplier = 1f;
	public float swag = 0f;
	public NetworkView nView;
	public bool isserver = false;
	public float tod2 = 0;
	float sunInitialIntensity;
	
	void Start() {
		sunInitialIntensity = sun.intensity;
		nView = GetComponent<NetworkView>();

			if (Network.isClient)
			isserver = false;
		else
			isserver = true;


	}
	
	void Update() {
		UpdateSun();
		if (isserver == true){
		currentTimeOfDay += (Time.deltaTime / secondsInFullDay) * timeMultiplier;
		if (currentTimeOfDay >= 1) {
			currentTimeOfDay = 0;

		}
	}
		}
	
	void UpdateSun() {
		sun.transform.localRotation = Quaternion.Euler((currentTimeOfDay * 360f) - 90, 170, 0);
		
		float intensityMultiplier = 1;
		if (currentTimeOfDay <= 0.23f || currentTimeOfDay >= 0.75f) {
			intensityMultiplier = 0;
		}
		else if (currentTimeOfDay <= 0.25f) {
			intensityMultiplier = Mathf.Clamp01((currentTimeOfDay - 0.23f) * (1 / 0.02f));
		}
		else if (currentTimeOfDay >= 0.73f) {
			intensityMultiplier = Mathf.Clamp01(1 - ((currentTimeOfDay - 0.73f) * (1 / 0.02f)));
		}
		
		sun.intensity = sunInitialIntensity * intensityMultiplier;
	}
}

1 Answer

1

You’ll need a reference to your DayNightServer script in your console somewhere in order to access the variable of course.

As to how to set the value, see here for a number of options on how to remove all non-digits from a string: c# - Keep only numeric value from a string? - Stack Overflow. If you want a float, you’ll need to alter it a bit to keep the decimal point. Or you could take a different approach and simply remove the first 10 chars from your command string (10 chars because "intensity " is 10 chars long), or something like that, then do float.Parse on the resulting string.

So you’ll have to do something like

using System;

DayNightServer server;

if(command.StartsWith("intensity")){
 float amount = float.Parse(ToDigit(command));
 server.SetSunIntensity(amount);
 
}

Not writing the ToDigit method here, you’ll have to think on what the best way to implement would be for your usecase.

Thank you :) This was really bothering me for awhile!

–