- Home /
Simple EXP Script In Javascript
Okay so I have taken previous examples and modified them briefly to try and get this to work. I have two scripts one for the enemy and one for the Player. Here is my enemy script.
#pragma strict
#pragma downcast
public var hitPoints = 100.0;
var deathEffect : Transform;
var effectDelay = 0.0;
private var gos : GameObject[];
var multiplier : float = 1;
var deadReplacement : Rigidbody;
@HideInInspector
var playerObject : GameObject;
var useHitEffect : boolean = true;
var expVal : int = 20;
@HideInInspector
var isEnemy : boolean = false;
var expScript : ExperienceSystem;
function ApplyDamage(Arr : Object[]){
//Info array contains damage and value of fromPlayer boolean (true if the player caused the damage)
//Find the player if we haven't
if(Arr[1] == true){
if(!playerObject){
playerObject = GameObject.FindWithTag("Player");
}
if(useHitEffect){
playerObject.BroadcastMessage("HitEffect");
}
}
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
var tempFloat : float;
tempFloat = Arr[0];
//float.TryParse(Arr[0], tempFloat);
hitPoints -= tempFloat*multiplier;
if (hitPoints <= 0.0) {
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = true;
Invoke("DelayedDetonate", effectDelay);
}
}
function ApplyDamagePlayer (damage : float){
//Info array contains damage and value of fromPlayer boolean (true if the player caused the damage)
//Find the player if we haven't
if(!playerObject){
playerObject = GameObject.FindWithTag("Player");
}
if(useHitEffect){
playerObject.BroadcastMessage("HitEffect");
}
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
//float.TryParse(Arr[0], tempFloat);
hitPoints -= damage*multiplier;
if (hitPoints <= 0.0) {
// Start emitting particles
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = true;
Invoke("DelayedDetonate", effectDelay);
}
}
function ApplyDamage (damage : float){
//Info array contains damage and value of fromPlayer boolean (true if the player caused the damage)
//Find the player if we haven't
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0){
return;
}
//float.TryParse(Arr[0], tempFloat);
hitPoints -= damage*multiplier;
if (hitPoints <= 0.0) {
// Start emitting particles
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter)
emitter.emit = true;
Invoke("DelayedDetonate", effectDelay);
}
if (hitPoints <= 0.0) {
GameObject.Find("Player").GetComponent(ExperienceSystem).GiveXP(expVal);
// Start emitting particles
}
}
function Detonate(){
if(isEnemy)
EnemyMovement.enemies--;
// Create the deathEffect
if (deathEffect)
Instantiate (deathEffect, transform.position, transform.rotation);
// If we have a dead replacement then replace ourselves with it!
if (deadReplacement) {
var dead : Rigidbody = Instantiate(deadReplacement, transform.position, transform.rotation);
// For better effect we assign the same velocity to the exploded gameObject
dead.rigidbody.velocity = rigidbody.velocity;
dead.angularVelocity = rigidbody.angularVelocity;
}
// If there is a particle emitter stop emitting and detach so it doesnt get destroyed right away
var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
if (emitter){
emitter.emit = false;
emitter.transform.parent = null;
}
BroadcastMessage ("Die", SendMessageOptions.DontRequireReceiver);
Destroy(gameObject);
}
function DelayedDetonate(){
BroadcastMessage ("Detonate");
}
Okay so in the second ApplyDamage(); function I have an if statement that should send a value of 20 to my giveXP function for my player script. Here is that script.
var guiTextEXP : GUIText;
var accumulatedExperiencePoints : int = 0;
var levelExpRequirements : int = 1000;
var currentLevel : int = 1;
var xp : int;
function GiveXP(addedXP : int)
{
if (addedXP < 0)
{
Debug.Log("Can't remove exp!");
return;
}
xp+=addedXP;
if (accumulatedExperiencePoints >= levelExpRequirements)
{
levelExpRequirements += 1000;
currentLevel += 1;
}
}
Now it am trying to get the first script to update the amount of accumulatedExperiencePoints that I have. But I am not sure how to do that. Can someone just point my in the right direction because I am clearly missing something lol. Thank you so much :)
public GameObject gameObject ; // In which attaching firstScript;
private firstScriptName firstScript;
function Start()
{
firstScript = gameObject.GetComponent<firstScriptName>();
}
function Update()
{
var secondVariable = firstScript.variableName ;
}
Try this idea
I would be putting that in the enemy script?
If you want to access variable from Player Script using Enemy script , then you should putting this code in Enemy Script . Else you want to access variable from enemy script using player script , then put in player script
Yeah I had to first redesign that script so that it didn't throw me errors while still keeping the base idea, still no luck :/
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Recording metrics? 1 Answer
Singletons in music script? 2 Answers
Script crashes Unity 1 Answer
Experience System 1 Answer