UnityScript: Chest system for RPG game

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 3520 Views - Last Post: 12 March 2013 - 03:47 PM Rate Topic: -----

#1 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

UnityScript: Chest system for RPG game

Posted 12 March 2013 - 08:08 AM

Hello,
i am trying to write a script for treasure chest in Javascript. Basically a Player will be able to open a chest and loot stuff from it. First thing first, i want to create an Enum state for chest being open/close or something inbetween.
I have this piece of code and i am having an error in Console

#pragma strict
//Chest script


enum ChestState { Open, Close, InBetween }												//Enum for tri-state values.

var stateOpen 		= ChestState.Open;													//Chest Open
var stateClose 		= ChestState.Close;													//Chest Close - set as default		
var stateInBetween 	= ChestState.InBetween;												//Somewhere in the process of open or close



function Start () {
	
	var chestState = ChestState.Close;
}

function Update () {

}

function onmouseenter() {
	Debug.Log("Enter");
}

function OnMouseExit() {
	Debug.Log("Exit");
}

function onmouseup() {
	Debug.Log("Up");
	
}


private function Open() {
	if(chestState === ChestState.Close) {
		animation.Play();
		}
	
}

private function Close() {
	animation.Play();
	
}


my error "Assets/Scripts/interactiveGameObjects/Chest.js(37,12): BCE0005: Unknown identifier: 'chestState'."
why do i have this error and how can i fix it please

Is This A Good Question/Topic? 0
  • +

Replies To: UnityScript: Chest system for RPG game

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8367
  • View blog
  • Posts: 31,092
  • Joined: 12-June 08

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 08:10 AM

Moving to javascript.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,212
  • Joined: 23-August 08

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 11:57 AM

function Start () {
	
	var chestState = ChestState.Close;
}


As the error says, your chestState variable only exists for the lifetime of the Start function.
Was This Post Helpful? 0
  • +
  • -

#4 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:21 PM

Continuing with my script, i want to be able to open and close the chest onmouseup. First piece of code works on 50%, it says chest open but does not close the chest, also 'else' statement does not work

#pragma strict
//Chest script


enum ChestState 									//Enum for tri-state values.
	{ 
		Open, 										//Chest Open
		Close, 										//Chest Close - set as default											
		InBetween 									//Somewhere in the process of open or close
	}												

public var myEnumInstance : ChestState;				//Create an Instance of a ChestState Enum


function Start () {
	myEnumInstance = ChestState.Close;	
	
}

function Update () {

}

function onmouseenter() {
	Debug.Log("Enter");
}

function OnMouseExit() {
	Debug.Log("Exit");
}

function onmouseup() {
	Debug.Log("Up");
	if(myEnumInstance == ChestState.Open) 			//Test our enum instance to see what it's value is/isn't	
		Open();	
		myEnumInstance = ChestState.Open;
	
	//else (myEnumInstance == ChestState.Close);
		//Close();
		//myEnumInstance = ChestState.Close;		
			
}

private function Open() {
	//animation.Play("open");						//Play animation	
	myEnumInstance = ChestState.Open;				//Assign a value to our new enum instance
	Debug.Log("Chest is now open.");
			
}

private function Close() {
	//animation.Play("close");						//Play animation
	myEnumInstance = ChestState.Close;				//Assign a value to our new enum instance
	Debug.Log("Chest is now closed.");
				
		
}



i am trying to find out what is going wrong and don't know what it is.

Second code is using switch statement and Coroutine which is the one i would preferand of course does not work either

function onmouseup() {
	Debug.Log("Up");
	
	switch(ChestState) {
	
	case 1 : myEnumInstance.Open; 
		myEnumInstance = ChestState.InBetween;
		StartCoroutine("Close");
	break;
	
	case 2 : myEnumInstance.Close; 
		myEnumInstance = ChestState.InBetween;
		StartCoroutine("Open");
	break;	
	}
}

private function IEnumerator (Open) {
	//animation.Play("open");						//Play animation	
	myEnumInstance = ChestState.Open;				//Assign a value to our new enum instance
	
	yield return new WaitForSeconds(2);
	
	Debug.Log("Chest is now open.");
	
		
}

private function IEnumerator (Close) {
	//animation.Play("close");						//Play animation
	myEnumInstance = ChestState.Close;				//Assign a value to our new enum instance
	
	yield return new WaitForSeconds(2);
	
	Debug.Log("Chest is now closed.");

}



i would be greatfull for any help, thank you

This post has been edited by TrewSx: 12 March 2013 - 01:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3390
  • View blog
  • Posts: 9,586
  • Joined: 08-June 10

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:34 PM

View PostTrewSx, on 12 March 2013 - 09:21 PM, said:

i would be greatfull for any help, thank you

out of interest, what browser/application does that code run in?
Was This Post Helpful? 0
  • +
  • -

#6 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:35 PM

Unity engine
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2876
  • View blog
  • Posts: 9,544
  • Joined: 12-December 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:42 PM

What language is this? It is not Javascript; maybe it's something like TypeScript?

    if(myEnumInstance == ChestState.Open)           //Test our enum instance to see what it's value is/isn't   
        Open();
        myEnumInstance = ChestState.Open;

You should use brackets { } to enclose the statements after your if condition. Only the first line (Open()) executes if the ChestState is Open, but the following line will ALWAYS be executed, always setting the the enum-instance to .Open.

Personally, I wouldn't use function names onmouseup() etc.; these already exist as JS attributes/events and this can only lead to confusion and possible error.

What are you attempting to do with this line, and similar:

case 1 : myEnumInstance.Open;

If you are calling a method then (in JS) it should be .Open(). I don't believe you intend to call a method (especially on an enum!) so delete this statement (but not the case 1: part).

This post has been edited by andrewsw: 12 March 2013 - 01:44 PM

Was This Post Helpful? 0
  • +
  • -

#8 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:45 PM

I am using Javascript. On line 32 it should be function onmouseup() {} my mistake sorry for confusing you.
And last two functions are basically Open an Close functions from previous code i only tried to use Open and Close as a parameter, because i didnt now what else to try
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2876
  • View blog
  • Posts: 9,544
  • Joined: 12-December 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:48 PM

IMO I would also change the name myEnumInstance - it is not very informative. Maybe currentChestStatus, or anything else more descriptive.

This post has been edited by andrewsw: 12 March 2013 - 01:49 PM

Was This Post Helpful? 1
  • +
  • -

#10 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:49 PM

If you are calling a method then (in JS) it should be .Open().
How come i did not see this, feel like an idiot now thanks for help anyway, but i tried {} brackets
in my if statement and it did not work either, maybe i overlooked something
Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2876
  • View blog
  • Posts: 9,544
  • Joined: 12-December 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:54 PM

View PostTrewSx, on 12 March 2013 - 01:45 PM, said:

I am using Javascript.

This is not vanilla Javascript - it might be called UnityJS, or, I believe, UnityScript.

if(myEnumInstance == ChestState.Open) {          //Test our enum instance to see what it's value is/isn't   
    Open();
    myEnumInstance = ChestState.Open;
} else {
    // do something else
}

This post has been edited by andrewsw: 12 March 2013 - 01:52 PM

Was This Post Helpful? 1
  • +
  • -

#12 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:55 PM

ok i am using UnityScript, from now on i will remember to use this term instead of regular JS

This post has been edited by TrewSx: 12 March 2013 - 01:56 PM

Was This Post Helpful? 0
  • +
  • -

#13 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2876
  • View blog
  • Posts: 9,544
  • Joined: 12-December 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:56 PM

if(myEnumInstance == ChestState.Open) {          //Test our enum instance to see what it's value is/isn't   
    Open();
    myEnumInstance = ChestState.Open;
} else {
    // do something else
}

But this doesn't make sense: the status is already .Open, your Open() function sets this to Open, and you then set it to Open a second time(!?).
Was This Post Helpful? 0
  • +
  • -

#14 TrewSx  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 50
  • Joined: 07-June 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:57 PM

thank you Andy for ur time and help
Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 2876
  • View blog
  • Posts: 9,544
  • Joined: 12-December 12

Re: UnityScript: Chest system for RPG game

Posted 12 March 2013 - 01:59 PM

View PostTrewSx, on 12 March 2013 - 01:55 PM, said:

ok i am using UnityScript, from now on i will remember to use this term instead of regular JS

Yes, they are very different languages ;)

View PostTrewSx, on 12 March 2013 - 01:57 PM, said:

thank you Andy for ur time and help

No worries.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2