- Home /
Grabbing a value using a string in the reference
The probable reason for me not being able to find the answer when searching is my lack of knowledge when it comes to coding terms and expressions.
public Vital Strength;
Declaring a Vital called Strength. All good so far.
Strength = new Vital(5, "Strength");
Here I am creating the Strength vital by calling upon the function Vital, passing the integer 5, and the string "Strenght" as arguments.
public Vital(int _valueMod, string _statName)
{
currentValue = Player.current._statName.currentValue * _valueMod;
maxValue = Player.current._statName.currentValue * _valueMod;
}
Here is where it (obviously) does not work. My Player class has no declaration for _vitalName(because it wouldn't do anything, I can't grab the currentValue out from a string).
I want to grab a currentValue from a Stat, using not the direct reference to the Stat(as this would not make it useable for more than 1 stat as I would have to manually type the name of a Vital) but a reference that can be changed in the Call arguments.
Is this possible?
Answer by Hellium · Jul 16, 2015 at 07:23 AM
A "simple" way to do what you want is to use a System.Collections.Generic.Dictionary<string,Vital>
. The, you could use the following syntax :
Player.current[_vitalName].currentValue
See https://msdn.microsoft.com/en-us/library/xfhwa508.aspx
I think that the concept of reflexion could help you but I absolutely don't know how to use it.
Well I tinkered for some hours with your solution but couldn't come up with anything that worked.
$$anonymous$$y current efforts is making a list
public List<Stat> Stats = new List<Stat>();
and inside the Stat constructor I have
public Stat(string StatName)
{
_statName = StatName;
currentValue = 1;
Player.current.Stats.Add(this);
}
and for the Vital constructor I have
public Vital(int _value$$anonymous$$od, string vitalName)
{
foreach(Stat s in Stats)
{
if (s._statName == vitalName)
{
currentValue = s.currentValue * _value$$anonymous$$od;
}
}
}
I get a nullreferenceException at "Player.current.Stats.Add(this);" however when trying to do this.
I guess the error doesn't tell you where it occured ?
How many objects are listed in Stats ? Do you instantiate several Vitals ? If yes, how many ?
Try to do a simple for loop
ins$$anonymous$$d of a foreach.
I'll link the entirety of the script for you to see: every class is [System.Serializable]
Player class:
public class Player
{
public static Player current;
public Vital Health;
public Stat Strength;
public List<Stat> Stats = new List<Stat>();
public Player()
{
_name = "Name";
Strength = new Stat("Strength");
Health = new Vital(5, Strength._statName);
}
}
Stat class:
public class Stat
{
public int currentValue;
public string _statName;
public Stat(string StatName)
{
_statName = StatName;
currentValue = 1;
Player.current.Stats.Add(this);
}
}
Vital class:
public class Vital
{
public int currentValue;
public int maxValue;
public Vital(int _value$$anonymous$$od, string vitalName)
{
foreach(Stat s in Stats)
{
if (s._statName == vitalName)
{
currentValue = s.currentValue * _value$$anonymous$$od;
}
}
}
}
Th nullreferanceexception error happens at lines:
Strength = new Stat("Strength");
Player.current.Stats.Add(this);
I'm kinda suspecting as I write this that I simply forgot to actually save the Player off somewhere for it to be referanceable?
I think that's because your static Player is never instantiated. Thus, current
doesn't exist, so does the list. The default constructor is not called.
It seems you are trying to implement a kind of Singleton but you have forgotten many things.
Add something like this in your player class :
using System;
public class Player
{
private static Player current;
public static Player Current
{
get
{
if (current== null)
{
current = new Player();
}
return current;
}
}
}
Source : https://msdn.microsoft.com/en-us/library/ff650316.aspx
Then, you will be able to use your Player from anywhere.
Where do you set the static current
field? You need to set that in the Player
constructor.