so i ran into some trouble with how i should go about updating my UI by getting a Player’s health and dmg and so on.
also the player class isnt attached to any gameobjects
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BasePlayer {
public BasePlayer(){
}
private string playerName;
private string playerDescription;
private float playerHealth;
private float playerMana;
private float playerCurrentHealth;
private float playerCurrentMana;
private float playerMaxHealth;
private float playerMaxMana;
private BaseCharacterClass.ClassType playerClass;
private int playerLevel;
private int playerStrength;
private int playerIntellect;
private int playerAgility;
private int playerLuck;
private float playerAttackSpeed;
private float playerCastSpeed;
private float playerSpeed;
public string PlayerName { get { return playerName; } set { playerName = value; } }
public string PlayerDescription { get { return playerDescription; } set { playerDescription = value; } }
public float PlayerHealth { get { return playerHealth; } set { playerHealth = value; } }
public float PlayerMana { get { return playerMana; } set { playerMana = value; } }
public float PlayerCurrentHealth { get { return playerCurrentHealth; } set { playerCurrentHealth = value; } }
public float PlayerCurrentMana { get { return playerCurrentMana; } set { playerCurrentMana = value; } }
public float PlayerMaxHealth { get { return playerMaxHealth; } set { playerMaxHealth = value; } }
public float PlayerMaxMana { get { return playerMaxMana; } set { playerMaxMana = value; } }
public BaseCharacterClass.ClassType PlayerClass { get { return playerClass; } set { playerClass = value; } }
public int PlayerLevel { get { return playerLevel; } set { playerLevel = value; } }
public int PlayerStrength { get { return playerStrength; } set { playerStrength = value; } }
public int PlayerIntellect { get { return playerIntellect; } set { playerIntellect = value; } }
public int PlayerAgility { get { return playerAgility; } set { playerAgility = value; } }
public int PlayerLuck { get { return playerLuck; } set { playerLuck = value; } }
public float PlayerAttackSpeed { get { return playerAttackSpeed; } set { playerAttackSpeed = value; } }
public float PlayerCastSpeed { get { return playerCastSpeed; } set { playerCastSpeed = value; } }
public float PlayerSpeed { get { return playerSpeed; } set { playerSpeed = value; } }
}
This class doesn’t inherit from Monobehaviour neither does the BasePlayer which holds all information about stats and name for player,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Player : BasePlayer {
public Player()
{
GameObject go = (GameObject)MonoBehaviour.Instantiate(Resources.Load("Player"),new Vector3(502,20,-608),Quaternion.identity)as GameObject;
GameObject gamescene = (GameObject)MonoBehaviour.Instantiate(Resources.Load("CharacterUI")) as GameObject;
MonoBehaviour.DontDestroyOnLoad (go.transform);
//GameObject go = MonoBehaviour.Instantiate(Resources.Load("MyPrefab")) as GameObject;
LoadInformation.LoadAllInformation ();
PlayerName = GameInformation.playerName;
PlayerLevel = GameInformation.playerLevel;
PlayerHealth = GameInformation.playerHealth;
PlayerMaxHealth = GameInformation.playerMaxHealth;
PlayerCurrentHealth = GameInformation.playerCurrentHealth;
PlayerMana = GameInformation.playerMana;
PlayerStrength = GameInformation.playerStrength;
PlayerIntellect = GameInformation.playerIntellect;
PlayerAgility = GameInformation.playerAgility;
PlayerLuck = GameInformation.playerLuck;
PlayerAttackSpeed = GameInformation.playerAttackSpeed;
PlayerCastSpeed = GameInformation.playerCastSpeed;
PlayerSpeed = GameInformation.playerSpeed;
Debug.Log ("PlayerHealth " + PlayerCurrentHealth.ToString());
}
public void DealDamage(float dmgvalue){
PlayerCurrentHealth -= dmgvalue;
updatehealth ();
}
public void updatehealth(){
Debug.Log ("Event was Fired");
EventManager.updatehealthevent += CalculateHealth;
}
public float CalculateHealth(){
EventManager.updatehealthevent -= CalculateHealth;
return PlayerCurrentHealth / PlayerMaxHealth;
}
}
this is my PlayerUI script that is currently attached to a Canvas
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerUI : MonoBehaviour {
public static PlayerUI _Instance { get; private set;}
public Canvas canvasui;
public Slider healthbar;
public Slider manabar;
public Text healthtext;
public Text manatext;
// Use this for initialization
void Awake () {
if (_Instance == null) {
DontDestroyOnLoad(this);
_Instance = this;
}
else {
DestroyImmediate(this);
}
}
void Start(){
}
// Update is called once per frame
void Update () {
i want to somehow access my playerdata here so i can update the GUI and display correctly.
}
void FixedUpdate(){
}
}
To answer your main question, you can simply say Player myPlayer = new Player();
And if you need to, you can reference it in a singleton or as a static object.
BUT
Let’s take a step back and look at this monstrosity:
If you’ve got long blocks of tedious code like this, then your data structure design needs an overhaul. Especially because you’re likely to be adding to this list of data about the player as development continues, and everything you add, you’re adding to at least four (and I suspect five or more) different places in the code.
I’d have to see what’s in GameInformation to know the best way to resolve this, but I suspect that you should probably have GameInformation have a Player object (that you simply either reference or clone), rather than every individual static variable that later gets copied over to the object. Bonus: if GameInformation has a static Player object, this will give you an easy answer to your original question!
Another solution might be to take advantage of serialization (JsonUtility is wonderful for this sort of thing), which would also allow you to easily save game progress, assuming these player attributes can change over the course of the game.
only using it for saving information once. and a place to hold information.
also i cannot use New keyword as that would make a new player which would instantiate it again?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameInformation : MonoBehaviour {
void Awake()
{
DontDestroyOnLoad(transform.gameObject);
}
public static string playerName;
public static string playerDescription;
public static float playerHealth;
public static float playerMana;
public static float playerCurrentHealth;
public static float playerCurrentMana;
public static float playerMaxHealth;
public static float playerMaxMana;
public static BaseCharacterClass.ClassType playerClass;
public static int playerLevel;
public static int playerStrength;
public static int playerIntellect;
public static int playerAgility;
public static int playerLuck;
public static float playerAttackSpeed;
public static float playerCastSpeed;
public static float playerSpeed;
}
i create player here. so i can’t really use new keyword again?
Okay, so yes, my recommendation is to wipe out all those static members from the GameInformation class, and replace it with a “public static Player player;” line. Then you can simply remove that block of code I quoted from the Player class, and anytime you need this data, replace
Including what I’m guessing is in LoadInformation.LoadAllInformation() - it can put that information directly into the Player class the same way. And you can use GameInformation.player to answer your original question of how to access it without creating a new instance of it.
[quote=StarManta, post
: 3003327, member: 1195]
Okay, so yes, my recommendation is to wipe out all those static members from the GameInformation class, and replace it with a “public static Player player;” line. Then you can simply remove that block of code I quoted from the Player class, and anytime you need this data, replace
i also have followed the same tutorial as simonlvschal, and i am in the same situation.
could you explain why they are creating a base player, a a game information to store everything (that is static) and also how to and why it is better to use your solution ? ^^ thanks you !