[HOWTO] How To: Extend Surface Data

Announcements, support questions, and discussion forum for Grid Controller.
Post Reply
User avatar
Tony Li
Posts: 22286
Joined: Thu Jul 18, 2013 1:27 pm

[HOWTO] How To: Extend Surface Data

Post 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);
    }
}
Arctichorse9
Posts: 44
Joined: Wed Sep 25, 2024 11:26 pm
Location: New York City
Contact:

Re: [HOWTO] How To: Extend Surface Data

Post 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?
User avatar
Tony Li
Posts: 22286
Joined: Thu Jul 18, 2013 1:27 pm

Re: [HOWTO] How To: Extend Surface Data

Post 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);
    }
}
Arctichorse9
Posts: 44
Joined: Wed Sep 25, 2024 11:26 pm
Location: New York City
Contact:

Re: [HOWTO] How To: Extend Surface Data

Post 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.
User avatar
Tony Li
Posts: 22286
Joined: Thu Jul 18, 2013 1:27 pm

Re: [HOWTO] How To: Extend Surface Data

Post 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.
Arctichorse9
Posts: 44
Joined: Wed Sep 25, 2024 11:26 pm
Location: New York City
Contact:

Re: [HOWTO] How To: Extend Surface Data

Post by Arctichorse9 »

Thank you!
Post Reply