question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Creation of properties from managed code

See original GitHub issue

I have a class deriving from Actor, with a constructor that looks like:

public class Card : Actor {
    public Card(string name = null) : base(name) {
      // Set up mesh, material, register events
      AddTag("MyTag");
      SetInt("MeaningOfLife", 42);
      var meaningOfLife = 9001;
      var gotIt = GetInt("MeaningOfLife", ref meaningOfLife);
      Debug.AddOnScreenMessage(1, 3.0f, Color.Red, $"Meaning of life: {gotIt}; {meaningOfLife}");
    }
}

When I play in the editor, it outputs False; 9001 when I’d expect True; 42.

I’ve played around and moved the GetInt and SetInt calls all over, thinking perhaps it was an object lifetime issue, but I can’t seem to get this to ever print out my expectations. Currently I’m instantiating a Card in OnWorldPostBegin(). I’ve also tried GetBool and SetBool and those didn’t work either – I’m guessing this is broken for all of the method pairs.

The AddTag() call seems to work – I can see it when I inspect the Details pane with the Card actor selected.

Is this a bug or am I doing something wrong?

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:25 (15 by maintainers)

github_iconTop GitHub Comments

3reactions
nxrightherecommented, Jul 7, 2021

Yes, the only option is to use workarounds with object IDs, at the moment, since properties is the only intermediate type-safe data storage.

It seems that now we have Data Registries which can be used as an intermediate type-safe data storage. It’s introduced in Unreal Engine 5 and 4.27.0, so I’m going to investigate it.

3reactions
OlsonDevcommented, Nov 3, 2020

Thank you! I just figured that out on my own. 😃

Is this the recommended way to maintain state on a subclassed Actor? I’d rather not make Blueprint classes that are just property bags, if you will. It doesn’t feel like the native C# way of doing things.

For example, I handle OnActorBeginCursorOver(ActorReference actor) and I do actor.ToActor<Card>().SomeProperty and SomeProperty is reset to its default value because you’re calling FormatterServices.GetUninitializedObject(...), and thus the instance you give me isn’t the same one as I originally instantiated. Do I need to maintain a… dictionary of objects by ID and then take the actor you give me and then just look it up and use that? Or should I actually create blueprint instances for each object and dynamically look up/set properties in C++ land each time?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Create an Object by Using Managed Code - Configuration ...
Learn how to create a configuration manager object by using managed code, with included examples and links.
Read more >
Managed Code Tutorial Example
This tutorial reviews the benefits and advantages of using managed code in .NET applications with Progress DataDirect.
Read more >
Python's property(): Add Managed Attributes to Your Classes
In this step-by-step tutorial, you'll learn how to create managed attributes, also known as properties, using Python's property() in your custom classes.
Read more >
Can a managed C++ assembly return an object to C#? - ...
I've created a managed C++ assembly which compiles to a DLL. I can call it, with a parameter, from the C# code. It...
Read more >
Working with a Managed-Code Action in a Suite/Advanced ...
set_Property—This method sets the value of a new property or changes the value of an existing property in the currently running Suite/Advanced UI...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found