changing color of another node using singleton

138 views Asked by At

I am trying to change the color value of a create_circle function inside of an instanced "Tree" node from within another script "Build_Tree". There should be a series of Red circles to white circles(Red on the bottom, gradually turning white as they get higher), however there are a bunch of black circles. I've tried setting a Color8(0, 0, 0) as a global variable in Tree.gd and setting a series of R G and B values that are accessed by a singleton. Funnily enough, there was ONE that spawned as the correct color when I set the pos_x and pos_y of the object to 0. But obviously that makes one circle on the top left corner and is not very impressive on it's own

Here is the code:

Build_Tree.gd

builder() stores the color gradient and the positions for a split in the tree

func builder():
    var color_gradient_cache = 255
    var radius = 36
    var anchor_position = 0
    var position_cache_storage = []
    for i in 3:
        if i == 0: 
            position_cache_storage.append(create_section(color_gradient_cache, radius, position_cache))
        elif i == 1:
            anchor_position = 1
            position_cache_storage.append(create_section(color_gradient_cache, radius, position_cache_storage[0], anchor_position))
        elif i == 2:
            anchor_position = 2
            position_cache_storage.append(create_section(color_gradient_cache, radius, position_cache_storage[0], anchor_position))

create_section creates a 3 section bit of circles then returns the last position so I can add a split there

func create_section(color, radius, pos,  anchor_position = 0):
    var anchor_angle
    print(str(anchor_position))
    var current_position = pos
    # 3-4 section of limbs
    for i in rand_range(3,4):
        if anchor_position == 0:
            anchor_angle = deg2rad(-rand_range(75, 105))
            current_position = Vector2(current_position.x + ((radius-10) * cos(anchor_angle)), current_position.y + ((radius-10) * sin(anchor_angle)))
            print(str(current_position))
            create_tree(radius, current_position.x, current_position.y, color)
        elif anchor_position == 1:
            anchor_angle = deg2rad(-rand_range(180, 150))
            current_position = Vector2(current_position.x + ((radius-10) * cos(anchor_angle)), current_position.y + ((radius-10) * sin(anchor_angle)))
            print(str(current_position))
            create_tree(radius, current_position.x, current_position.y, color)
        elif anchor_position == 2:
            anchor_angle = deg2rad(-rand_range(75, 45))
            current_position = Vector2(current_position.x + ((radius-10) * cos(anchor_angle)), current_position.y + ((radius-10) * sin(anchor_angle)))
            print(str(current_position))
            create_tree(radius, current_position.x, current_position.y, color)
    return current_position

creates each circle

func create_tree(radius, pos_x, pos_y, color):
    # creates the tree (circle) when called
    print(str(color))
    var TreeObjVar = get_node("/root/Tree_Gd")
    TreeObjVar.pos_x = pos_x
    TreeObjVar.pos_y = pos_y
    TreeObjVar.R = color
    TreeObjVar.G = 0
    TreeObjVar.B = 0
    print (TreeObjVar.G)
    var TreeScene = load("res://Tree.tscn")
    var TreeObj = TreeScene.instance()
    TreeObj.set_position(Vector2(pos_x, pos_y))
    get_node("/root/").call_deferred("add_child", TreeObj)

Tree.gd

extends Node2D

# Declare member variables here. Examples:
var r = 18.0
var pos_x = 0
var pos_y = 0
var R = 0
var G = 0
var B = 0
# Called when the node enters the scene tree for the first time.
func _ready():
    set_process(true)
    randomize()
    pass # Replace with function body.

func _draw():
    draw_circle( Vector2(pos_x, pos_y), r, Color8(R, G, B))


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    if r < 36.0:
        r += 3.0
    print(str(R) + " " + str(G) + " " + str(B))
    update()

I hope that this all sounds understandable, I don't know much about what I'm doing but I have been an on and off game designer for too long and I figured I'd ask for a little bit of help instead of giving up again

1

There are 1 answers

1
hola On BEST ANSWER

I have reviewed and marked up code with some suggestions and how to interpolate the colors:

Build_Tree.gd

extends Node2D


## Use exports to parameterize your tree builder node.
export(Color) var color_start = ColorN('red')
export(Color) var color_stop = ColorN('white')
export(float) var radius = 36.0

## If you are going to apply a gradient, you can
## use the gradient resource as a helper.
## I'll show you how to use `Gradient` below.
export(Gradient) var gradient = Gradient.new()


var position_cache = Vector2()

func _ready():
    randomize()
    position_cache = Vector2(position.x, position.y)
    set_process(true)
    set_process_input(true)
    create_small(color_start, color_stop, 36.0, position_cache)

## If you go the exports route you can remove
## these parameters and just use `self`'s properties.
func create_small(color_start, color_stop, radius, pos):
    # anchor_angle holds the random number that holds where the tree deviates from the center
    var anchor_angle

    ## I added iterations here to use it as the weight/offset in
    ## the color lerping functions below.
    var iterations = 3.0
    for i in range(int(iterations)):

        ## Color can be changed here.
        ## `Color` provides the `Color.linear_intepolate()` method to
        ## lerp between two colors:
        create_tree(color_start.linear_interpolate(color_stop, i / iterations))
        ## Alternatively, you can use the `Gradient` resource:
#       create_tree(gradient.get_offset(i / iterations))

        anchor_angle = deg2rad(-rand_range(75, 105))
        position_cache = Vector2(position_cache.x + (radius * cos(anchor_angle)), position_cache.y + (radius * sin(anchor_angle)))


## The `color` parameter should take a `Color` object.
## Or, if you go the exports route, remove the parameters and
## use `self`'s properties.
func create_tree(color):
    var TreeObjVar = get_node("/root/Tree_gd")
    var TreeScene = load("res://Tree.tscn")
    var TreeObj = TreeScene.instance()
    get_node("/root/").call_deferred("add_child", TreeObj)

    ## This only sets `R` on the Singleton `Tree`.
    ## On `Tree`, `R` is not used after initialization.
    TreeObjVar.R = color.r

    ## The color of the newly instanced `Tree` will now be updated.
    TreeObj.color = color

    ## What you want to do is use
    TreeObj.set_position(Vector2(position_cache.x, position_cache.y))

Tree.gd

extends Node2D

#global variables to be accessible by singleton
var pos_x = 0
var pos_y = 0

#radius starts small so it can increase for a flashy entry
var r = 3.0


## Remove these, access color via `color`
#color values start at 'black' so I don't get excited when things are going wrong
var R = 0
var G = 0
var B = 0

var color = Color8(R, G, B)

func _ready():
    set_process(true)

func _draw():
    draw_circle(Vector2(pos_x, pos_y), r, color)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
    if r < 36.0:
        r += 3.0
    update()