Assign ClassA.struct.enum value to ClassB.struct.enum

Discussions about Coding and Scripting
Post Reply
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Assign ClassA.struct.enum value to ClassB.struct.enum

Post by PrinceOfFunky »

I have this code line:

Code: Select all

module.FULL_VERSION.Build = default.FULL_VERSION.Build;
where FULL_VERSION is the variable of type "struct" written like this:

Code: Select all

struct FullVersion
{
	var float Version;
	var EBuild Build;
	var float Revision;
};
and "Build" variable inside that struct is of the type "EBuild" which is an enum written like this:

Code: Select all

enum EBuild
{
	BUILD_ALPHA,
	BUILD_BETA,
	BUILD_PRERELEASE,
	BUILD_RELEASE,
};
"module" variable, from the line of code I wrote up is a class which contains the same struct and enum of the class where I'm performing the assignment.
When I try to compile it, I get this error:

Code: Select all

Error, Type mismatch in '='
Does it mean that the enums values cannot be assigned between classes even if these classes contain the same enum?
"Your stuff is known to be buggy and unfinished/not properly tested"
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by PrinceOfFunky »

Wormbo wrote:Declarations of module and FULL_VERSION, please.

Code: Select all

class CommandSystemModule extends Actor;

enum EBuild
{
	BUILD_ALPHA,
	BUILD_BETA,
	BUILD_PRERELEASE,
	BUILD_RELEASE,
};

//major.minor[.build[.revision]]
struct FullVersion
{
	var float Version;
	var EBuild Build;
	var float Revision;
};

var FullVersion FULL_VERSION;

Code: Select all

class CommandSystem extends Mutator
	config(CommandSystem);

enum EBuild
{
	BUILD_ALPHA,
	BUILD_BETA,
	BUILD_PRERELEASE,
	BUILD_RELEASE,
};
	
//major.minor[.build[.revision]]
struct FullVersion
{
	var float Version;
	var EBuild Build;
	var float Revision;
};

var FullVersion FULL_VERSION;
"Your stuff is known to be buggy and unfinished/not properly tested"
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by Barbie »

even if these classes contain the same enum
In my understanding you have defined two enumeration that have accidentally the same name but in fact are different for the compiler. I'd try commenting out one of both.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by PrinceOfFunky »

Barbie wrote:
even if these classes contain the same enum
In my understanding you have defined two enumeration that have accidentally the same name but in fact are different for the compiler. I'd try commenting out one of both.
Nu, I didn't create them accidentally with the same name. You can't use the struct or enum from a class into another class but, since I needed to compare the "apparently same" struct and enum of two different classes, I needed to declare them on both classes.

EDIT: I tried solving removing all struct and enum declarations. I created a class which acts like an enum:

Code: Select all

class BuildInfo extends Info;

const BUILD_ALPHA = 0;
const BUILD_BETA = 1;
const BUILD_PRERELEASE = 2;
const BUILD_RELEASE = 3;
And I put this piece of code on any other class:

Code: Select all

var float VERSION;
var int BUILD;
var float REVISION;
Default properties of these classes are like these:

Code: Select all

defaultproperties
{
    VERSION=0.8;
    BUILD=BuildInfo.BUILD_BETA;
    REVISION=1;
}
So I changed the assignment from this:

Code: Select all

module.FULL_VERSION.Build = default.FULL_VERSION.Build;
to this:

Code: Select all

module.BUILD = default.BUILD;
Now the probem is, that if I log "module.FULL_VERSION.Build" I get a "0" whatever the value is.

EDIT 2: Oh no -.-
From Unreal Wiki:

Code: Select all

Starting with Unreal Engine 3 you can access constants from other classes by using the const keyword like shown below.
EDIT 3: Ok I modified the BuildInfo class as follow:

Code: Select all

class BuildInfo extends Info;

var const int BUILD_ALPHA;
var const int BUILD_BETA;
var const int BUILD_PRERELEASE;
var const int BUILD_RELEASE;
Unreal Wiki says, about variable modifiers:

Code: Select all

Const
    Prevents any changes to this variable from within UnrealScript. You are still allowed to specify a value in the defaultproperties block and access the variable's value in your code.
And so I set them into the default properties as follow:

Code: Select all

defaultproperties
{
	BUILD_ALPHA=0;
	BUILD_BETA=1;
	BUILD_PRERELEASE=2;
	BUILD_RELEASE=3;
}
Rest of the code didn't change, but it still gives me a "0" instead of the value set into the default properties of the other classes for the variable "BUILD". For example:

Code: Select all

BUILD=BuildInfo.BUILD_BETA;
Even if I write it this way:

Code: Select all

BUILD=BuildInfo.default.BUILD_BETA;
I still get a "0"...
"Your stuff is known to be buggy and unfinished/not properly tested"
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by Barbie »

I found out using project wide constants is not possible in UScript.
If I guess right, you want to have your version number accessible from every class. I have solved that as follows, maybe it helps you:
BarbiesWorldVersion.uc wrote:

Code: Select all

class BarbiesWorldVersion extends Object; 
static function int GetBarbiesWorldVersion() { 
	return 2152; 
}
Then it is accessible from every class with

Code: Select all

class'BarbiesWorldVersion'.static.GetBarbiesWorldVersion();
(The file "BarbiesWorldVersion.uc" is automatically created by "make.cmd", and the number is incremented with every successful build.)
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by PrinceOfFunky »

Barbie wrote:I found out using project wide constants is not possible in UScript.
If I guess right, you want to have your version number accessible from every class. I have solved that as follows, maybe it helps you:
BarbiesWorldVersion.uc wrote:

Code: Select all

class BarbiesWorldVersion extends Object; 
static function int GetBarbiesWorldVersion() { 
	return 2152; 
}
Then it is accessible from every class with

Code: Select all

class'BarbiesWorldVersion'.static.GetBarbiesWorldVersion();
(The file "BarbiesWorldVersion.uc" is automatically created by "make.cmd", and the number is incremented with every successful build.)
Thank you, does this mean, if I want to use this solution, I should declare this function into any class?
"Your stuff is known to be buggy and unfinished/not properly tested"
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by Barbie »

PrinceOfFunky wrote:Thank you, does this mean, if I want to use this solution, I should declare this function into any class?
No, it is defined only once in one small file, here »BarbiesWorldVersion.uc«. But you can call that function GetBarbiesWorldVersion() in every class of that project (package) then. (Even access from other packages should be possible, but I didn't try that yet.)
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by PrinceOfFunky »

Barbie wrote:
PrinceOfFunky wrote:Thank you, does this mean, if I want to use this solution, I should declare this function into any class?
No, it is defined only once in one small file, here »BarbiesWorldVersion.uc«. But you can call that function GetBarbiesWorldVersion() in every class of that project (package) then. (Even access from other packages should be possible, but I didn't try that yet.)
That would return the version you defined in that file, but what if I have multiple files and I want define a version for any file?
"Your stuff is known to be buggy and unfinished/not properly tested"
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by Barbie »

PrinceOfFunky wrote:what if I have multiple files and I want define a version for any file?
Then of course you have to define then version info for every class that should support that. You can do that either by functions or variables.

Example (tested) with variables:

Code: Select all

Class Abel extends Actor;

var int Version;

defaultproperties {
	Version=20160630
}

Code: Select all

Class Bebel extends Actor;

var int Version;

defaultproperties {
	Version=20160701
}

Code: Select all

Class MainStuff extends Actor;

function LogVersions() {
	log("Class Abel has version" @ class'Abel'.default.Version);
	log("Class Bebel has version" @ class'Bebel'.default.Version);
}
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by PrinceOfFunky »

Barbie wrote:
PrinceOfFunky wrote:what if I have multiple files and I want define a version for any file?
Then of course you have to define then version info for every class that should support that. You can do that either by functions or variables.
I always try to optimize the code :/ I have this problem about using "const" variables of another class because I don't want people to be forced to use numbers instead of variable names to specify their build type. I made a class to store these "const" variables to let people use the name of these variables to set their build type in the defaultproperties of their own class.
So I tried writing that "BuildInfo" class with const variables and then use the value of one of these const variables for the build type of another class into its defaultproperties like this:

Code: Select all

BUILD=class'BuildInfo'.BUILD_BETA;
It said that "Class" doesn't have BUILD_BETA property, so I used default keyword:

Code: Select all

BUILD=class'BuildInfo'.default.BUILD_BETA;
It still gives a "0" even if "BUILD_BETA" default value is "1".
If I call

Code: Select all

class'BuildInfo'.default.BUILD_BETA;
within a function at runtime, I get the correct value (1), so it seems like the you can't set a value into the defaultproperties, taking it from the default value of another class.

EDIT: Maybe because, if I'm compiling the "BuildInfo" class while compiling the other classes, since the default properties are set within the compilation, when a class gets compiled before of "BuildInfo", the default value of "BuildInfo" didn't exist yet, and that's why it returns 0.
EDIT 2: That last edit was a bs because I noticed there's a class that gets compiled after "BuildInfo" class but that still has its Build default value set as "0", even if I set it to "class'BuildInfo'.BUILD_BETA;"
"Your stuff is known to be buggy and unfinished/not properly tested"
User avatar
Barbie
Godlike
Posts: 2808
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by Barbie »

PrinceOfFunky wrote:so it seems like the you can't set a value into the defaultproperties, taking it from the default value of another class.
Wormbos statement comes into my mind here: The defaultproperties block is not even UnrealScript code, but T3D content (source)
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
PrinceOfFunky
Godlike
Posts: 1200
Joined: Mon Aug 31, 2015 10:31 pm

Re: Assign ClassA.struct.enum value to ClassB.struct.enum

Post by PrinceOfFunky »

Barbie wrote:
PrinceOfFunky wrote:so it seems like the you can't set a value into the defaultproperties, taking it from the default value of another class.
Wormbos statement comes into my mind here: The defaultproperties block is not even UnrealScript code, but T3D content (source)
Oh well, didn't know it...
"Your stuff is known to be buggy and unfinished/not properly tested"
Post Reply