Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Looking for some thoughts about how to set up a pseudoconstructor, sharing my thoughts and what I've noticed (Instantiate only clones public fields, for example)
Post Flair (click to view more posts with a particular flair)
Post Body

I have a prefab called TileBehaviour which is used to create a grid of tiles. Originally each TileBehaviour accessed a singleton to get row and column info. I decided I wanted to make some kind of constructor. I called it "UnityConstructor." I've been discovering what works and doesn't work.

Here is the code that I use to create the TileBehaviours, after dragging in the prefab to a GameTracker object:

            var tile = TileObj.GetComponent<TileBehaviour>();
            tile.UnityConstruct( columnCount, rowCount,"Hello World" );

meanwhile, in TileBehaviour.cs, here are some fields and properties I tried experimenting with

//this must be public, if it's private, it won't be set
public string testString = "unset";
public string TestString { get => testString; private set => TestString = value; }

//this will not be changed by the UnityConstructor method, because it is a property.  Rather, it won't be cloned by Instantiate.
public bool PseudoConstructRan { get; set; }

public object UnityConstruct(int column, int row, string testString )
{
    //set Row, Column and Point
    Row = row;
    Column = column;
    Point = new IntPoint2( Row, Column );
    this.testString = testString;

    TileId = GameState.Instance.TileCount  ;

   ///some positioning code removed from here

    PseudoConstructRan = true;
    if( PseudoConstructRan )
    {
        print( "PseudoConstructRan" );
    }

    //Instantiate, store and return:
    //Instantiate
    var Instantiation = Instantiate( this );
    if( Instantiation.PseudoConstructRan )
    {
        print( "Instantiation has desired qualities" );
    }
    else
    {
        Debug.LogWarning( "Intantiation does not have desired qualities!" );
        print( $"Row set to {Row}, Instantiation.Row: {Instantiation.Row}" );
        print( $"Test string set to {this.testString}, Instantiation.TestString: {Instantiation.TestString}" );
    }

    //store
    TileCollection.Instance.AddTile( Instantiation );

    //return
    return Instantiation;
}

results: Instantiation.PseudoConstructRan is false (new set value is not cloned); in the console, I get these custom warnings:

Intantiation does not have desired qualities!

Row set to 40, Instantiation.Row: 40

Test string set to Hello World, Instantiation.TestString: Hello World

If I set testString to private, Instantiation.TestString remains "unset"

Anyway, my first thought on going through this is: I want to use custom constructors, but if I accidentally try to edit a property in one, it may be very hard to find the bug later. So, perhaps I should just not use properties. Writing properties is nice and all, but I'm used to just doing method calls from JavaScript anyway.

Anyway, I'm happy to hear any of your thoughts about how you made your pseudoconstructors. I was looking up about using the factory pattern in Unity, but that doesn't directly address this. Any info on nomenclature would also be great.

Author
Account Strength
100%
Account Age
15 years
Verified Email
Yes
Verified Flair
No
Total Karma
635,608
Link Karma
107,612
Comment Karma
524,749
Profile updated: 4 days ago
Posts updated: 2 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago