0 votes

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?

in Engine by (49 points)

1 Answer

+3 votes

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.

by (10,423 points)

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)

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.

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?

With a Label-Node

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

(Label Node) (Formatting Strings)

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.

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.

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.
Code
Scene
Run game

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

func _ready():
    randomize()
    var price = generate_stock_price()
    update_price(price)

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

func _ready():
    randomize()

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

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!

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

  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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.