Typecasting.. agian.

  • Two Factor Authentication is now available on BeyondUnreal Forums. To configure it, visit your Profile and look for the "Two Step Verification" option on the left side. We can send codes via email (may be slower) or you can set up any TOTP Authenticator app on your phone (Authy, Google Authenticator, etc) to deliver codes. It is highly recommended that you configure this to keep your account safe.

Dark[NSF]

Northwest Secessionalist Forces
For some reason typecasting is a brick wall to me. This is what I got here:

Code:
PlayerController(Other.Controller).NSFWeaponFire(NSFWeapon(Pawn.Weapon).FireMode[0]).variable += variable2;


This is the WeaponFire's function DoTrace. Other is the object the tracer hits. I am trying to access the pawn's playercontroller that gets hit.


Though, I don't think I typecasted it correctly, what did I do wrong?

Thanks for any help
 

Mr Evi1

New Member
Mar 22, 2002
336
0
0
UK
come.to
I think it's a bad idea to do so much casting in one line, as it makes it difficult to read. Also, you will be wanting to check that each of the actors actually exists.

First, how do you know that 'Other' can even have a controller? Only Pawns have controllers, so you need to check that 'Other' is a pawn then check that its controller exists before trying to access it.

You don't need to cast 'Pawn.Weapon' to 'NSFWeapon' since 'FireMode[0]' is declared in 'Weapon' and 'Pawn.Weapon' is a 'Weapon' already.
 

Dryn

New Member
Feb 20, 2003
128
0
0
Visit site
you _really_ have to watch the objects; thats how typcasting works...

Code:
Pawn.Weapon.Blarg

Some variable called Blarg in some object thats in the variable called weapon in some object within the variable called pawn.

Thats a lot of information within those three words. Brackets don't change anything; it just gets bigger and bigger (if you can follow along, I don't mind long typecasting statements provided its all used properly).

Basically you wrote for the variable to be a part of some object which is a _function_ of the playercontroller... That, and the following is how I would dissasemble that line:

if Other.Controller is a valid statement, there is no need to cast this AS a controller, for Controller is a var of Other, and if anything you should make sure 'Other' is a pawn, though in this case I'd assume Other is a local variable of the type pawn so Other.Pawn is a controller garenteed.

And, if this is the case, there is no need to find this controller's pawn, as this pawn who's controller you're looking into is already declared as Other...

Also, the brackets must be all encompassing. Bob.CheeseLog(Gross) would access the CheeseLog() function of Bob, with the variable Gross. However, CheeseLog(Bob.Gross) means the CheeseLog object in the variable Gross of the object Bob.

Also, you use a raw 'Pawn' variable; if it were Other.Controller.Pawn that would be the pawn in the controller of the something other, but as I think your Other looks to be a Pawn with a controller, using this pawn variable as such makes no sense, and is contradictory. This may not be accurate, but without the full function and class I have to go with the most likely situation.

Neways, this is probally what you would want in the end...

Code:
if(Pawn.Weapon != none && NSFWeaponFire(Pawn.Weapon.FireMode[0]) != none)
	NSFWeaponFire(NSFWeaponFire(Pawn.Weapon.FireMode[0])).variable += variable2;

The if statement to make sure the accessed objects do not exist.
 

Mychaeel

New Member
Typecasting in a nutshell:

  1. Write the object reference you want to typecast: Pawn.Weapon (which is a reference to an object of class Weapon).
  2. Now you don't want this to be a Weapon reference but a NSFWeapon one; so add the typecast to this expression: NSFWeapon(Pawn.Weapon).
  3. Consider this whole expression a NSFWeapon reference and start over.

Typecasting is really not a difficult or complex concept. Just try understanding it.