Reward System
Reward System
Hi,
I have setup a quest line following one of the YouTube tutorials but I would like the player to receive some coins at the end (about 20 coins). I already had two scripts "coin script & gamehandler" and with these scripts I am able to walk around collecting coins but I would also like to receive these as a quest reward. I am new to unity and dialogue system so I am not sure how I would go about doing this, I can post my scripts if that would help? Thank you
I have setup a quest line following one of the YouTube tutorials but I would like the player to receive some coins at the end (about 20 coins). I already had two scripts "coin script & gamehandler" and with these scripts I am able to walk around collecting coins but I would also like to receive these as a quest reward. I am new to unity and dialogue system so I am not sure how I would go about doing this, I can post my scripts if that would help? Thank you
Re: Reward System
Hi,
I'll assume that your script has a method to give coins to the player.
The Dialogue System supports a simple scripting system called Lua that you can use in dialogue entries' Conditions and Script fields. You can tie your script method into Lua. Then in the dialogue entry's Script field call the method to give coins to the player.
More info: (the last link -- a video tutorial -- is probably the most immediately helpful)
I'll assume that your script has a method to give coins to the player.
The Dialogue System supports a simple scripting system called Lua that you can use in dialogue entries' Conditions and Script fields. You can tie your script method into Lua. Then in the dialogue entry's Script field call the method to give coins to the player.
More info: (the last link -- a video tutorial -- is probably the most immediately helpful)
Re: Reward System
Hi Tony,
Thank you for your help I really appreciate it.
I think I have most of it working, when completing the quest I now get the coins shown in the public int which is attached to the player. For example I get 20 coins for completing this quest and shows in the inspector for the player but how would I update this to the canvas as I already have a certain amount of coins collected but i'd like this to add to the other amount collected from the quest. Sorry for the stupid question
Thank you for your help I really appreciate it.
I think I have most of it working, when completing the quest I now get the coins shown in the public int which is attached to the player. For example I get 20 coins for completing this quest and shows in the inspector for the player but how would I update this to the canvas as I already have a certain amount of coins collected but i'd like this to add to the other amount collected from the quest. Sorry for the stupid question
Re: Reward System
Hi,
It sounds like you should put that in your script, separate from any specific Dialogue System activity. For example, if your C# method is this:
A better way may be to use a property:
It sounds like you should put that in your script, separate from any specific Dialogue System activity. For example, if your C# method is this:
Code: Select all
public Text coinsText; // The UI Text element for coins amount
public int coins; // The number of coins the player has
public void AddCoins(int amount)
{
coins += amount;
coinsText.text = coins.ToString();
}
Code: Select all
public Text coinsText; // The UI Text element for coins amount
private int _coins; // The number of coins the player has (this is called a private backing variable)
public int coins
{
get { return _coins; }
set { _coins = value; if (coinsText != null) coinsText.text = _coins.ToString(); }
}
public void AddCoins(int amount)
{
coins += amount;
}
Re: Reward System
Hi,
It's just not updating the UI, would you like me to post the scripts I currently have?
It's just not updating the UI, would you like me to post the scripts I currently have?
Re: Reward System
Sure. I'll take a look and help if I can. You might also try adding Debug.Log() statements to make sure that the code is actually being called.
Re: Reward System
Sorry about this, I have 3 scripts as shown below;
They seem to work in the inspector but I'm just having issue transferring that to the UI
Code: Select all
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class GameHandler : MonoBehaviour
{
public TextMeshProUGUI AcornText;
public int Acorns = 0;
public void AddCoins(int amount)
{
AcornText.text = "Acorns Collected : 0 " + Acorns;
Acorns += amount;
AcornText.text = Acorns.ToString();
}
}
Code: Select all
public class CoinScript : MonoBehaviour
{
public GameHandler GH;
public AudioClip acornSound;
public int AcornValue = 20;
// Start is called before the first frame update
void Start()
{
GH = GameObject.Find("Canvas").GetComponent<GameHandler>();
}
private void OnTriggerEnter(Collider other)
{
GH.Acorns += AcornValue;
AudioSource.PlayClipAtPoint(acornSound, transform.position);
Destroy(gameObject);
}
}
Code: Select all
using PixelCrushers.DialogueSystem;
public class PlayerAcorn : MonoBehaviour
{
public int Acorns;
public GameHandler GH;
public void GiveAcorns(double amount)
{
GH.Acorns += (int)amount;
}
#region Register with Lue
private void OnEnable()
{
Lua.RegisterFunction("GiveAcorns", this, SymbolExtensions.GetMethodInfo(() => GiveAcorns((double)0)));
}
private void OnDisable()
{
Lua.UnregisterFunction("GiveAcorns");
}
#endregion
}
Re: Reward System
In GameHandler.cs, change the order:
In CoinsScript.cs, you can make it more robust by using FindObjectOfType so it doesn't depend on a GameObject named exactly "Canvas":
In PlayerAcorn.cs, make sure to find the GameHandler before using GH. Also, use GameHandler.AddCoins(), which updates the UI Text in addition to incrementing the variable:
Code: Select all
public void AddCoins(int amount)
{
Acorns += amount;
AcornText.text = "Acorns Collected : " + Acorns;
}
Code: Select all
void Start()
{
GH = FindObjectOfType<GameHandler>();
}
Code: Select all
private void OnEnable()
{
GH = FindObjectOfType<GameHandler>();
Lua.RegisterFunction("GiveAcorns", this, SymbolExtensions.GetMethodInfo(() => GiveAcorns((double)0)));
}
public void GiveAcorns(double amount)
{
GH.AddCoins((int)amount);
}
Re: Reward System
Thank you for your reply, it was really helpful and it works once the quest is completed but the issue now is that the Acorns in the open world do not count towards the score, it only seems to be the Acorns for the quest completion
Re: Reward System
You need to call AddCoins in that script, too:
Code: Select all
private void OnTriggerEnter(Collider other)
{
GH.AddCoins(AcornValue);