0 votes
by (1.1k points)
edited by

It's something really curious, the push does not seem to work as it should.

E.g.

In the clothing store, the tops.

<<for _top, _clothesTops range $avaliableTops>>\
	<<if $inventory.money lt $avaliableTops[_top].value>>\
		• Buy <<= _clothesTops.name>> - ''@@.a1;Insufficient money for this one.@@''
	<<else>>\
		<<capture _top>>\
			<<nobr>>\
				• <<link _clothesTops.name>>
					<<set $inventory.money -= $avaliableTops[_top].value>>
					<<set $player_Tops.push($avaliableTops[_top])>>
					<<set $avaliableTops.shift(_top)>>
					<<replace "#content-2">>
						<<include "Clothes">>
					<</replace>>
				<</link>>
				''$<<= $avaliableTops[_top].value>>''
				<<if $avaliableTops[_top].type == 'casual'>>
					@@.a2;(''<<= _clothesTops.type>>'')@@
				<<elseif $avaliableTops[_top].type == 'sport'>>
					@@.a5;(''<<= _clothesTops.type>>'')@@
				<<elseif $avaliableTops[_top].type == 'going out'>>
					@@.a1;(''<<= _clothesTops.type>>'')@@
				<</if>><br>
			<</nobr>>\
		<</capture>>\
	<</if>>\
<</for>>\

Occasionally an element of this code fails, the shift, it is curious that all elements work correctly except the shift it does not delete the indicated element but the first one from the list. The temporary variable with the capture works normally with the other elements of the code though.

Maybe it's something related to the browser? I have tested in two different browsers and the same seems to happen.

1 Answer

+1 vote
by (68.6k points)
selected by
 
Best answer

You're using the <Array>.shift() method improperly—it takes no parameters and removes, and returns, the first member of the array.  To delete a specific index within the array, you likely want SugarCube's <Array>.deleteAt() method.

 

Find:

<<set $avaliableTops.shift(_top)>>

Replace wIth:

<<set $avaliableTops.deleteAt(_top)>>

 

...