Player Variables, GUI, and Me...

Hey guys! I’m trying to implement player variables (stats, inventory, etc) and I’m using the 3D platform script as a reference, and I’m getting a little hung-up on the process…allow me to elaborate (without too much bloat ;)) I see that there’s a ThirdPersonStatus.js file which attaches to the player which stores said variables and contains functions to add health, items, etc; Then there’s an “object Pickup” script that gets attached to the collectible items, and stores the item type, and calls the upon the functions in ThirdPersonStatus scripts; and lastly there’s the HUD scripts which simply relay info from ThirdPerson.js and updates the GUI text as needed. I’m having trouble trying in figure out how they all “work” together, for example:
In GameHUD.js

function Awake()
{
	playerInfo = FindObjectOfType(ThirdPersonStatus);

	if (!playerInfo)
		Debug.Log("No link to player's state manager.");
}

the FindObjectOfType part confusesme, because ThirdpersonStatus is a script, not an object…do I need another game object of the same name?

Or in pickup.js

enum PickupType { Health = 0, FuelCell = 1 }
var pickupType = PickupType.FuelCell;
var amount = 1;
var sound : AudioClip;
var soundVolume : float = 2.0;


private var used = false;
private var mover : DroppableMover;

function Start ()
{
	// do we exist in the level or are we instantiated by an enemy dying?
	mover = GetComponent(DroppableMover);
}

function ApplyPickup (playerStatus : ThirdPersonStatus)
{
	// A switch...case statement may seem overkill for this, but it makes adding new pickup types trivial.
	switch (pickupType)
	{
		case PickupType.Health:
			playerStatus.AddHealth(amount);
			break;
		
		case PickupType.FuelCell:
			playerStatus.FoundItem(amount);
			break;
	}
	
	return true;
}

function OnTriggerEnter (col : Collider) {
	if(mover  mover.enabled) return;
	var playerStatus : ThirdPersonStatus = col.GetComponent(ThirdPersonStatus);
	
	//* Make sure we are running into a player
	//* prevent picking up the trigger twice, because destruction
	//  might be delayed until the animation has finished
	if (used || playerStatus == null)
		return;
	
	if (!ApplyPickup (playerStatus))
		return;

	used = true;
	
	// Play sound
	if (sound)
		AudioSource.PlayClipAtPoint(sound, transform.position, soundVolume);
		
	
	
	// If there is an animation attached.
	// Play it.
	if (animation  animation.clip)
	{
		animation.Play();
		Destroy(gameObject, animation.clip.length);
	}
	else
	{
		Destroy(gameObject);
	}
}

// Auto setup the pickup
function Reset ()
{
	if (collider == null)	
		gameObject.AddComponent(BoxCollider);
	collider.isTrigger = true;
}

@script RequireComponent(SphereCollider)
@script AddComponentMenu("Third Person Props/Pickup")

I don’t understand where it explicitly calls the ApplyPickup function, im also unsure as to why the ThirdPersonStatus script gets passed as an argument when they declare the ApplyPickup function like so.

function ApplyPickup (playerStatus : ThirdPersonStatus)

I’m assuming it’s because accesses methods (correct terminology??) of the ThirdPersonStatus script? I’m just looking for some clarification as to why things work they way they do, ya know the old “Teach a man to fish”… Anyway thanks in advance guys, i’ll have a little demo up shortly!

It is an object. Everything is an object of some type or another.

In the OnTriggerEnter function.

It needs a reference to the script so it can run the “playerStatus.AddHealth(amount);” and “playerStatus.FoundItem(amount);” functions.

–Eric

Thanks for the quick response, I must have worded that wrong, because I need a little more than one sentence answers. So FindObjectOfType will locate anything in my project within the game be it a script, prefab, variable, matrix, array, or sound clip?

At the risk of sounding rude I’m not THAT simple, if i couldn’t understand basic logic I wouldn’t be trying to code, haha…I don’t see where at ACTUALLY tells that function to run.

As for the last part I see what’s going on, maybe i’m just unclear on the rules as to WHAT an argument actually is. Is it basically just a list of variables that gets used in the function? Is there a limit on the amount or type? for instance if i needed a function that manipulated data about two individuals, pertaining to their name and age it would look like this?

function ManipulationofData (personAName, personAAge, personBName, personAAge){
var sumOfAges = personAAge += personBAge;
return sumOfAges;
}

like I said I want to gain a deeper understanding of how this stuff works as I go so I can become a contributing member of these boards… Thanks a ton!

Well, anything that’s derived from UnityEngine.Object anyway, which scripts are. So, not variables like floats or ints; “everything is an object” was kind of overly broad in hindsight.

You wanted to learn to fish, I think. :wink: Look in the OnTriggerEnter function again; there’s not much code there so it should be easy to see.

Yes, and no, respectively.

You’re not using personAName or personBName, so you would leave those out (or extend the function so it uses them somehow), but otherwise yes, that’s the general idea. It would be better to declare the variable types though, otherwise it uses dynamic typing, which is slow. (Dynamic typing can be useful, but not really in this case.) I’m sure it’s just a typo, but += here should just be +. Also, it would be a bit better to do

return personAAge + personBAge;

since there’s no particular reason to make the sumOfAges variable since you’re not doing anything else with it. Also a more descriptively accurate name instead of ManipulationOfData, so you can tell what the function does when called from elsewhere. I’m sure that was just an off-the-cuff example, but I wanted to be clear about everything.

–Eric

Awesome, now i feel like i’m getting somewhere. I’ve read a TON of programming stuff, I’m familiar with all the syntax, I have minor experience with C++, and actionscript, as well as event-driven scripting experience using Multimedia Fusion (Kind of like a spreadsheet of cause and effect relations between events and objects) so i’m really just trying to learn the finer points of coding and cut my teeth at the same time, so any and all tips are greatly appreciated. As for that ApplyPickup function I just can’t see it, im sure i just have the wrong perspective so i’ll break down what i see it as and maybe you could correct me where Im wrong?

if (used || playerStatus == null)
   return;

If the item is used or if their is no playerStatus object attached to the colliding object (which is needed to run the ApplyPickup function), then return 0; (Don’t pick it up)

This next part trips me out:

if (!ApplyPickup (playerStatus))
   return;

:slight_smile: I think i just figured it out, if the ApplyPickup function (located in the script associated with the variable playerStatus) returns false (in this case meaning that the collectible is not a valid type), then return 0 as well. Otherwise set used to true and carry on with the function. So just by checking the return value of a function (and in this case passing it as an argument) the function gets invoked? Is my wording correct? Am i right to assume that functions return 0 by default?

Almost…any time you call a function, the function is invoked (well, yeah :wink: ). Whether it’s inside an if statement or wherever isn’t actually relevant; if it didn’t run it wouldn’t be able to return anything.

No, functions return the default value for the type that they are. In Javascript you can leave out the return type, but they still have one. ApplyPickup is returning a boolean, so it would return false by default. It could be made explicit:

function ApplyPickup (playerStatus : ThirdPersonStatus) : boolean {

OnTriggerEnter is of type void, so it doesn’t return anything (you can see function return types in the docs). Only a function of return type int, byte, ushort, long, etc. (basically the integer types) would return 0 by default.

Although the ApplyPickup function can only ever return true as written, so checking the return value doesn’t seem to make much sense here. Maybe it was intended to be expanded so it would return false in some cases.

–Eric