In this first attempt I'm trying to keep a reference of a variable in DialogBoxFactory by injecting either an int or string to an IDialogFooterText, and call 'GetFooterDisplay()' in my factory which then returns a string. I was simulating a dialog page increment and should receive an updated page index.
This didn't work properly (I then found a forum answer from Eric Lippert that outlined the reason why I couldn't store the ref parameter in the external class' method and tried another route.
My second attempt does in fact work but it seems dirtier since in DialogFooterPagenum I explicitly know the factory type of DialogBoxFactory. What are some other ways I can get the page index without directly referencing the factory instantiating either IDIalogFooterText?
public class DialogBoxFactory
{
public int _currentPageIndex = 0;
private string[] _dialogText = new string[10]; //filled with dialog content
//an example I've left to demonstrate the need for injecting different parameter types in their constructors
//private IDialogFooterText = new DialogFooterText( "ok" );
private IDialogFooterText _footerText = new DialogFooterPagenum( ref _currentPageIndex, dialogText.Length );
void Start(){
_currentPageIndex +=1;
Debug.Log( _footerText.GetFooterDisplay() );
//expecting the log to show "1/10"
}
}
public class DialogFooterPagenum : IDialogFooterText
{
private int _pageIndex;
private int _dialogLength;
public DialogFooterPagenum( ref int p_pageIndex, int p_dialogLength ){
_pageIndex = p_pageIndex;
_dialogLength = p_dialogLength;
}
public string GetFooterDisplay(){
return _pageIndex.ToString() + " / " + _dialogLength;
}
}
SECOND ATTEMPTED VERSION - Reworked since the ref didn't work
public class DialogBoxFactory
{
public int _currentPageIndex = 0;
private string[] _dialogText = new string[10]; //filled with dialog content
//an example I've left to demonstrate the need for injecting different parameter types in their constructors
//private IDialogFooterText = new DialogFooterText( "ok" );
private IDialogFooterText _footerText = new DialogFooterPagenum( this, dialogText.Length );
void Start(){
_currentPageIndex +=1;
Debug.Log( _footerText.GetFooterDisplay() );
//expecting the log to show "1/10"
}
}
public class DialogFooterPagenum : IDialogFooterText
{
private int _dialogLength;
private DialogBoxFactory _factory;
//this works now but is unacceptable since I wish this class to be unaware of the factory instantiating this class.
public DialogFooterPagenum( DialogBoxFactory p_factory , int p_dialogLength ){
_factory = p_factory;
_dialogLength = p_dialogLength;
}
public string GetFooterDisplay(){
return _factory._currentPageIndex.ToString() + " / " + _dialogLength;
}
}
I don't yet read your problem in depth. I can only see that you need referenced of mutable
page index
toDialogFooterPagenum
. Is aPageIndex
class can solve the problem?This way you can keep references to page index.