bigd123 Posted February 13, 2019 Share Posted February 13, 2019 In my script I want the user to be able to choose what spell to use, except I can't use a variable. Example: Spells.NormalSpells.LVL_4_ENCHANT would work but Spells.NormalSpells.spellvariable wouldn't. Sorry if this may be a simple question but help would be appreciated Quote Link to comment Share on other sites More sharing options...
Chris Posted February 13, 2019 Share Posted February 13, 2019 https://osbot.org/api/org/osbot/rs07/api/ui/MagicSpell.html Quote Link to comment Share on other sites More sharing options...
bigd123 Posted February 13, 2019 Author Share Posted February 13, 2019 4 minutes ago, Chris said: https://osbot.org/api/org/osbot/rs07/api/ui/MagicSpell.html ? Quote Link to comment Share on other sites More sharing options...
Chris Posted February 13, 2019 Share Posted February 13, 2019 Just now, bigd123 said: ? Thats the answer Quote Link to comment Share on other sites More sharing options...
luciuspragg Posted February 13, 2019 Share Posted February 13, 2019 (edited) 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 February 14, 2019 by luciuspragg Quote Link to comment Share on other sites More sharing options...
bigd123 Posted February 14, 2019 Author Share Posted February 14, 2019 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 Quote Link to comment Share on other sites More sharing options...
Super Posted February 14, 2019 Share Posted February 14, 2019 1 Quote Link to comment Share on other sites More sharing options...
bigd123 Posted February 14, 2019 Author Share Posted February 14, 2019 Thank you so much! What I was looking for. Quote Link to comment Share on other sites More sharing options...
luciuspragg Posted February 14, 2019 Share Posted February 14, 2019 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 } 1 Quote Link to comment Share on other sites More sharing options...
bigd123 Posted February 14, 2019 Author Share Posted February 14, 2019 (edited) Makes a lot more sense logically like that should help with my new script I'm planning to release. Thank you so much Edited February 14, 2019 by bigd123 Quote Link to comment Share on other sites More sharing options...
Chris Posted February 14, 2019 Share Posted February 14, 2019 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 https://docs.oracle.com/javase/tutorial/java/IandI/interfaceAsType.html 1 1 Quote Link to comment Share on other sites More sharing options...
luciuspragg Posted February 14, 2019 Share Posted February 14, 2019 (edited) 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 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 February 14, 2019 by luciuspragg Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.