Why this way of coding?

This is part of open source rpg game.

I don’t know why this structure needed.

In the code,

  1. Why use typeof?

  2. Why in the function Awake variable be difined like that? or what happen?

static var camTrans : Transform;

// Custom components
static var uI : UI;
static var cameraControls : CameraControls;
static var playerControls : PlayerControls;
static var playerState : PlayerState;
static var stats : Stats;
static var abilities : Abilities;
static var abilityList : AbilityList;
static var combat : Combat;
static var attacks : Attacks;
static var playerAnimation : PlayerAnimation;
static var myAnimation : Animation;

// Other things
static var rightForearmNode : Transform;

// Serialized vars
var anim : Animation;

function Awake()
{
  player = gameObject;
  myTrans = transform;
  myRigid = rigidbody;
  controller = GetComponent(typeof(CharacterController));
  
  // Camera
  cam = Camera.main;
  camTrans = cam.transform.root;
  cameraControls = cam.GetComponent(typeof(CameraControls));
  
  myAnimation = anim;
	playerState = GetComponent(typeof(PlayerState));
	stats = playerState.stats;
	abilities = GetComponent(typeof(Abilities));
	abilityList = abilities.abilityList;
	playerControls = GetComponent(typeof(PlayerControls));
	uI = GetComponent(typeof(UI));
	combat = GetComponent(typeof(Combat));
	attacks = combat.attacks;
	playerAnimation = GetComponent(typeof(PlayerAnimation));
	
	rightForearmNode = GameObject.FindWithTag("RightForearmNode").transform;
	
}

Good question; maybe the author was used to C# and didn’t realize it’s not necessary in JS.

Why is what variable defined like what?

–Eric

Why is what variable defined like what?

–Eric[/quote]

Above, static var already difined. And again, awake defined like same thing in my view.

If I’m seeing what you are seeing, the person was giving types to the static variables at the top, but in the Awake function s/he is actually assigning values to them.

Only the types of the variables are declared outside Awake; the actual values are assigned inside Awake. This is generally good practice because running code outside functions can fail in interesting ways, and assigning the values in Awake means you have a completely defined order in which the values are available (i.e., before any Start functions).

–Eric