0 votes
by (180 points)
Hello Community

Since my last question was answered rather quickly I wanted to ask you again for another solution to a problem I ran into lately:

I want my game to have dynamic events, for example if you go into the "Go for a Walk" Passage, a random event including a random image should appear. Furthermore I have written a code that prevents the game from showing the same event twice until all possible events have been encountered:

The following Code is simply to create an array with 10 variables and then randomly choosing one which will be deleted afterwards.

<<if $Auswahlarray == 0>>
   <<set $Auswahlarray to ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']>>
<<elseif $Auswahlarray == undefined>>
   <<set $Auswahlarray to ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']>>
<</if>>
<<set _Verbot to $Auswahlarray.random()>>
<<for _i = 0; _i <= $Auswahlarray.length; _i++>>
    <<if $Auswahlarray[_i] == _Verbot>>
        <<set $Auswahlarray.delete(_Verbot)>>\
    <</if>>
<</for>>

Now I want to use the "_Verbot" Variable, which should hold one of the numbers as its value, as the dynamic part for a picture link. For example If I have a picture called: Walkencounter1. I want to show it like:

[img[Walkencounter_Verbot.png]]

So basically I have ten "WalkencounterX" Pictures, while X should be filled dynamically. How do I do that in Sugarcube?

1 Answer

+1 vote
by (68.6k points)

What you're doing there is a very inefficient way to accomplish your goal and also incorrect in various ways.

Try something like the following:

<<if ndef $Auswahlarray or $Auswahlarray.length is 0>>
   <<set $Auswahlarray to ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']>>
<</if>>
<<set _Verbot to $Auswahlarray.pluck()>>

And to use _Verbot in the [img[]] markup:

[img["Walkencounter" + _Verbot + ".png"]]

 

...