This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
0 votes

I'm making a game with a "price" variable, and every time you buy a upgrade, the price goes * 1.5, but the price that it costs is different then the price that shows at the button, because i set to every time the price variable hits more than "999", it turns to 1, but at this case, the price is 1.5, and the price showing at the button is just "1k", and when it gets doubled, the button price get as "2k" and the real price as "3000".

How do i fix this?

Godot version 3.0
in Engine by (22 points)

supply sample code and variable declarations.

var preco = 100
var level = 0
var valor = ""

signal comprou

func _process(_delta):
    if preco >= 999 and valor == "":
        valor = "K"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "K":
        valor = "M"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "M":
        valor = "B"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "B":
        valor = "T"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "T":
        valor = "Q"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "Q":
        valor = "QQ"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "QQ":
        valor = "S"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "S":
        valor = "SS"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "SS":
        valor = "OC"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "OC":
        valor = "N"
        preco = 1
        _ready()

func _ready():
    $Preco.text = "%d" % [floor(preco)] + (valor)
    $Level.text = String(level)

func _on_CoinBar_compra_efetuada_power():
    preco = preco * 1.5
    level = level + 1
    _ready()
    emit_signal("comprou")

"preco" = price
"valor" = value
"compra" = buy
"onCoinBarcompraefetuada_power" = a signal when the shop sell the upgrade

2 Answers

0 votes

Ok a better way is to just use an array having the limits and scale format. Also do not call _ready() as a function. Normally they are called by events.

E.g.

extends Button

var preco = 100
var level = 0
var valor
var scale

signal comprou

func _process(_delta):
    pass

func _ready():
    scale=0
    valor = [[1,"%10.0f"],[1000.0,"%10.2fK"],[1e6,"%10.2fM"],[1e9,"%10.2fB"],[1e12,"%10.2fT"]]
    format()    
    pass

func format():
    var  prcf
    # check if price reached next scale
    if preco>=valor[scale+1][0]:
        prcf=preco/valor[scale+1][0]
        scale+=1
    else:
        prcf=preco/valor[scale][0]

    text =  valor[scale][1] % [prcf]


func _on_CoinBar_compra_efetuada_power():
    preco = preco * 1.5
    level = level + 1
    format()    
by (810 points)
0 votes

i dont know how node hierarchy on your project so i just make it more easier for me,
you can try this

extends Node2D

var preco = 100
var level = 0
var valor = ""


func _process(_delta):
    if preco >= 999 and valor == "":
        valor = "K"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "K":
        valor = "M"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "M":
        valor = "B"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "B":
        valor = "T"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "T":
        valor = "Q"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "Q":
        valor = "QQ"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "QQ":
        valor = "S"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "S":
        valor = "SS"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "SS":
        valor = "OC"
        preco = 1
        _ready()
    elif preco >= 999 and valor == "OC":
        valor = "N"
        preco = 1
        _ready()


func _ready():
    $Preco.text = str(stepify(preco, 0.1), valor)
    $Level.text = String(level)
    print(preco)


func _input(event):
    if event.is_action_pressed("ui_up"):
        level_up()


func level_up():
    preco = preco * 1.5
    level = level + 1
    _ready()

im using the button to call the level_up price instead of calling a signal, because im not using manual signal alot, but i think you can implement this too on your project

by (24 points)

It worked, but the price that shows at the button is different than the real price that it costs, ill try to fix it myself

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 Frequently asked questions and 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 [email protected] with your username.