[solved] Syntax for assigning class<whatever>-variables

Discussions about Coding and Scripting
User avatar
Barbie
Godlike
Posts: 2792
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

[solved] Syntax for assigning class<whatever>-variables

Post by Barbie »

I want to assign a variable of type "class<whatever>" with a constant or variable, but I don't know the syntax. It's best explained by an example:

Code: Select all

class Example extends Actor;

var class<ammo> AmmoName;


function ThisWorks() {
	AmmoName = Class'UnrealI.FlakBox';
}

function HowToMakeThisWork() {
local name MyAmmo, MyClass;
const CMyAmmo = 'FlakBox';

	MyClass = 'UnrealI';
	MyAmmo = 'FlakBox';
	// How can I assign either *CMyAmmo* or *MyAmmo* to *AmmoName*?
	AmmoName = ???
}
Last edited by Barbie on Tue Jan 05, 2016 11:53 pm, edited 1 time in total.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
Wormbo
Adept
Posts: 258
Joined: Sat Aug 24, 2013 6:04 pm
Contact:

Re: Syntax for assigning class<whatever>-variables

Post by Wormbo »

Class literals are object references. Apart from the null reference None, there's no way to put them into a constant. You might put the object's string representation into the constant instead and dynamically load the object at runtime. The string representaiton always needs to include the package name, e.g. "UnrealI.FlakBox". (UnrealI is the package name, FlakBox the class name)
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: Syntax for assigning class<whatever>-variables

Post by JackGriffin »

When you assign default values like this it (AFAIK) can't be a variable. The engine will not replace values in this instance, instead it will literally assign
AmmoName = MyAmmo
and MyAmmo will not be replaced as a variable.

You might be able to craft a Set command that would work here but it's messy. May I ask what you are trying to accomplish? There's probably a better way.

(Edit: NVM me, Wormbo answered as I was replying.)
So long, and thanks for all the fish
User avatar
Barbie
Godlike
Posts: 2792
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Syntax for assigning class<whatever>-variables

Post by Barbie »

Thanks for replies so far.
JackGriffin wrote:May I ask what you are trying to accomplish?
Of course: I want to keep the source code clear. In other programming languages I define specific values at the beginning of the code, then put a line -------No changes needed below------- and only use predefined constants or variables but no literals below that line.
It has the advantage if I (or someone else) want to change something, the change has to be done at one place only.

It seems that I have to take a look at an UCC precompiler.
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: Syntax for assigning class<whatever>-variables

Post by JackGriffin »

Kudos for not only allowing sub usage but making it easier to do. We need more developers that do that.
So long, and thanks for all the fish
User avatar
sektor2111
Godlike
Posts: 6403
Joined: Sun May 09, 2010 6:15 pm
Location: On the roof.

Re: Syntax for assigning class<whatever>-variables

Post by sektor2111 »

Again more maths and I'm guessing again simple is better.

As I well recall "quadshot"-s story, I was respecting rules granted in original. However at replacing an OLquadshot (Yes I'm replacing that too) I took in account (a little later indeed) that my replacement needs to hook New ammo. Whoever was "fixing" that OL... (bad A.I. code) he has changed FlakAmmo with SheelBox (I cannot answer why - some head-lulu perhaps). My clone will have that too by changing properties on fly right when spawn during replacement process. I don't get what is so hard.
Solution 1) - for me works in "function bool ReplaceWithOther"

Code: Select all

...
//all stories goes here
//then latest checks
			if ( O.IsA('OLquadshot') && MHquadshot(Alt) != None )
				Alt.SetPropertyText("AmmoName",O.GetPropertyText("AmmoName"));
Solution 2) Comes addressing AmmoName directly else "Default.AmmoName" or both (timings issues ? - TimeDilation, States ) - default I added at cloning armors, it was something nasty at Suits so I had to attack defaults.
In random moments of life UScript has an advantage of simple things doable. No worries I the past I've missed these classes.
____
Edit: - According to http://wiki.beyondunreal.com/Legacy:QueenDest
I don't understand explanations without explanations.
Some people forgot to explain what happens when 2 or 3 Queens are teleporting into the same QueenDest and are counted. Level become a lottery as long as Queens are vanished like, they won't perform their Event any more and will mess up opening whatever doors, firing a Factory, triggering something (new PlayerStart, Teleport to next zone, MonsterEnd for MH) and so on... having all chances to mess up Level. After 16 Years of UT I don't see anyone explaining to these lost mappers from whatever location "utmappers" forum board claiming themselves mappers what exactly is a need to avoid in order to not have a rammed Level when we speak about UnrealI creatures or they need new glasses to read some tutorials because ON-Line they seems to not learn nothing. I won't go Off-Topic with CreatureFactory setup...
User avatar
Barbie
Godlike
Posts: 2792
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Syntax for assigning class<whatever>-variables

Post by Barbie »

sektor2111 wrote:SetPropertyText
Thanks for the hint, I have already used that as a fall-back. In general I try to avoid this function, because it shifts string parsing and type and value range checks from compile time into multiple run time executions and therefore it must be inefficient.
sektor2111 wrote:Legacy:QueenDest
I cannot clearly see what you mean here. Recently I added the remarks there. Is something wrong with that description or is it incomplete? Feel free to improve content. :D
Barbie wrote:It seems that I have to take a look at an UCC precompiler.
I had a look at "UCC Helper" and another wrapper tool but I did not find anything about their ability preprocessing files. Has anybody experiences with UE1PreProcessorCommandlet or can recommend a preprocessor for UCC?
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
Wormbo
Adept
Posts: 258
Joined: Sat Aug 24, 2013 6:04 pm
Contact:

Re: Syntax for assigning class<whatever>-variables

Post by Wormbo »

There's really notrhing wrong with storing class references in default properties. That's much more flexible than storing or infering them in code, as it won't require recompiling.
Chris
Experienced
Posts: 134
Joined: Mon Nov 24, 2014 9:27 am

Re: Syntax for assigning class<whatever>-variables

Post by Chris »

Theoretically it would be possible to have a pure constant contain a Class literal since;
const MYCLASS = class'someclass';

SomeFunc(MYCLASS); is just evaluated to SomeFunc(class'someclass'); at the parsing stage, before the class'someclass' is even loaded. Isn't that right?
Doesn't seem to be implemented in UScript tho.

That PreProcessor seem to be a good alternative, simply using the "namespace()" or a similar directive.
User avatar
Barbie
Godlike
Posts: 2792
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Syntax for assigning class<whatever>-variables

Post by Barbie »

Chris wrote:const MYCLASS = class'someclass';
Yes, this works, but with this I cannot use the class name only (example: Ammo.IsA('someclass')). I have to use two constants with someclass in it then, and that is the thing I want to avoid. Sadly it is not possible to use a constant in a constant definition (or I didn't figure that syntax out).
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
User avatar
Wormbo
Adept
Posts: 258
Joined: Sat Aug 24, 2013 6:04 pm
Contact:

Re: Syntax for assigning class<whatever>-variables

Post by Wormbo »

Since class literals are object references, you can simply get their name via MYCLASS.Name. Alternatively (and probably more rubust) you could instead do ClassIsChildOf(Ammo.Class, MYCLASS). You can also validate the constant itself that way, e.g. with ClassIsChildOf(MYCLASS, class'Inventory').
User avatar
Barbie
Godlike
Posts: 2792
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: Syntax for assigning class<whatever>-variables

Post by Barbie »

Wormbo wrote:MYCLASS.Name
That was the key! Thank you. :thuup:

As a reference for later readers some examples:

Code: Select all

const CProjectPrefix = "MyProject.";
const CNewAmmoClass = class'SomeAmmo';


// Example for direct assignment:
SomeWeapon.AmmoName = CNewAmmoClass;

// Example for usage in "IsA":
SomeAmmo.IsA(CNewAmmoClass.Name)

// Example for usage with "ReplaceWith":
ReplaceWith(Other, CProjectPrefix $ CNewAmmoClass.Name);
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
JackGriffin
Godlike
Posts: 3774
Joined: Fri Jan 14, 2011 1:53 pm
Personal rank: -Retired-

Re: [solved] Syntax for assigning class<whatever>-variables

Post by JackGriffin »

Code: Select all

ReplaceWith(Other, CProjectPrefix $ CNewAmmoClass.Name);
You tested this as working? If so, that's pretty neat. I'd have bet it wouldn't work.
So long, and thanks for all the fish
User avatar
Barbie
Godlike
Posts: 2792
Joined: Fri Sep 25, 2015 9:01 pm
Location: moved without proper hashing

Re: [solved] Syntax for assigning class<whatever>-variables

Post by Barbie »

JackGriffin wrote:

Code: Select all

ReplaceWith(Other, CProjectPrefix $ CNewAmmoClass.Name);
You tested this as working? [...]
Yep, tested and working. (It would be embarrassing to post a solution that won't work.)
"Multiple exclamation marks," he went on, shaking his head, "are a sure sign of a diseased mind." --Terry Pratchett
Post Reply