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
I have reviewed and marked up code with some suggestions and how to interpolate the colors:
Build_Tree.gd
Tree.gd