Results 1 to 9 of 9
  1. #1
    Redeemer
    Join Date
    Dec 2001
    Posts
    1,482

    Default Is the player crouching?

    ok, first of all... im somewhat new to unreal coding, but i also am starting to get the overall framework of it. im doing a simple weapon project of my own, mostly to learn how to code a little and also potentially for a mod down the road...

    here's my situation: i want to check to see if the player is crouching, in determining the weapon spread.

    Code:
    event ModeDoFire()
    {
    
        local PlayerController PC;
    
        PC = PlayerController(Instigator.Controller);
    
        // 1. CHECK if GUN IS "COOL"
        if ( Level.TimeSeconds - LastFireTime > 0.5 )
            Spread = Default.Spread;
    
        // 2. CHECK if PLAYER IS CROUCHED
    
        else if ( PC.bIsCrouched == True )
            {
            Log("checking crouch: PC");
            }
    
        // 3. ELSE, WIDEn SPREAD A LOT
    
        else
            {
            Spread = FMin(Spread+0.02,0.12);
            }
    
        LastFireTime = Level.TimeSeconds;
        Super.ModeDoFire();
    }
    yes, i know that code doesnt work

    i was trying to tap into the variable/state by checking the bIsCrouched, but it yells at me ("error, Cast from Pawn to PlayerCOntroller will always fail" is the wotgreal/ucc message).

    how do i do this?

    any/all tips would be greatly appreciated. normally, id look to the base code for a working example of the kind of thing i want, but i just dont see one.

  2. #2

    Default

    It might help to know what line the compiler is pointing out as containing the error...

  3. #3
    Palace Guard
    Join Date
    May 2003
    Posts
    3,461

    Default

    bIsCrouched is a property of pawn so instead of 'local PlayerControler PC;' you's wand somthing more like 'local Pawn P' i belive 'P = Pawn(instigator);' will work and then you can have 'if (p.bIsCrouched = True)'

  4. #4
    Redeemer
    Join Date
    Dec 2001
    Posts
    1,482

    Default

    Originally posted by FireCrack
    bIsCrouched is a property of pawn so instead of 'local PlayerControler PC;' you's wand somthing more like 'local Pawn P' i belive 'P = Pawn(instigator);' will work and then you can have 'if (p.bIsCrouched = True)'
    rgr,. i went ahead and tried that syntax...

    Code:
    event ModeDoFire()
    {
        local Pawn P;
        P = Pawn(instigator); 
    
        // 1. CHECK if GUN IS "COOL"
    
        if ( Level.TimeSeconds - LastFireTime > 0.5 )
            Spread = Default.Spread;
    
        // 2. CHECK if PLAYER IS CROUCHED
    
        else if (p.bIsCrouched = True)
             {
             Log("checking crouch: PC");
             }
    
        // 3. ELSE, WIDEn SPREAD A LOT
    
        else
            {
            Spread = FMin(Spread+0.02,0.12);
            }
    
        LastFireTime = Level.TimeSeconds;
        Super.ModeDoFire();
    }
    the error was: "Cast from 'pawn' to 'Pawn' is unneccessary."

    :bulb:

  5. #5

    Default

    Have you read the error message?

    Instigator is already a pawn. As you are using WOTGreal, you can hold CTRL, point in the word 'instigator' and wait until it gets underlined. When this happens, click on it. You will go to the declaration of the variable (this works for fnctions too). THere you will see something like
    var Pawn Instigator;

    What means that Instigator is of Pawn class. So, you dont need to typecast Instigator to Pawn, as its already one. Generally, you use typecasting to access variables in a child class.

    You should read something about Object Oriented Programming, it will help a lot. The Unreal Wiki have pages about this 2 subjects, OOP and typecasting.

    Good luck

  6. #6
    Redeemer
    Join Date
    Dec 2001
    Posts
    1,482

    Default

    Originally posted by luciano.bargman
    Have you read the error message?

    Instigator is already a pawn. As you are using WOTGreal, you can hold CTRL, point in the word 'instigator' and wait until it gets underlined. When this happens, click on it. You will go to the declaration of the variable (this works for fnctions too). THere you will see something like
    var Pawn Instigator;

    What means that Instigator is of Pawn class. So, you dont need to typecast Instigator to Pawn, as its already one. Generally, you use typecasting to access variables in a child class.

    You should read something about Object Oriented Programming, it will help a lot. The Unreal Wiki have pages about this 2 subjects, OOP and typecasting.

    Good luck
    ya, i understand this in concept, just totally new at unreal syntax... and trying anything people suggest.



    I do admit not having much clue about Instigator specifically, although the "controller" rolei think i understand. I'll try some variations. this little excercise is helping me a lot.

    *pokes at code some more*

    PS. i just tried this:

    Code:
        else if ( P.bIsCrouched = true )
             {
             Log("checking crouch: PC");
             }
    and im getting an error about a missing ")"

    P = instigator;

  7. #7
    MSgt. Shooter Person
    Join Date
    Jun 2003
    Posts
    117

    Default

    else if ( P.bIsCrouched = true )

    should be

    else if ( P.bIsCrouched == true )

  8. #8
    Redeemer
    Join Date
    Dec 2001
    Posts
    1,482

    Default

    ahahaha, thanks so much.

    it worked.

    *hides back in his newbie coding cave*

  9. #9
    MSgt. Shooter Person
    Join Date
    Nov 2002
    Posts
    291

    Default

    You can also shorten it:
    else if ( P.bIsCrouched )

    You don't have to ask if a boolean variable == true.

    you can use
    if (!boolean)
    instead of
    if (boolean == false)
    too.


 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Copyright ©2009-2011 Epic Games, Inc. All Rights Reserved.
Digital Point modules: Sphinx-based search vBulletin skin by CompletevB.com.