Changing prices for a mock stock trading game

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By CatRass

I want to make a mock stock trading/crypto trading game using godot, but I’m unsure how to generate the prices. I made a prototype using python, in which it was decided by a random number generator, where the values change depending on the balance you have (the more money you get the higher the prices can go), but I’m not really sure how to make that in GDscript. What are my options?

:bust_in_silhouette: Reply From: njamster

Something like this?

var balance = 1337

const MINIMUM_PRICE = 100 # no stock will be priced cheaper than this
const FACTOR = 2 # the highest possible price will be 2x your balance

func ready():
    randomize() # you have to call this only once

func generate_stock_price():
    var price = rand_range(MINIMUM_PRICE, balance * FACTOR)
    return price

If that is not what you were after, feel free to post your python code for clarification.

No that’s actually better than what I had made in python, thank you so much! I have another question relating to this which is a little more complex. As the values change I’d want to display them in a graph, like a stock price chart does, updating every time a price changes. What would I need to use to accomplish this? (What tools and possibly code, if it’s not too much of a hassle explaining)

CatRass | 2020-05-13 12:39

The most simple solution would be using a Line2D. For more complex drawings, read this part of the documentation. Then check out this repository, this method is doing what you want. The README only mentions Godot 3.1, but I’d guess it still works in 3.2 and if not, it should be easy to port.

njamster | 2020-05-13 12:50

Thank you so much for all your help, one small question to finish up. How would I display the price, as in the numerical price, on my game scene?

CatRass | 2020-05-13 23:52

With a Label-Node

func update_price(price : float) -> void:
	$PriceLabel.text = "%.2f" % price

(Label Node) (Formatting Strings)

whiteshampoo | 2020-05-14 07:41

Hi, thank you for the help, but I’m unsure of how to use this code. Do I attach this to the script to the Node2D, or the PriceLabel. If to the Label, what do I name it? Sorry for the bother, I couldn’t find anything in the attached docs.

CatRass | 2020-05-15 08:20

If you don’t change the script you have to attach it to the parent-node of a Label-node called “PriceLabel” (case-sensitive!) - the dollar-sign is just a shorthand symbol for get_node. If you want to attach it to the Label-node itself, it would be:

extends Label

var price = 1337

func update_price(price : float) -> void:
    text = "%.2f" % price

It all depends on what you’re trying to do. As long as all paths are correct the script will work, no matter where it’s located. The text is a property of your Label, so you need to get the label first. The price is a variable defined in a script, so you need to get the node this script is attached to first. How exactly you get those two nodes depends to which node your script for updating the price is located and how your trees looks.

njamster | 2020-05-15 09:36

I’ve tried and double checked my code, and I think it’s something to do with the parent node (my Node2D). The Label-node is called “PriceLabel”, and when I run the scene it returns blank. I’m not sure what else I can do. I’ve attached screenshots of my scene, code and the game being run.

CatRass | 2020-05-15 10:11

You never call the methods! If you only want to do it once do this:

func _ready():
    var price = generate_stock_price()

If you would like to call it every frame (unlikely, but I’ll include it nonetheless) :

func _ready():

func _process(delta):
    var price = generate_stock_price()

njamster | 2020-05-15 10:38

Aaaaaaa sorry I’m new to Godot coding so I was confused at how to call methods/functions properly, my bad. Thank you for your incredible help, it’s all working now!

CatRass | 2020-05-15 10:57

Another small question, if I wanted to update the values at specifically lets say, once per minute, how would I achieve this?

CatRass | 2020-05-16 10:20

  1. Add Timer-Node
  2. Change Timer-properties to your needs (Autostart yes, One Shot no)
  3. Connect timeout()-Singal to a script
  4. Put your code into the new method/function, to update every x-time

whiteshampoo | 2020-05-16 11:41

How would I ‘add’ the Timer-Node? Is it just by putting in the autostart (set_autostart(10))?

Edit: Sorry, I understood what you were saying, my bad

CatRass | 2020-05-17 03:24