Gosub return error

Hey I’m having trouble with using return for a gosub function in the stats area. Whenever I put a return at the bottom of my function, it gives me the same error saying I don’t have a parameter for “trait_value” when I’m pretty sure its right there at the top of everything. If I remove the return, I can only call the gosub once and no other text will show after that which isn’t what I want.

The error I’m getting is: “choicescript_stats line 82: No parameter passed for trait_value”

My code looks like this:

[b]PERSONALITY TYPE:[/b] [[b]${personalitytype[index]}[/b]]


BRAWN:
*gosub show_trait_bar traits_1

CHARISMA:
*gosub show_trait_bar traits_2



*label show_trait_bar
*params trait_value
*temp filled ""
*temp empty ""
*comment calculate number of filled stars (1 star per 4 points)
*temp stars (trait_value / 4)
*set stars round(stars)
*comment build the filled stars
*if stars > 0
  *temp count 0
  *label build_filled
  *if count < stars
    *set filled & "★"
    *set count + 1
    *goto build_filled
*comment build the empty stars
*temp empty_stars (10 - stars)
*if empty_stars > 0
  *temp count_empty 0
  *label build_empty
  *if count_empty < empty_stars
    *set empty & "☆"
    *set count_empty + 1
    *goto build_empty
${filled}${empty}
*return

If you use Preformatted text it becomes easier to read the code. It preserves the white spaces. It’s one of the options when you click the gear icon on the menu bar when composing a post.


Regarding your error, if I had to guess it’s because you have to “hide” the subroutine. ChoiceScript will read your code top to bottom. Right now, after charisma, it runs through the show_trait_bar subroutine code block again. My suggestion is to move it to the end of the file and put a *finish before it.

1 Like

I once got a similar problem with a similar subroutine although my code was trying to emulate the “Chice of Robots” style stats and was simpler.

In my case I forget to declare the param with a proper value!=0.
I mean, the parameter was a blank “” whithout numeral value and the code was supposed to “count” said blank value.

Other cause of this problem may be an issue seems with how you are calling the show_trait_bar function in the choicescript_stats file.

The error message indicates that no parameter was passed for trait_val

Hmm… lets make some tests.

Try this:


Instead of:

BRAWN:
*gosub show_trait_bar traits_1
CHARISMA:
*gosub show_trait_bar traits_2

Try explicitly passing the variable:

*comment if trairs_1 or trait_2 ISNUMBER
BRAWN: 
*gosub show_trait_bar (traits_1)
CHARISMA: 
*gosub show_trait_bar (traits_2)


Also, I did notice you are using implicit control flow. Do trait_1 or trait_2 are global variables or are they temporary variables declared in another routine?

This actually worked, thanks!

But now, I’m trying to check for a personality type and a trait to set the max star count to 7, but for soem reason the if statement I wrote doesn’t do anything. Any thoughts?

*label show_trait_bar

*params trait_value

*if "${trait_value}" = ""

*set trait_value 0

*temp filled ""

*temp empty ""

*temp count_empty 0

*comment calculate number of filled stars (1 star per 4 points)

*temp stars (trait_value / 4)

*set stars round(stars)

*comment build the filled stars

*if stars > 0

*temp count 0

*label build_filled

*if count < stars

*set filled & "★"

*set count + 1

*goto build_filled

*comment build the empty stars

*temp empty_stars (10 - stars)

*if empty_stars > 0

*label build_empty

*if count_empty < empty_stars

*set empty & "☆"

*set count_empty + 1

*goto build_empty

*comment check for personality type and adjust the bar

*if (personalitytype_1 = "Hard-Working Idiot") and (trait_value = traits_1)

*set count_empty 7

*set count 7

${filled}${empty}

*return
1 Like

Assuming the indentations are as written, your if statement seems to be empty, that would be why it’s not doing anything. It’d need to be:

*if count < stars
    *set filled & "★"
(...)

However, I’d recommend using multireplace to fill stars like that instead of trying to emulate a repetition loop on ChoiceScript. In that case, you wouldn’t call a subroutine to fill stars and would instead use plain text.

Something on the lines of:

*temp traitname_stars traitname / 4
*set traitname_stars round(traitname_stars) 

Traitname: @{(traitname_stars+1)  ☆☆☆☆☆☆☆|★☆☆☆☆☆☆|★★☆☆☆☆☆|★★★☆☆☆☆|★★★★☆☆☆|★★★★★☆☆|★★★★★★☆|★★★★★★★}

If you want the maximum value to change over time, then you’d have traitname, traitname_stars_max and the temporary traitname_stars:

*temp traitname_stars traitname / 4
*set traitname_stars round(traitname_stars)

Traitname: @{(traitname_stars+1) |★|★★|★★★|★★★★|★★★★★|★★★★★★|★★★★★★★}@{((traitname_stars_max-traitname_stars)+1) |☆|☆☆|☆☆☆|☆☆☆☆|☆☆☆☆☆|☆☆☆☆☆☆|☆☆☆☆☆☆☆}

Traitname is whatever trait you’re converting to stars.

2 Likes

Thanks for the reply but this is what I mean:

I have 5 Personality “Types” that I’m trying to give each a respective max set of traits, (e.g. let’s say “type 1” can only have a max amount of 6 stars in trait1, while “type 2” can have a max amount of 7 in trait1)

This is what I have currently:

[b]PERSONALITY TYPE:[/b] [[b]${personalitytype[index]}[/b]]


BRAWN:
*gosub show_trait_bar traits_1

CHARISMA:
*gosub show_trait_bar traits_2

DEXTERITY:
*gosub show_trait_bar traits_3

INTELLIGENCE:
*gosub show_trait_bar traits_4

WILL:
*gosub show_trait_bar traits_5

PRESENCE:
*gosub show_trait_bar traits_6

__________________________________________________________

STRATEGY
*gosub show_trait_bar traits_7
*finish

*label show_trait_bar
*params trait_value

*if "${trait_value}" = ""
  *set trait_value 0

*temp max_stars 10
*temp filled ""
*temp empty ""
*temp count_empty 0

*if trait_value = traits_1
  *set max_stars 10
*if trait_value = traits_2
  *set max_stars 10
*if trait_value = traits_3
  *set max_stars 10
*if trait_value = traits_4
  *set max_stars 10
*if trait_value = traits_5
  *set max_stars 10
*if trait_value = traits_5
  *set max_stars 10
*if trait_value = traits_6
  *set max_stars 10

*comment Determine max stars based on personality type and trait type
*if (personalitytype_1 = "Hard-Working Idiot") and (trait_value = traits_1)
  *set max_stars 6
*if (personalitytype_1 = "Hard-Working Idiot") and (trait_value = traits_2)
  *set max_stars 9
*if (personalitytype_1 = "Hard-Working Idiot") and (trait_value = traits_3)
  *set max_stars 10 
*if (personalitytype_1 = "Hard-Working Idiot") and (trait_value = traits_4)
  *set max_stars 3
*if (personalitytype_1 = "Hard-Working Idiot") and (trait_value = traits_5)
  *set max_stars 8
*if (personalitytype_1 = "Hard-Working Idiot") and (trait_value = traits_6)
  *set max_stars 9

*comment Calculate number of filled stars (1 star per 4 points)
*temp stars (trait_value / 4)
*set stars round(stars)

*comment Adjust star count based on max_stars
*if stars > max_stars
  *set stars max_stars

*comment Build the filled stars
*if stars > 0
  *temp count 0
  *label build_filled
  *if count < stars
    *set filled & "★"
    *set count + 1
    *goto build_filled

*comment Build the empty stars
*temp empty_stars (max_stars - stars)
*if empty_stars > 0
  *label build_empty
  *if count_empty < empty_stars
    *set empty & "☆"
    *set count_empty + 1
    *goto build_empty

*comment Display the bar
${filled}${empty}
*return

This only really works once, before it just copies the latest max star value for the 5 other 5 traits. I’m guessing this is because it only resets “max_stars” once, but I have no clue what the alternative would be. (I’m super new to choicescript sorry lol)

I see. In this case, you’re comparing actual variable values:

*if (personalitytype_1 = "Hard-Working Idiot") and (trait_value = traits_1)
  *set max_stars 6
*if (personalitytype_1 = "Hard-Working Idiot") and (trait_value = traits_2)
  *set max_stars 9

Let’s say you’re running the code checking for traits_2.

In the first run, traits_1 has a value of 30 and traits_2 has a value of 45. In this case, the code would run, correctly detect trait_value = 45, and set max_stars to 9.

However, if traits_1 has a value of 30 and traits_2 also has a value of 30, the code finds that trait_value = 30 = traits_1 and sets the max_stars to 6.

It’ll run into this problem everytime two traits have the same value.

Under the assumption that you don’t want to use multireplace, your best bet would be to set a second control param.

*label show_trait_bar
*params trait_value
*params trait_number

(...)

*if (personalitytype_1 = "Hard-Working Idiot") and (trait_number = 1)
    *set max_stars 6
*if (personalitytype_1 = "Hard-Working Idiot") and (trait_number = 2)
    *set max_stars 9
(...)

And earlier on, you’d call on the subroutine using a number literal:

BRAWN:
*gosub show_trait_bar traits_1 1

CHARISMA:
*gosub show_trait_bar traits_2 2

(...)

Ok, just tried this and it gives me the error:

“choicescript_stats line 99: No parameter passed for trait_number”

That’s my bad, I’m a little scatterbrained.

*label show_trait_bar
*params trait_value trait_number

That’s how the subroutine should read, not two different declarations.

2 Likes

IT WORKS! Thank you so much, you have no idea how much time you just saved me.

2 Likes

This topic was automatically closed 24 hours after the last reply. If you want to reopen your WiP, contact the moderators.