What is the class type for
Data members, unlike temporary variables, cannot change the type that you declare for them. This ensures that the type of the data member can be relied upon to be as was declared regardless of what routine it is used in. So you can set it to that type or any compatible type such as a subclass.
If its type is
Pawn for example, then you can only set it to some other
There are a few options available.
If you only need to change it within a given routine then you could set a temporary variable to it and then change the temporary variable as needed.
!target_enemy: @target_enemy, use it and then later
target_enemy: nil. This is allowed by the compiler since it can see the entire lifetime of the temporary variable so you can change it to whatever class type you want. No change will persist across routine calls though since you won’t be changing
Use a union class such as
<Pawn|None> that allows the possibility of
nil. This has the downside that you may need to test for
nil or cast to use it
@target_enemy<>Pawn. Because unions with the
None class are common you can use the shortcut of just
@target_enemy<> without specifying the type you want - it assumes that the non-nil type is more interesting. Another cool language helper with this case is that you can use the apply operator
% instead of
. on a object that may be
@target_enemy%do_stuff and if it is non-nil it calls
do_stuff() and if it is
nil then it is a no-op and does nothing.
Some objects have an empty or invalid state. For example there can be a null/invalid
Entity object. So rather than setting the data member to
nil you could make the object invalid or set it to an invalid object.
Cheat. Sometimes you just want to do what you want to do and the rules can stuff it. We used to have a method that allowed you to set a data member to whatever type you wanted and circumvent the compiler. For example:
untyped_data_set('@target_enemy', nil). It seems to have been removed a couple of years ago since no one was using it (and it is bad practice). Though sometimes an escape hatch is really what you need so if this is the solution for you, let us know and we can put it back.
If you supply more info about what you are trying to accomplish, we might be able to give other options.