Question

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;

}

}
Was it helpful?

Solution 2

I don't yet read your problem in depth. I can only see that you need referenced of mutable page index to DialogFooterPagenum. Is a PageIndex class can solve the problem?

class PageIndex{
    private int index;
    public int Index{get{return index;} 
        set{
            index = value;
            OnPageIndexChange();
        }
    }

    public Action OnPageIndexChange{get;set;} // change this to event-based
}

This way you can keep references to page index.

OTHER TIPS

I can't see any reason to not to store current page index in IDialogFooterText implementations:

interface IDialogFooterText
{
    string GetFooterDisplay();
    int CurrentPageIndex { get; set; }
}

public class DialogBoxFactory
{
    IDialogFooterText _footerText = // ...

    void Start()
    {
        _footerText.CurrentPageIndex += 1;

        Debug.Log( _footerText.GetFooterDisplay() );
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top