1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. 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.

[UT2K4]Accessing variables from other classes

Discussion in 'Programming' started by eLy, Apr 5, 2004.

  1. eLy

    eLy New Member

    Joined:
    Mar 18, 2003
    Messages:
    169
    Likes Received:
    0
    This has been driving me crazy for a while, and i know it's possibile.
    I'm trying to access a global variable from another class, i got the code from http://wiki.beyondunreal.com/wiki/Typecasting
    But when i log the variable, it always returns 0.00

    here's the variable i want to call.

    Code:
    class MyShockProjectile extends ShockProjectile;
    
    var float MyVar;
    
    function SuperExplosion()
    {
                 MyVar = 10
                 log("test: "$myVar);        // returns 10
    	Spawn(class'MyShockCombo');
    .....
    }
    
    and this is where i want to call it:

    Code:
    class MyShockCombo extends ShockCombo;
    
    var class<actor> ConvertMe;
    
    simulated event PostBeginPlay()
    {
              local float x;
              ConvertMe = class'MyShockProjectile';
              x = class<MyShockProjectile>(ConvertMe).Default.MyVar;
    
    ......
              
              log(x);      // returns 0.00
    }
    
    i just don't get it.
    any help is appreciated.
    thanks
     
  2. Mychaeel

    Mychaeel New Member

    Joined:
    Oct 3, 2001
    Messages:
    3,830
    Likes Received:
    0
    You're setting an instance variable and reading its default value. Setting a variable of an object naturally doesn't affect the class's defaults.

    If you want to set the default value, use "Default.MyVar = 10" -- but you should really know why you want that before you do it, because it defies a whole bunch of OOP concepts.
     
    Last edited: Apr 5, 2004
  3. eLy

    eLy New Member

    Joined:
    Mar 18, 2003
    Messages:
    169
    Likes Received:
    0
    Hey Michaeel,

    thanks a lot for the reply. it works great !
     
  4. Zer0Zer0

    Zer0Zer0 New Member

    Joined:
    Apr 1, 2004
    Messages:
    35
    Likes Received:
    0
    Your profile pic freaks me out :S
     
  5. eLy

    eLy New Member

    Joined:
    Mar 18, 2003
    Messages:
    169
    Likes Received:
    0
    hehe, i know.
    it's was my high-school graduation pic.
    i don't have teh mustache anymore though =)
     
  6. [SAS]Solid Snake

    [SAS]Solid Snake New Member

    Joined:
    Jun 7, 2002
    Messages:
    2,633
    Likes Received:
    0
    Well another way to do this is:

    Code:
    class MyShockProjectile extends ShockProjectile;
    
    var float MyVar;
    
    function SuperExplosion()
    {
      MyVar = 10
      Spawn(class'MyShockCombo',,Self);
    }
    
    Code:
    class MyShockCombo extends ShockCombo;
    
    var class<actor> ConvertMe;
    
    simulated event PostBeginPlay()
    {
      local float x;
      x = MyShockProjectile(Owner).MyVar; // this returns the MyVar set in the object
      x = class'MyShockProjectile'.default.MyVar; // this returns the MyVar default property from class MyShockProjectile
    }
    
     
  7. eLy

    eLy New Member

    Joined:
    Mar 18, 2003
    Messages:
    169
    Likes Received:
    0
    thanks solid snake.
     
  8. eLy

    eLy New Member

    Joined:
    Mar 18, 2003
    Messages:
    169
    Likes Received:
    0
    hmm,
    when i use:
    Spawn(class'MyShockCombo',,Self);
    i get:
    -Error call to spawn type mismatch in parameter 3


    Spawn(class'MyShockCombo',Self); works though.
     
  9. [SAS]Solid Snake

    [SAS]Solid Snake New Member

    Joined:
    Jun 7, 2002
    Messages:
    2,633
    Likes Received:
    0
    I probably got the syntax wrong. I was pretty sure at the time that the syntax for Spawn in UT2003 was function Spawn(class,name,owner,location, rotation[/i] ...) ... well that's all I can remember of the top of my head.

    The Self part in Spawn is just to define who 'owns' the actor.
     

Share This Page