Using IntPtr (For example, with SetDragDropPayload)
See original GitHub issueHi there! I have a bit of a newbie question to ask since I’m having a hard time finding how people approach this: How should I go about using ImGui.NET methods that take an IntPtr?
For example, ImGui.SetDragDropPayload takes a type string, an IntPtr, and a size. When I ImGui.GetDragDropPayload() I don’t seem to be able to just grab the type that I set (I can only query if it is a certain type)… And I’m not sure how to use IntPtr to represent an object in my game engine.
I feel like there is a fundamental paradigm that I’m not understanding here, as using IntPtr to hold game data seems like a common thing in ImGui.NET. I’ve done some looking into how to represent objects as IntPtr in .Net, but this seems to point me to creating IntPtr for use in WinApi functions and I haven’t been able to get anything working yet or understand how I should be approaching this…
As you can probably tell, I’m also entirely unfamiliar with the C++ Dear ImGui, so this makes things even trickier for me to get my head around. Any thoughts would be appreciated!
Thanks, Allen
Edit:
Here’s an example of how I’m using it right now. As I mentioned, I don’t know how to use the payload propertly, so I’m just using a separate static object to represent what’s currently being dragged. Then checking to see if the payload’s NativePtr is null or not. It seems to work, I think, but is obviously not ideal:
private static unsafe void SubmitTestWindow()
{
ImGui.Begin("Test");
string[] items = new string[] { "hello", "world", "how", "are", "you?" };
for (int i = 0; i < items.Length; i++)
{
ImGui.Button(items[i]);
if (ImGui.BeginDragDropSource())
{
ImGui.Text(items[i]);
ImGui.SetDragDropPayload(typeof(string).FullName, IntPtr.Zero, 0);
draggedItem = items[i];
ImGui.EndDragDropSource();
}
if (ImGui.BeginDragDropTarget())
{
var payload = ImGui.AcceptDragDropPayload(typeof(string).FullName);
if (payload.NativePtr != null)
{
Console.WriteLine("Dropped " + draggedItem + " onto " + items[i]);
draggedItem = null;
}
ImGui.EndDragDropTarget();
}
}
ImGui.End();
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (2 by maintainers)

Top Related StackOverflow Question
@Thraka I’m a bit rusty on ImGui, but I think you can just pass the variable address as the data parameter, like in the example below:
I did it in less than 20 lines. (NOTE: this code is inside the
BeginTabBar->for loop.