Cogito, ergo sum

Legacy talk:Variable Syntax

From Unreal Wiki, The Unreal Engine Documentation Site
Jump to: navigation, search

Discussion[edit]

Foxpaw: I have some variables that I could make private, but I'm curious - is there any advantage to having them private or should I leave them public on the off chance that a subclass might want to use them somewhere down the road? I don't see any reason why they would, but on the other hand there are some functions declared as final that Epic apparently didn't think anyone would want to override, whilst I very much do - so it's possible that sometime someone might want to use this variable for something. Is there a performance gain or anything like that to be had for using private variables?

Mychaeel: That's more a code design question than a purely technical one. While I'm very glad Epic followed a policy of leaving virtually everything public and non-final, my own code makes very frequent use of private or protected variables and functions.

At least during development my intent is to force others working with my code (and myself) to interact with my code only in a clearly defined, controllable fashion instead of messing with the internal state of my objects at will. That allows me to change the internal implementation of my classes without having to worry about what those changes do to others' code as long as I keep the interface true to its specification.

If you want to keep people from accessing variables or functions from outside a class but would still like to let them use those variables and use/overwrite the methods in subclasses, make them protected, not private.

Tarquin: I don't quite understand what "private" does. If I declare variable "Foo" to be private in MyClass, and I have objects A and B of that class, and C of a child class, can B read A's foo but C can't read A's foo? C has a foo variable of its own, with the same restrictions to its child classes?

Mychaeel: First part: Yes. Second part: C can have its own private (or public) variable called "foo," but that variable wouldn't be connected in any way to the "foo" variable from the base class. (Of course C has a piece of memory space allocated that the code in the base class would address as "foo," but the code in the child class doesn't know about the base class's "foo" at all.)


Tarquin: Can a constant be a boolean? eg

const DEFAULT_PROTECT_ARENA = True;

Foxpaw: That seems to work for me. I believe that constants in UnrealScript are closer to #DEFINE in C rather than a variable that can't be changed. (Only for constants, not const vars.) Unfortunately, however, you can't set a constant to be a statement or an expression like you can with a #DEFINE. Booleans should work, though.


Evolution: regarding the noexport modifier: this is only used by the GUIDesigner - it tells the GUIDesigner that this property shouldn't be included in the subobject definition that will be generated when you hit Ctrl+X while you are in design mode and have a control highlighted. The fact that it prevents those properties from appearing in the defaultproperties block for the class when you use batchexport is in fact a bug  :) It's fixed in the first patch, but I completely forgot about exporting classes from UnrealEd! so it will only correctly export those properties if you use batchexport to create the .uc files.


Devi: Is there a way of declaring member variables as static? I was wondering because I wanted to use the singleton model in one of my classes (The singleton model is where you declare a class in such a way that there can only be one instance of it, that instance is usually a static member variable of the class and is accessed via a static method of the class) in C++ I would do it like this:

class MySingleton

{

  private:
     int some_data_or_other;
     static MySingleton my_singleton_instance;
  public:
     inline static MySingleton &GetInstance(void) {return my_singleton_instance;}

};

Then, whenever I needed to get access to the class or it's functionality I can do:

MySingleton::GetInstance().DoSomethingWonderful();

I normally use this technique for big manager classes and such that get referenced all over the place and am wondering how I'd deal with the same kind of situation in UScript...

Wormbo: There are no static variables in UnrealScript. There are default class variables, though. You could store objects in them, but it's not a good idea. You will very likely mess up garbage collection by assigning dynamically generated objects (i.e. not within the editor) to default class variables, because they might still reference the level even after a map change. You should store the reference to your object in another central object that can easily be accessed.

Foxpaw: You can use default variables for the same purpose. In many instances I have done the following:

class SomeClass extends Whatever;
 
var SomeClass Reference;
 
function PreBeginPlay()
{
  default.Reference = Self;
}
 
function Destroyed()
{
  default.Reference = None;
}

However, default properties don't get "cleaned up" in the same way that dynamic references do, so you have to make sure you manually clean up these references like I've done above.

Devi: That seems to make sense, I'll give it a try... For extra protection you may like to use code like this:

function PreBeginPlay()
{
  if (default.Reference!=None)
  {
    if (default.Reference!=Self)
    {
      Log("There's more than one SomeClass class placed in this level, one named: "$Name$" the other: "$default.Reference.Name$"...");
    }
  }
  else
  {
    default.Reference=self;
  }
}
function Destroyed()
{
  if (default.Reference==Self)
  {
    default.Reference=None;
  }
}

This way your code is protected if something accidentally generates a second instance of your singleton class...


Jimboh: Are newly declared variables GUARANTEED to be zero?

Wormbo: Yes, however, only local variables and new array elements can be considered "newly declared".

Skrilax: What string is a 'coerce string'? (for example used in BroadcastMessage function)

Mychaeel: The "coerce" isn't part of the variable type; it tells the compiler to accept non-string arguments at that place as long as they can be auto-typecasted as strings. The Log function also coerces its argument to string, so you can write Log("foo") or Log(123) or even Log(Pawn.Location) without having to bother with writing an explicit typecast.


Juxtapose: I added new information to constants as pertaining to default properties. I haven't investigated the situation further than the information used above (using constants from classes compiled before the current default properties block, e.g.) so if anyone else has run into this, please add what you know. Also, are there any suggestions on where this should go? I linked the default properties page, but maybe all this information needs to be there instead. Should Default_Properties link back to here for use of constants?


MM: Is there a limit to the [length] of a static array? (In Unreal Engine 1) Such as

var string testtxt[1000000]

Tex23BM Is there a way To declare C++ member variables in Unreal? AKA: I want a C++ fstream (not a FileLog).

You can't declare C++ types from UnrealScript. You can, however, declare the variable of a replacement type. For pointers you simply use int up to UT2003 and pointer in UT2004. You'll have to declare your class as noexport and write its native declaration yourself, though. --Wormbo 07:30, 12 August 2008 (UTC)