Help me with Gold, silver, copper system (Drafon Age setting)

Twine Version: 2.3.14
Story Format: SugarCube 2.34.1

Hello! I need help setting up a gold/silver/copper system. I want this:

1 gold piece = 100 silver pieces = 10,000 copper pieces

How do I apply this in twine? As in, how do I do to, for example, display the player money using this system? Let’s say the player has 2 gold, 57 silver, 590 copper. I want to display this as “2(g)57(s)590(c)” or something like that, I dont know. The money will be displayed at the sidebar.

Also, what is the code for when the player earn/loses money in this type of currency? For example, they do a quest and receive + 40 silver pieces. Something like <<set $money to $money +……???>> I have no idea how to do the calculation for this system

I want to make a game that takes places in Dragon Age world (a medieval fantasy game), their currency is called Sovereigns. 10,00 copper pieces = 1 silver piece. 100 silver pieces = 1 gold piece/Sovereigns.

What do you suggest?

(English is not my native language, sorry for any mistake)

(Before I start, I’m going to assume you typoed on the copper to silver conversion. According to the web, it’s 100 copper not 10,000 or 1,000 or whatever you attempted to write.)

I can’t help you with the sidebar portion of it, but I can help with the money conversion.

What I’d do is internally track all money as copper just to keep it simple. When you give 4 silver, give 400 copper instead and for 5 gold give 50000 copper.

For the code to turn that into the display that you want, add this function to your javascript passage.

window.getCoinString = function(money) {
	var gold = Math.floor(money / 10000);
	money -= gold * 10000;

	var silver = Math.floor(money / 100);
	var copper = money - (silver * 100);

	return gold + "(g)" + silver + "(s)" + copper + "(c)";
}

Using that, <<print getCoinString(123456)>> should return 12(g)34(s)56(c).

Edit: Made a mistake on the declaration of the function. Changed it to be added correctly to the window object.

2 Likes

Tracking all money as cooper is an excelent tip, I will do it! So, for the use of the money variable I know what to do now! The player starts with 50 silver pieces, so in the storyinit passage i have to put <<set $money to 500>>

Then, everytime the player earn something, i will use <<set $money to $money + x>>

But the display part I am still confused. I have to put your code in my story Javascript, yes? Im so so sorry, this seems exactly what i need but I am still very noob to twine. Considering that i will use the variable $money for money your code will still work? Or this have nothing to do with variables? I know how to put something in the sidebar, so dont worry about that ^-^ Is just the use of this code you gave me that I am not understanding exactly

First, I made a change to my code above. My example wouldn’t work because it wasn’t added to the global window object and therefore you couldn’t call it from inside twine script. Sorry about that.

And to answer your question, you just add the function to your javascript stuff inside twine. It’s this button that I point to in this image.

And then in your twine script you’d do something like

<<set $money = 123456>>

<<print getCoinString($money)>>

And it will display 12(g)34(s)56(c).

That’s about it. All the function does is conveniently spit out text of the value passed to it converted into gold, silver, and copper. You can pass it the $money (if that’s what you can use to track the player’s money) or you can pass it the value of, say, the price of a sword. So instead of hand coding the sword price as gold and silver, you can just display <<print getCoinString(40000)>> or <<print getCoinString(_sword_price)>> or whatever works for you.

2 Likes

it worked!! THANK YOU SO MUCH!

1 Like

One last question, Is there a way to change the colors of the “(g), (s), (c)?” So that in the display it appears colored. Thank you again for helping me!! You saved my life!

Replace the “return” line (the last line) of the function with this one.

	return gold + "@@color:#FFD700;(g)@@" + silver + "@@color:#C0C0C0;(s)@@" + copper + "@@color:#B87333;(c)@@";

I’m assuming you wanted their respective colors. Just replace the #RRGGBB parts of each section with a different color code or name (like "gold’) if you want to change the color. There’s a handy dandy reference right here for you so you don’t have to use trial and error.

1 Like

Youre amazing!! :DD