Page 1 of 1

[HOWTO] How To: Extend Surface Data

Posted: Sat Mar 30, 2024 5:03 pm
by Tony Li
Grid Controller detects Surfaces on floors, walls, and ceilings.

Version 1.0.10 adds a GridController.CurrentFloorSurface property that returns the Surface under the player's feet.

In version 1.0.9 and earlier, you can make a subclass of GridController and override GetSurfaceUnderPosition to get the Surface, something like:

Code: Select all

public class MyGridController : GridController
{
    public Surface CurrentFloorSurface {get; set;}
    
    protected override Surface GetSurfaceUnderPosition(Vector3 position, out Vector3 hitPoint)
    {
        CurrentFloorSurface = base.GetSurfaceUnderPosition(position, out hitPoint);
        return CurrentFloorSurface;
    }
}
(I just typed that in here; there could be typos, etc.)

Version 1.0.10 is coming out on Monday (as of this post), so it might be simpler to just wait for it.

Whether you use 1.0.9 or 1.0.10, you can make a subclass of Surface, or a subclass of the SurfaceType ScriptableObject asset class, to add your own data. For example, if you want to damage the player when they step on certain squares, you could make a subclass of SurfaceType:

Code: Select all

[CreateAssetMenu]
public class DamagingSurfaceType : AudioSurfaceType
{
    public int damage = 0;
}
Create ScriptableObject assets of this type and assign them to Surfaces. Then you can hook into GridController's EnteredPosition event:

Code: Select all

GridController.Instance..EnteredPosition += CheckSurfaceOnEnteredPosition;
...
void CheckSurfaceOnEnteredPosition(Vector3 position)
{
    if (GridController.Instance.CurrentFloorSurface.SurfaceType is DamagingSurfaceType damagingSurfaceType)
    {
        TakeDamage(damagingSurfaceType.damage);
    }
}

Re: [HOWTO] How To: Extend Surface Data

Posted: Thu Dec 19, 2024 8:25 am
by Arctichorse9
This is very useful. What is the best way to use this and trigger a message from the Dialogue System that you've sustained damage? Just add a Dialogue trigger?

Re: [HOWTO] How To: Extend Surface Data

Posted: Thu Dec 19, 2024 9:00 am
by Tony Li
Hi,

Grid Controller doesn't have a damage system. The example code above assumes that you've written a method TakeDamage(int) that works with your damage system. You could add the alert message to your TakeDamage(int) method, or you could show the alert outside of that message by adding it to CheckSurfaceOnEnteredPosition:

Code: Select all

void CheckSurfaceOnEnteredPosition(Vector3 position)
{
    if (GridController.Instance.CurrentFloorSurface.SurfaceType is DamagingSurfaceType damagingSurfaceType)
    {
        string damageAlert = $"You took {damagingSurfaceType.damage} from {damagingSurfaceType.name}";
        PixelCrushers.DialogueSystem.DialogueManager.ShowAlert(damageAlert);
        TakeDamage(damagingSurfaceType.damage);
    }
}

Re: [HOWTO] How To: Extend Surface Data

Posted: Thu Dec 19, 2024 3:53 pm
by Arctichorse9
Thanks so much. I have a damage system and can think of many uses for this. For example, it might push the player back.

How do you identify the surface the player is stepping on? For example, one particular square? By tag? By name? By position? I'm a bit fuzzy on that. Thanks.

Re: [HOWTO] How To: Extend Surface Data

Posted: Thu Dec 19, 2024 4:57 pm
by Tony Li
Hi,

The event provides the player's world space position (e.g., to the CheckSurfaceOnEnteredPosition method in the example above).

The surface type under that position is in the C# property GridController.Instance.CurrentFloorSurface.SurfaceType.

Re: [HOWTO] How To: Extend Surface Data

Posted: Thu Dec 19, 2024 10:07 pm
by Arctichorse9
Thank you!