- Home /
Simple quest system: from Update function to OnGUI? (javascript)
Hello all!
First time posting here :)
I've come across a simple quest system script (which can be seen here in Portuguese) to which I made some minor tweaks. However, being no programmer myself, I'm feeling way out of my league to convert the code I've got so far - which uses mostly the Update function - to an OnGUI function in order to make it more flexible for the use of the various GUI components.
The system so far uses 2 scripts that communicate to each other. Here's the first, which goes to my quest giver NPC:
#pragma strict
var questHelper : Missoes; //calls for the script Missoes.js, not the variable
var wizetteDistance : Vector3;
var talk : GUIText;
var questDesactivation : boolean;
function Start ()
{
questDesactivation = false;
}
function Update ()
{
if(questDesactivation == false)
{
wizetteDistance = GameObject.FindWithTag("Player").transform.position;
wizetteDistance = wizetteDistance - transform.position;
talk.text = "";
if(wizetteDistance.magnitude < 2)
{
talk.text = "Press Enter to accept the mission";
if (Input.GetKeyDown(KeyCode.Return))
{
questHelper = GameObject.FindWithTag("Player").AddComponent("Missoes");
questHelper.questText = talk;
questDesactivation = true;
}
}
}
else if(Input.GetKeyDown(KeyCode.Escape))
{
questDesactivation = false;
}
}
The following script will then be added to the player:
#pragma strict
var currentObjective : int;
var quests : String[];
var questText : GUIText;
var distance : float;
var item : GameObject;
var itemCreation : boolean;
var itemPosition : Vector3;
//var questName = "Retrieve the Bottle."
function Start ()
{
currentObjective = 0;
quests = new String[4];
quests[0] = "Talk to the master ";
quests[1] = "Go to the temple ";
quests[2] = "Search for the bottle ";
quests[3] = "Go back and deliver it to the master ";
}
/*
function OnGUI ()
{
questWindow = GUI.Window(0, questWindow, Quest, "Quest: " + questName);
}
function Quest (windowID : int)
{
GUI.DragWindow(Rect(0,0, 10000, 20));
GUI.Label (Rect (10, 10, 300, 310), quests[currentObjective]);
if (Input.GetKeyDown("z"))
{
}
}
*/
function Update ()
{
questText.text = quests[currentObjective];
if (Input.GetKeyDown("z"))
{
currentObjective++;
if (currentObjective >= quests.Length)
{
currentObjective = 0;
}
}
if (currentObjective == 0)
{
distance = Mathf.FloorToInt(Vector3.Distance(GameObject.Find("Master").transform.position, transform.position));
questText.text += "Dis: "+ distance;
if(distance <= 3)
{
questText.text = "You must to go the temple and look for the golden vase [Press Enter to accept it].";
if(Input.GetKeyDown(KeyCode.Return))
{
currentObjective = 1;
}
}
}
if (currentObjective == 1)
{
distance = Mathf.FloorToInt(Vector3.Distance(GameObject.Find("MissionPoint").transform.position, transform.position));
//distance = Mathf.FloorToInt(Vector3.Distance(GameObject.Find("MissionPoint").transform.position, transform.position / transform.lossyScale.magnitude));
questText.text += "Dis: "+ distance;
if(distance <= 3)
{
currentObjective = 2;
itemCreation = false;
}
}
if (currentObjective == 2)
{
if(!itemCreation)
{
item = GameObject.Find("Bottle");
if (Random.value < 0.5)
{
itemPosition = GameObject.Find("P1").transform.position;
}
else
{
itemPosition = GameObject.Find("P2").transform.position;
}
item = Instantiate(item, itemPosition, item.transform.rotation);
itemCreation = true;
}
else
{
distance = Mathf.FloorToInt(Vector3.Distance(item.transform.position, transform.position));
questText.text += "Dis: " + distance;
if(distance <= 1)
{
questText.text = "Press Enter to get the bottle. ";
if(Input.GetKeyDown(KeyCode.Return))
{
currentObjective = 3;
Destroy(item);
}
}
}
}
if (currentObjective == 3)
{
distance = Mathf.FloorToInt(Vector3.Distance(GameObject.Find("Master").transform.position, transform.position));
questText.text += "Dis: "+ distance;
if(distance <= 3)
{
questText.text = "Thank you for the bottle! [Press Enter to finish the quest].";
if(Input.GetKeyDown(KeyCode.Return))
{
currentObjective = 4;
questText.text = "";
Destroy(gameObject.GetComponent(Missoes));
}
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
questText.text = "You quit the quest! Talk to the dummie to get the quest again. ";
yield WaitForSeconds(8); //Not working due to the constant refreshing :(
Destroy(gameObject.GetComponent(Missoes));
}
}
My biggest issue right now is the fact that for all of this to work, I need to keep the GUIText element for the communication of the 2, as far as I see it... :/ The mechanics of it are great, and fit my needs very well. I just need this all to be inside an OnGUI function rather than displayed as a GUIText.
Ideally, the visual outcome of it would be something such as seen on this tutorial, which uses somewhat the following code:
#pragma strict
static var activateQuest = false;
var questWindow : Rect = Rect (((Screen.width / 2) - 100), ((Screen.height / 2) - 100), 512, 256);
var questName = "Test";
var questContent = "";
var avatar = Texture;
function OnGUI ()
{
if(activateQuest)
{
questWindow = GUI.DragWindow(0, questWindow, Quest, "Quest" + questName);
}
}
function Quest (windowID : int)
{
GUI.Label (Rect (10, 10, 300, 310), questContent);
GUI.DrawTexture(Rect(a,b,c,d), avatar);
if (GUI.Button(Rect(((questWindow.width / 2) - 50), (questWindow.height / 2) - 100), 512, 256), "Accept")
{
activateQuest = true;
}
if (GUI.Button(Rect(((questWindow.width / 2) - 30), (questWindow.height / 2) - 100), 512, 256), "Cancel")
{
activateQuest = false;
}
}
function OnMouseDown ()
{
renderer.material.color = Color.red;
activateQuest = true;
}
Ugh, I hope I'm not being confusing!
This is for a game I'm creating as a graduation project in 2 weeks from now, so other than eternal gratitude, I'll also make sure to mention you in my project :)
Maaaaaaaaaaaaaaany many thanks in advance to anyone who can give me a helping hand!
Laura
Your answer
Follow this Question
Related Questions
Variables in GUI not updating/changing? (javascript) 1 Answer
Ambiguous Label error in custom "for i in Array" GUI 2 Answers
Eliminating input loss 1 Answer
GetKeyDown is fired more than once in Update() 1 Answer
OnTriggerEnter only recognised once, no matter how many times I enter ? (Solved) 1 Answer