Announcement

Collapse
No announcement yet.

How do I get the Unreal editor to use my class's Add() method when adding objects?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How do I get the Unreal editor to use my class's Add() method when adding objects?

    If I have a class which contains a Set() type method and does some validation and sanity checking on any objects added to the class, how do I get the editor to use my Set() method to add objects?


    For example, I have a collection of unit blueprints. They live inside of an army container. Each unit needs to have a reference to the army which contains it. Here is the CPP code which does this:

    Code:
    void AArmy::AddUnit(AUnit* newUnit)
    {
    	AllUnits.Add(newUnit);
    
    	newUnit->AttachToArmy(this);
    }
    The AllUnits is a TArray<AUnit*> variable.

    So, I could make the AllUnits array a publicly accessible variable which can be used within the editor. However, I lose the capability to tell a unit about the army its attached to. It would become something that needs to be done within a "BeginPlay" event within blueprints and that would be annoying and easy to overlook / mess up. I still want less technical users (like my artist) to be able to add units to an army via a super simple editor UI but take care of all of the association logic on the backend. When/if my artist adds a unit to an army, how do I get the object injection to reroute through my validation code instead? Is there a virtual override I can hook into somewhere?

    #2
    I would just do it super simple and add an AArmy* to the AUnit class. When you create the AUnit, you can either just set the AArmy* ptr directly, or make it part of the class constructor (should work post 4.6).
    On BeinPlay or PostInitializeComponents, you can add the AUnit to the AArmy through some function on the AUnit.

    If you plan on doing networking at some point, it is wise to try and hold as much of the logic for an actor on the actor in question, to avoid replication issues etc.
    For this reason, I would put the call of the AttachToArmy() function on the AUnit that will be affected by it, regardless of your original conundrum...
    Last edited by oasf; 12-16-2014, 11:38 PM.

    Comment


      #3
      Yeah, that's going to have to work. It's less than perfect, but it works. I was doing exactly what you're saying and I just needed to think it out. I blame the long hours and the beer.

      Anyways, for those who are curious on the implementation:
      A creature in my game inherits from ACharacter (not sure if this is 100% correct).
      A unit is a collection of creatures (1-255) and can have ranks and files.
      An army is a dynamic collection of units which get instanced at runtime.


      I needed to implement the following in a blueprint using the "Event Begin Play":
      Click image for larger version

Name:	Army.png
Views:	1
Size:	388.2 KB
ID:	1062228

      (hint: this doesn't go in a construction script)

      Comment

      Working...
      X