Hello, I am creating a project that has 3 different currencies and I need each currency to use a system that allows managing big numbers (code here).
I want to have the option to use this code with any currency to manage the calculations and then save the result in separate variables to display it on screen. Let me know if you need more info
Can you be more explicit on what you need? With which method do you have problems with? What is it supposed to recieve and whats is it supposed to do with it
I have no problem with the code. What I need is to find a way where I can use that script to calculate when the user earns each coin, save the result in a variable, and update that variable when needed. The data is of type String, and the variable arrCoin is a setter that handles the result of the process and displays it on the screen.
This is the way I’m trying to approach:
class_name coin1
extends TouchScreenButton
@onready var arrCoin1: String
func _ready():
print(arrCoin1)
#Using this inside the ready function is probably
#not the most appropriate, but it's just illustrative.
func _on_pressed():
#call NumberSystem script and add 1 coin per click
#save the result on var arrCoin1
queue_free()
-----------------------------------------------
#the same with coin2 and coin3
-----------------------------------------------
class_name NumberSystem
extends Node2D
#receives the current coin is calling the method
#check the current amount of that coin
#process and return the result for coin script to save it
I don’t know if this approach is viable, or if it is better to create an individual system for each currency. That’s what I’m trying to figure out
Ok, I do it using custom signals to send the amount of coin to NumberSystem script, process it and then call a function that send the result to coin script and work pretty good.
But how can I create a signal on an instantiated node? Is this possible?
You could extend RefCounted instead of Node2D, if given a class_name you can make new “BigNumbers” in any class
# big_number.gd
extends RefCounted
class_name BigNumber
var total: Array = []
func _init(value: String = "0") -> void:
total = arrayNum(value)
func arrayNum(num : String):
# etc ...
# could be `_to_string()` for implicit conversions, like in `print()`
func display_number():
# etc ...
func add(num : Array):
# etc ...
class_name coin1
extends TouchScreenButton
@onready var arrCoin1: String
# Best define these in a Autoload/global, illustrative purposes only
var copper = BigNumber.new()
var silver = BigNumber.new()
var gold = BigNumber.new()
func _on_pressed():
copper.add(BigNumber.new("1"))
queue_free()
I have problems with this, the total array seems to be null… I make improvements using signals with my current script. Now, the problem is that I need to send a signal by code from a node that needs to be instantiated. Don’t know if it’s possible
var node = YourNode.instanitate()
node.your_signal.connect(method_to_connect_to)
# 'your_signal' is the name of your signal and 'method_to_connect_to' is the name of the method to connect to
Is there a way to make the signal connect when the user taps the instance? I made the connection inside _ready() (in NumberSystem script), but it only connects when I process another coin, which works fine.
I’m really not sure if the signal is working correctly or not.
Edit: it’s not connecting, I test it alone and nothing happen
class_name main_button
@onready var buttonSeed = preload("res://scenes/seed.tscn")
@onready var world:Node2D = get_tree().current_scene
(...)
func _on_timer_main_incremental_timeout():
random_location()
func random_location():
(...)
for i in range(5):
(...)
var objInst = buttonSeed.instantiate()
var tween : Tween = create_tween().set_trans(Tween.TRANS_SPRING).set_ease(Tween.EASE_OUT)
tween.tween_property(objInst, "position", finalPoint, sec).from(initPoint)
world.add_child(objInst)
(...) its irrelevant code
So, I need to make the connection where buttonSeed it’s created or send buttonSeed instance to the script I need to connect?
Edit: buttonSeed script works when it’s created, as you can see on the video, queue_free() works well. Maybe I have to send the node from buttonSeed script (this script is on buttonSeed node itself) to bigNumber script?
This is a different button from the one I pointed out. And this one is added as a child so it can emit signals. Notice you use buttonSeed.instantiate() here, in the for loop that makes 5 buttonSeeds. The onready makes one separate buttonSeed that isn’t added as a child so it is mostly inoperable.
Maybe you want to add your connection to these objInst values?