Jump to content

Use a variable for type of spells.


bigd123

Recommended Posts

You could try to use enumerations and a switch statement to accomplish what you're looking to do:

enum Spell { 
	SPELL1, SPELL2, SPELL3..., SPELLX; 
} 

/* get a Spell object from user choice */

switch (spellObject) { 
	case SPELL1: 
		Spells.NormalSpells./*the proper spell name*/
		break; 
	case SPELL2:
    		Spells.NormalSpells./*the proper spell name*/
		break;
  	case SPELLX:
		Spells.NormalSpells./*the proper spell name*/
  	default:
      		//no spell selected
    		break;
}

 

Edited by luciuspragg
Link to comment
Share on other sites

12 minutes ago, luciuspragg said:

You could try to use enumerations and a switch statement to accomplish what you're looking to do:


enum Spell { 
	SPELL1, SPELL2, SPELL3..., SPELLX; 
} 

/* get a Spell object from user choice */

switch (spellObject) { 
	case SPELL1: 
		Spells.NormalSpells./*the proper spell name*/
		break; 
	case SPELL2:
    		Spells.NormalSpells./*the proper spell name*/
		break;
  	case SPELLX:
		Spells.NormalSpells./*the proper spell name*/
  	default:
      		//no spell selected
    		break;
}

 

I've been trying to wrap my head around enums, but what would i do then with the code you send? Would I put something after this:

Spells.NormalSpells.case
Link to comment
Share on other sites

3 minutes ago, bigd123 said:

I've been trying to wrap my head around enums, but what would i do then with the code you send? Would I put something after this:


Spells.NormalSpells.case

This explanation leaves a lot of details out so I'd recommend doing some more research. So basically an enum object works similar to a variable in that you can assign it an arbitray value, however the difference is that these values are constant and decided at compile time (static) rather than when the script is running (dynamic).

e.g. 

//this is static
int x = 5;

//this is dynamic
int y = foo();

The difference is that when the script starts x already has a value of 5, however y does not recieve a value until the foo() method is called and assigns it one. So when you initialize an enum object you provide it with all the possible values that it can be. In the snippet in the previous post an enum object of type Spell can only have the values given to it initially (SPELL1, SPELL2, etc).

However remember that it's only the possible values of the enum that are static, what the actual value of the enum object is can be changed which is why we can do what I was suggesting. I'm assuming you have a way for the user to select what spell they want so you'd create an enum object of type Spell, assign it a value based off what the user chooses, and then set the spell accordingly:

enum Spell { 
	SPELL1, SPELL2, SPELL3..., SPELLX; 
} 

Spell userChoice = //whatver value you want to use to determine which spell was picked

switch(userChoice) {
	case SPELL1:
  		Spells.NormalSpells./*the proper spell name*/
		break;
	//etc
}

 

  • Like 1
Link to comment
Share on other sites

25 minutes ago, luciuspragg said:

This explanation leaves a lot of details out so I'd recommend doing some more research. So basically an enum object works similar to a variable in that you can assign it an arbitray value, however the difference is that these values are constant and decided at compile time (static) rather than when the script is running (dynamic).

e.g. 


//this is static
int x = 5;

//this is dynamic
int y = foo();

The difference is that when the script starts x already has a value of 5, however y does not recieve a value until the foo() method is called and assigns it one. So when you initialize an enum object you provide it with all the possible values that it can be. In the snippet in the previous post an enum object of type Spell can only have the values given to it initially (SPELL1, SPELL2, etc).

However remember that it's only the possible values of the enum that are static, what the actual value of the enum object is can be changed which is why we can do what I was suggesting. I'm assuming you have a way for the user to select what spell they want so you'd create an enum object of type Spell, assign it a value based off what the user chooses, and then set the spell accordingly:


enum Spell { 
	SPELL1, SPELL2, SPELL3..., SPELLX; 
} 

Spell userChoice = //whatver value you want to use to determine which spell was picked

switch(userChoice) {
	case SPELL1:
  		Spells.NormalSpells./*the proper spell name*/
		break;
	//etc
}

 

literally all he has to do is populate all the normalspells into a combobox (Enum)

MagicSpell spell = combobox.getselecteditem()



its not hard and I gave him the answer. its basic Java imo

first step is learning how to read the Javadocs

then apply how to create Objects

5w9mdZf.png

https://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html

  • Like 1
Link to comment
Share on other sites

1 hour ago, Chris said:

literally all he has to do is populate all the normalspells into a combobox (Enum)

MagicSpell spell = combobox.getselecteditem()



its not hard and I gave him the answer. its basic Java imo

first step is learning how to read the Javadocs

then apply how to create Objects

5w9mdZf.png

https://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html

Yes that's true and would be a great way to implement it ultimately with a GUI, however given the fact he wasn't too confident with enums to begin with I opted to explain them using a simple switch statement. I know it's the go to advice to read the docs but they can be daunting and confusing for some people. Besides, I had some free time so I didn't mind typing up an explanation. It seems that it helped @bigd123, so hopefully he can move onto your suggestion and work from there :)

Edited by luciuspragg
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...