Is a FunctionPointer Considered Data?

Hi All,
Just curious if a FuntionPointer is considered data in terms of ECS and a ComponentData? I have a NoiseParameters component that currently holds pointers to different noise types and processes. This is for a terrain region and biome system so that different areas can have different noise setups. it all works well at the moment and is pretty quick (all Burst compatible). Is this anti-pattern?

using System.Runtime.InteropServices; using Unity.Burst; using Unity.Entities; using Unity.Mathematics; using static Bodyclock.Core.Mathematics.Noise; namespace Bodyclock.Core.Mathematics { [BurstCompile] [StructLayout(LayoutKind.Sequential)] public struct NoiseParameters : IComponentData { #region PUBLIC FIELDS public double Frequency; public double Amplitude; public int Octaves; public double Lacunarity; public double Persistence; public double2 Offset; public FunctionPointer<NoiseDelegate> NoiseType; public FunctionPointer<ProcessXYDelegate> XYProcess; public FunctionPointer<ProcessZDelegate> ZProcess; public FunctionPointer<PostProcessDelegate> PostProcess; #endregion } }

Some may argue it is an anti-pattern, but if it is fast and maintainable, does it really matter?

One thing you need to watch out for is that FunctionPointer is not serializable, which may limit the utility of it. But if that isn’t a concern for you, I’ve found FunctionPointers to be pretty good at improving parallelism in the job system when employed in the correct situations. And I could definitely see your use case being one of those situations.

The other thing you need to watch out for is if you need a configuration with each function pointer, unless you are already have a good type-erasure scheme for it, you may be better off using ECS archetypes and using components and dynamic buffers as the common output interface for each of your different algorithms.

1 Like

Thanks for the reply. Basically my thoughts confirmed. As I’m someone new to ECS could you perhaps give a quick example of what you mean in the sentence above?

I think this video will introduce the concept better than I can. Evaluate for yourself whether you think the approach is better or not. It isn’t a “best practice”, just “a way” to go about it. https://www.youtube.com/watch?v=W1xnyQI8-O4