Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

How to "Switch Equipment" in Surgarcube?! D:

Infinite, battle tower kind of game. I managed to finish most vital aspects such as stats you can increase in a style similar to Dark Souls and a combat system with not only skills, but also evasion and critical. Now, I'm trying to finally build a Shop where player can buy equipment that they can equip to enhance their stats even further. It's kind of hard to explain so I'll just leave a part of the code here and explain as I go.

This is the Inventory screen
<<if $basic_iron_sword is true>>Iron Sword(+5 Attack) <---It is automatically obtained and automatically equipped when game first start.
A cheap and dull iron sword you bought at a sale.<</if>>
<<if $equiped_basic_iron_sword is true>>You're already wielding this weapon.<<else>>Equip basic_iron_sword<</if>>

<<if $golden_sword is true>>Golden Sword(+7 Attack, +3 Magic Effectiveness)<----Bought from shop
A golden sword you bought from the ghost merchant. He assured you that the thing is tougher than it looks.<</if>>
<<if $equiped_golden_sword is true>>You're already wielding this weapon.<<else>>Equip golden_sword<</if>>
Base<----This button returns the player back to their base, the HUB basically. So no infinite <<return>> loop.

Now say, I want to equip the golden sword. So, I click on Equip which takes me to this passage, "Equip golden_sword"
<<set $playerattack += 7>><<set $playermagicattack += 3>><<set $equiped_golden_sword to true>><<goto "Player Bag">><----This will equip the thing. Set all that stats and then immediately return player back to inventory.

With that example written, my question is. How do I get the Basic Iron Sword to auto-unequip when I switch to another item? I mean, I can obviously do

<<set $playerattack += 7>><<set $playermagicattack += 3>><<set $equiped_golden_sword to true>><<set $equiped_basic_iron_sword to false>><<if $equiped_basic_iron_sword is false>><<set $playerattack -= 5>><</if>><<goto "Player Bag">>

But if I use this method, then it will get out of control real quick if I were to have like 20+ swords. Does anyone know a more, reasonable effective way to accomplish this goal?

Comments

  • Please use the code tag when posting code—it's C on the editor bar.

    You should always specify the version of the story format you're using. The syntax between different versions may vary, especially between major versions. In this case, the are significant differences between the current two major versions of SugarCube—the current v2 and the legacy v1.

    Before I get into your question, a tip. All conditional expressions—e.g. the expression used in an <<if>>—ultimately resolve to a boolean. Ergo, you do not need to test a variable holding a boolean value against the boolean literals. You should simply use the variable, possibly with the not operator if you want to test for the false state. For example:
    /* BAD */
    <<if $golden_sword is true>> ...
    <<if $golden_sword is false>> ...
    
    /* GOOD */
    <<if $golden_sword>> ...
    <<if not $golden_sword>> ...
    


    Without radically altering the design of what you're current doing, I'd probably suggest using a widget to encapsulate the (un)equip logic, so you can easily reuse that code without having to copy/paste it everywhere. I'm also going to suggest not navigating to another passage and using <<goto>> as a return method to handle the (un)equipping as that's both unnecessarily complicated and bloats the story history for no good reason.

    Additional notes:
    • Within my inventory passage examples, I made the <<if $equiped_…>> invocations children of the <<if $…>> ones, as it didn't seem to make sense for them to be siblings.
    • You misspelled equipped as equiped everywhere.


    Example for SugarCube v2

    Create a new, separate passage and add the tag widget, then place something like the following within:
    <<widget "equipweapon">><<silently>>
    
    	/* First.  Unequip the current weapon. */
    	<<if $equiped_basic_iron_sword>>
    		<<set $playerattack -= 5>>
    		<<set $equiped_basic_iron_sword to false>>
    	<<elseif $equiped_golden_sword>>
    		<<set $playerattack -= 7>>
    		<<set $playermagicattack -= 3>>
    		<<set $equiped_golden_sword to false>>
    	<</if>>
    
    	/* Second.  Equip the new weapon. */
    	<<switch $args[0]>>
    	<<case "basic_iron_sword">>
    		<<set $playerattack += 5>>
    		<<set $equiped_basic_iron_sword to true>>
    	<<case "golden_sword">>
    		<<set $playerattack += 7>>
    		<<set $playermagicattack += 3>>
    		<<set $equiped_golden_sword to true>>
    	<</switch>>
    
    <</silently>><</widget>>
    


    How you'd use the <<equipweapon>> widget in your inventory passage:
    <<if $basic_iron_sword>>''Iron Sword (+5 Attack)''
    A cheap and dull iron sword you bought at a sale.
    <<if $equiped_basic_iron_sword>>You're already wielding this weapon.<<else>><<link "Equip">><<equipweapon "basic_iron_sword">><</link>><</if>>
    <</if>>
    <<if $golden_sword>>''Golden Sword (+7 Attack, +3 Magic Effectiveness)''
    A golden sword you bought from the ghost merchant. He assured you that the thing is tougher than it looks.
    <<if $equiped_golden_sword>>You're already wielding this weapon.<<else>><<link "Equip">><<equipweapon "golden_sword">><</link>><</if>>
    <</if>>
    
    [[Back to game|Base]]
    


    Example for SugarCube v1

    Create a new, separate passage and add the tag widget, then place something like the following within:
    <<widget "equipweapon">><<silently>>
    
    	/* First.  Unequip the current weapon. */
    	<<if $equiped_basic_iron_sword>>
    		<<set $playerattack -= 5>>
    		<<set $equiped_basic_iron_sword to false>>
    	<<elseif $equiped_golden_sword>>
    		<<set $playerattack -= 7>>
    		<<set $playermagicattack -= 3>>
    		<<set $equiped_golden_sword to false>>
    	<</if>>
    
    	/* Second.  Equip the new weapon. */
    	<<if $args[0] is "basic_iron_sword">>
    		<<set $playerattack += 5>>
    		<<set $equiped_basic_iron_sword to true>>
    	<<elseif $args[0] is "golden_sword">>
    		<<set $playerattack += 7>>
    		<<set $playermagicattack += 3>>
    		<<set $equiped_golden_sword to true>>
    	<</if>>
    
    <</silently>><</widget>>
    


    How you'd use the <<equipweapon>> widget in your inventory passage:
    <<if $basic_iron_sword>>''Iron Sword (+5 Attack)''
    A cheap and dull iron sword you bought at a sale.
    <<if $equiped_basic_iron_sword>>You're already wielding this weapon.<<else>><<click "Equip">><<equipweapon "basic_iron_sword">><</click>><</if>>
    <</if>>
    <<if $golden_sword>>''Golden Sword (+7 Attack, +3 Magic Effectiveness)''
    A golden sword you bought from the ghost merchant. He assured you that the thing is tougher than it looks.
    <<if $equiped_golden_sword>>You're already wielding this weapon.<<else>><<click "Equip">><<equipweapon "golden_sword">><</click>><</if>>
    <</if>>
    
    [[Back to game|Base]]
    
  • I gotta say, my silly mistake of the word equip aside. I just wanna say thank you for your help. Especially with the "widget" feature. I was not aware of its existence. Not only did you solved my equipment issue, this also gave me that, basis that I needed to create my "Learn Skill" system. Thanks again!
Sign In or Register to comment.