+1
Need More Information

MarshalDirectiveException when trying to create struct that contains a UnityEngine.Component field

jerome latteux 5 months ago updated by Lazlo Bonin (Lead Developer) 2 weeks ago 3

I was trying to do the same thing as in this post, creating a new constraint source:
https://support.ludiq.io/communities/5/topics/2132-parameterless-struct-constructors-not-available

But in Bolt2 I get the following error message:

MarshalDirectiveException: Type UnityEngine.Component which is passed to unmanaged code must have StructLayout attribute.
(wrapper unknown) UnityEngine.Animations.ConstraintSource.StructureToPtr(object,intptr,bool)

Bolt Version:
2.0.0a5
Unity Version:
2019.3
Platform(s):
Scripting Backend:
.NET Version (API Compatibility Level):
.NET 4.x
Bolt 2 GFI
Need More Information

Hi Jerome,

Thanks for the report and sorry for the very late reply on this.

Could you please show me your graph or give me step-by-step reproduction steps?

This happens to me when I try to work with structs that hold a MonoBehaviour field. (I guess this is the "Type UnityEngine.Component", which is referred to in the error.)

Try to create such struct in the graph, or use a method that uses that struct and the error will appear on play.

MarshalDirectiveException: Type UnityEngine.Component which is passed to unmanaged code must have a StructLayout attribute.
(wrapper unknown) structtest.PtrToStructure(intptr,object)
Ludiq.Cloning.CloneViaMarshalling (System.Object value) (at Assets/Ludiq/Ludiq.Core/Runtime/Cloning/Cloning.cs:245)
Ludiq.Bolt.Flow.AssignLocal (System.Object identifier, Ludiq.Bolt.HybridType type, System.Object value) (at Assets/Ludiq/Bolt.Flow/Runtime/Flow.cs:259)
Ludiq.Bolt.Flow.SetValue (Ludiq.Bolt.ValueOutput output, System.Object value) (at Assets/Ludiq/Bolt.Flow/Runtime/Flow.cs:560)
Ludiq.Bolt.CreateStruct.Enter (Ludiq.Bolt.Flow flow) (at Assets/Ludiq/Bolt.Flow/Runtime/Units/Codebase/CreateStruct.cs:85)
Ludiq.Bolt.Flow.InvokeDelegate (Ludiq.Bolt.ControlInput input) (at Assets/Ludiq/Bolt.Flow/Runtime/Flow.cs:513)
Rethrow as GraphPingException:
Ludiq.Bolt.Flow.InvokeDelegate (Ludiq.Bolt.ControlInput input) (at Assets/Ludiq/Bolt.Flow/Runtime/Flow.cs:517)
Ludiq.Bolt.Flow.Invoke (Ludiq.Bolt.ControlOutput output) (at Assets/Ludiq/Bolt.Flow/Runtime/Flow.cs:408)
Ludiq.Bolt.EventHandlerUnit`1[TArgs].Invoke (Ludiq.Bolt.GraphReference reference, TArgs args) (at Assets/Ludiq/Bolt.Flow/Runtime/Units/Events/EventHandlerUnit.cs:114)
Ludiq.Bolt.EventHandlerUnit`1+<>c__DisplayClass17_0[TArgs].b__0 (TArgs args) (at Assets/Ludiq/Bolt.Flow/Runtime/Units/Events/EventHandlerUnit.cs:73)
Ludiq.Bolt.EventBus.Invoke[TArgs] (Ludiq.Bolt.EventHook hook, TArgs args) (at Assets/Ludiq/Bolt.Core/Runtime/Events/EventBus.cs:153)
UnityEngine.Debug:LogException(Exception, Object)
Ludiq.Bolt.EventBus:Invoke(EventHook, EmptyEventArgs) (at Assets/Ludiq/Bolt.Core/Runtime/Events/EventBus.cs:157)
Ludiq.Bolt.BoltClassShellTrait:InvokeEvent(EventHookID) (at Assets/Ludiq/Bolt.Core/Runtime/Architecture/BoltClassShellTrait.cs:870)
Ludiq.Bolt.BoltUnityClassShellTrait:Start() (at Assets/Ludiq/Bolt.Core/Runtime/Architecture/BoltUnityClassShellTrait.cs:30)
Ludiq.Bolt.BoltComponent:Start() (at Assets/Ludiq/Bolt.Core/Runtime/Architecture/BoltComponent.cs:121)

Hi Bittsu,

Thanks for the additional information.

Can you please show me:

1. The C# code for the struct

2. The graph used to create the struct

Thanks!