Easier way to allow a player to swap equips without unequipping item first in Sugarcube 2.35.0/Tweego

FruitSmoothie

Well-Known Member
Jan 22, 2019
1,656
2,046
Described most of the issue in the thread title. Basically I want to allow the player to swap from say an iron helmet to a steel helmet without unequipping the iron helmet first. I have no coding experience and just really started with Sugarcube/Tweego a couple weeks ago, so I'm mostly on the basics atm (No javascript yet). My current solution is:

You don't have permission to view the spoiler content. Log in or register now.

Basically I create a bunch of IF statements checking what the current weapon/armor piece, then I return it to the player inventory and return all the stats that previous gear piece gave the player before adding the new piece. It seems to work okay, issue is I can see this being obnoxious to scale up. Any time I add a new equip/weapon, I have to go through all the other armor/weapon pieces that use the same slot and add it to their if statements.

At a hundred armor sets, It'd be a nightmare to add new sets. I know the simple solution is just to make the player unequip a slot before adding a new piece of gear to that slot, but I want to avoid that if possible. I guess one extra click wouldn't be the end of the world if there's no easy way to scale this up without each gear piece taking longer to add to the game. I guess I could set up a widget or something to make it a bit better but I'm sure there's a much more efficient solution.

Also I know I don't need the getcheckweight in every if statement and could just put it at the end, that was something new I was testing and I'll be fixing that soon.
 
Last edited:

aukamo92

Newbie
Dec 8, 2018
17
7
FruitSmoothie let
`Armor` be a class with fields: weight, resistance.
`Resistance` is a class with fields: phisical, fire, ice, electic, stun.
`ArmorSet`: helmet, body, bracers, boots, etc.

Then you need a variable of class `ArmorSet` and implement `swap_<piece name>` for each of helm, body, etc. or one method if logic is totally same.

Ways to setup custom JS can be found here:
*
*
*
 
  • Like
Reactions: FruitSmoothie

FruitSmoothie

Well-Known Member
Jan 22, 2019
1,656
2,046
FruitSmoothie let
`Armor` be a class with fields: weight, resistance.
`Resistance` is a class with fields: phisical, fire, ice, electic, stun.
`ArmorSet`: helmet, body, bracers, boots, etc.

Then you need a variable of class `ArmorSet` and implement `swap_<piece name>` for each of helm, body, etc. or one method if logic is totally same.

Ways to setup custom JS can be found here:
*
*
*
Thanks for the answer. Javascript is still pretty intimidating for me and I'll start trying to figure it out on the side for my next project. I think for now I'll just create a widget to cut down on a lot of the repetition of the current system so I don't get stuck for too long. That swap function seems really useful though, maybe I can work on that when I feel a bit braver. I'll start going through a bit every day.
 

Winterfire

Forum Fanatic
Respected User
Game Developer
Sep 27, 2018
5,048
7,396
Easiest way (Logic-wise, I do not know the engine) is to store the currently equipped item in a temp variable inside the function used to swap the items, remove the current equipped item, wear the new one in its place, then add the old one stored in the temp variable as a new item.

That is needed only if your item is unique somehow (Renamed, has damage, and so on), otherwise you can simply use the id to add a new item rather than storing the whole item in a variable.
 

aukamo92

Newbie
Dec 8, 2018
17
7
I do not know the engine
Same to me, I got quick check on syntax/API and few search requests. Best I can imagine with this piece of information is to extent engine with required functions.

On the other hand, I'm not sure plain ID will be helpful. Meaningful names will help in terms of readability. Storing objects can be as fast as storing reference to it (depends on implementation, you can always clone things to add overhead).
 

guest1492

Member
Apr 28, 2018
322
272
There are many possible solutions for what's asked. The most proper way is probably to set up classes for a person or player as well as equipment and maybe an inventory class as well. However, most Twine games on this forum don't go to such lengths.

I do recommend that you learn something about programming but for now the most simple thing for you to do is to set a Twine story variable for what a player has equipped, similar to your variable for a player inventory.

I'm on my phone so it's inconvenient for me to write actual and accurate code. Basically you want something that will let you do this:

Code:
<<widget equip>><<silently>>
    <<set _slot = $args[0].slot>>
    <<for _i, _item range $equipped>>
        <<if _slot == _item.slot>>
            <<unequip _i>> /* write a widget to unequip stuff and adjust player stats */
            <<break>>
        <</if>>
    <</for>>
    <<set $equipped.append($args[0])>>
    <<for _stat, _value range $args[0].stats>>
        <<set $player[_stat] += _value>>
    <</for>>
<</silently>><</widget>>
 
  • Like
Reactions: FruitSmoothie

FruitSmoothie

Well-Known Member
Jan 22, 2019
1,656
2,046
Here is something more detailed since I wrote it on the computer:
You don't have permission to view the spoiler content. Log in or register now.
Thanks, I can understand most of that.

I'm learning a lot of the stuff in Sugarcube/Twine is just working around how the history/back button cloning shit every passage change makes things more difficult. A lot of the basic examples given in videos don't take that into account or warn about that at all. I only just found out how obnoxious it is a while ago. I was like "Why do people go through all that trouble to do something that can be done so much easier?". Going to have to rework a lot of crap I guess, at least I was prepared for that lol.

My inventory basically tracks how much of an item the player has with just like $playerironswordamount and I update that whenever I get more or less. So the player really only has one of any item but it won't be deleted until that amount hits 0. Did a lot of janky stuff like that instead of actually adding multiple items to the array and stuff lol.

You don't have permission to view the spoiler content. Log in or register now.

I already started reworking the combat to be better about that and handle new stuff better. Probably still not great though. Man, I wish there were some more advanced videos for making rpgs with sugarcube. Not too many people seem to use it for more complex rpgs, kinda makes me worried that there's a reason for that.

At least the twine cookbook and w3schools are better than the sugarcube documentation for a beginner.
 
Last edited: