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 (48 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,329 points)

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.