ID:329959
 
Keywords: if
(See the best response by Solomn Architect.)
Code:
src.SP=5
for(SP;SP>1;SP--)//SP being Stat Points.
switch(input(src,"Which statistic would you like to train? [src.sp] Stat Point(s) remain.",\
"Distribution")in list("Accuracy - [src.acc]","Constitution \
-
[src.con]","Defense - [src.def]","Dexterity - [src.dex]",\
"Intelligience - [src.int]","Strength - [src.str]"))
if("Accuracy - [src.acc]")//Error: expected a constant expression</b>
src.acc+=1
/*else if("Constitution - [src.con]"); src.con+=1
else if("Defense - [src.def]"); src.def+=1
else if("Dexterity - [src.dex]"); src.dex+=1
else if("Intelligience - [src.int]"); src.int+=1
else if("Strength - [src.str]"); src.str+=1*/


Problem description: What I'm trying to do here is get the player to distribute their five SP/Stat Points to their Stats through a switch(input()). I want the player to be aware of their current stat point. Unfortunately, I'm getting an error: expected a constant expression.
I didn't think this was the right way to do it... but I was clueless so I just went with it.

Thanks!

Hi Kenny,

I'm not sure how helpful I can be with this, but an if statement usually has to have something to evaluate and compare an answer to.

I believe the reason why your getting a "expected a constant expression" error is because your if statement isn't really comparing the evaluated answer to anything.

I'm not overly confident on how to fix the problem your having, or recommend an alternative (due to my own inexperience), but hopefully someone else with more experience comes along, or you could try moving this topic to Developer Help, you may get a better response there.
Best response
Kenny84 wrote:
Code:
> src.SP=5
> for(SP;SP>1;SP--)//SP being Stat Points.
> switch(input(src,"Which statistic would you like to train? [src.sp] Stat Point(s) remain.",\
> "Distribution")in list("Accuracy - [src.acc]","Constitution \
> -
[src.con]","Defense - [src.def]","Dexterity - [src.dex]",\
> "Intelligience - [src.int]","Strength - [src.str]"))
> if("Accuracy - [src.acc]")//Error: expected a constant expression</b>
> src.acc+=1
> /*else if("Constitution - [src.con]"); src.con+=1
> else if("Defense - [src.def]"); src.def+=1
> else if("Dexterity - [src.dex]"); src.dex+=1
> else if("Intelligience - [src.int]"); src.int+=1
> else if("Strength - [src.str]"); src.str+=1*/

>


Problem description: What I'm trying to do here is get the player to distribute their five SP/Stat Points to their Stats through a switch(input()). I want the player to be aware of their current stat point. Unfortunately, I'm getting an error: expected a constant expression.
I didn't think this was the right way to do it... but I was clueless so I just went with it.

Thanks!

You're not comparing anything in the if() statement. But here's my solution. For this, don't use switch(). Instead just create a new random variable to parse the input proc from. Now you can use that new var to reference in your if() statement, actually giving a valid expression.
src.SP=5
for(SP;SP>1;SP--)
var/select = input(src,"**Stuff that goes in here**")
if(select == "Accuracy - [src.acc]")
src.acc+=1
/*Other Code*/

This tested with no errors, so let me know if you run into any bugs or problems.
Solomn Architect gave the correct solution (well, except for the indentation), but I'd like to expand on the reason you're getting that error a bit more.

The issue isn't that you aren't comparing the value to anything (with a switch() statement, you're implicitly comparing it to the value passed to switch()). The problem is that switch() statements need to compare to constant values that can be determined at compile time. Because your value changes with each iteration of the loop, it's dynamic and a switch() statement can't handle it.

I would also like to mention that the else-if chain for subsequent values in a switch() statement is unnecessary. See the reference entry for an example usage.

But as mentioned already, a standard if() chain would be the way to go.
Thanks for the help guys, it's much obliged! Works like a charm now. I thought that input() was only for a text response, I didn't know that it could contain a list. Thanks a ton!
In response to DarkCampainger (#3)
DarkCampainger wrote:
Solomn Architect gave the correct solution (well, except for the indentation), but I'd like to expand on the reason you're getting that error a bit more.

Haha, yes, indeed I forgot to change the indentation after copying the snippet. Overlook one problem while solving another. I fixed it for search purposes. Thank you for the in depth explanation, Dark.
Correct, switch compares it with constant values.

Here's a tricky way to make use of switch.
for(SP=5;SP>=1;SP--)
var/list/statlist = list(\
"Accuracy - [acc]",\
"Constitution - [con]",\
"Defense - [def]",\
"Dexterity - [dex]",\
"Intelligience - [int]",\
"Strength - [str]")
switch( statlist.Find( input(src,\
"Which statistic would you like to train? [SP] Stat Point\s remain.",\
"Distribution") in statlist ) )
if(1) acc++
if(2) con++
if(3) def++
if(4) dex++
if(5) int++
if(6) str++

You can also use the \s text macro like above.