Hi,
I’m working on a simple dialogue system where a ‘dialogue’ can have an event that edits player stats / adds items to an inventory etc
I was hoping to use something like (from PlayerStats.cs)
public class Stat {
dynamic value;
}
to manipulate player stats when a dialogue is called
e.g.
void ChangeStat(Stat stat, dynamic value)
{
//Do some type conversion and such then assign
stat.value = value;
}
DialogueTree.DialogueSet[index].dialogueEvent.ChangeStat(Stat.PlayerHealth, 0);
DialogueTree.DialogueSet[index].dialogueEvent.ChangeStat(Stat.PlayerName, "Bob");
It’s pseudo code at the moment as I found out that unity doesn’t support the ‘dynamic’ type
I was wondering how you guys get around not being able to use this?
This issue kinda ruined my plan for the system so far haha
Cheers,
“get around”…
Well I don’t know about other people, but I don’t “get around” it… I don’t design with the idea of dynamics in mind.
Like… maybe there’s something more to your example… like what’s the point of your ‘Stat’ class?
Why can’t ‘PlayerHealth’ just be an int/float?
Why can’t ‘PlayerName’ just be a string?
Why don’t I just say:
Stat.PlayerHealth = 0;
Stat.PlayerName = "Bob";
If PlayerHealth and PlayerName need validation, make them properties…
public class StatContainer
{
private float _health;
public float Health
{
get { return _health; }
set {
_health = Mathf.Clamp(value, 0f, 100f);
}
}
}
Or is there something else I’m missing here?
Thanks for the reply.
My intention was to make the code more dynamic, efficient and easier for someone not too familiar with scripting to be able to create dialogue trees with events without much fuss.
I’d like for the dialogue to be written something like this: (which works well for me bar the event thing)
PR01_Char_04.Character = "Character Name";
PR01_Char_04.Text = "Dialogue Text";
PR01_Char_04.WaveFile = "/Resources/Audio/Dialogue/Char/Prologue/";
PR01_Char_04.Icon = "Sprites/Icons/Character/Char/Dialogue/Char01";
PR01_Char_04.dialogueEvent = DialogueEvent.DialogueEventType.ChangeStat(Stat.PlayerHealth, 10);
To which I thought - It’d be handy if from the one function (ChangeStat) I could store the variable, and some values. Which is why I was hoping to use a ‘dynamic’ variable. Because the only way I could think to achieve this with defined types would potentially require reflection on the script. Unless i’m being dumb and missing a simple solution to this]
Cheers again