Drop zone doesn't recognize instanced objects but can recognized objects when added manually.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mortycaii

extends Node2D

var level = 1
var level_tasks =
var num_of_tasks

var rng = RandomNumberGenerator.new()

var num1
var num1_addends =
var num2
var num2_addends =
var num3
var num3_addends =

var pill_scene = preload(“res://Pill.tscn”)

#NOTES ON CODE
#each level has 2 or 3 tasks, all of which are addends of the level_sum; thus, all tasks sum up to level_sum

level_sum increases by 5 per level [ex. level 1 = 20, 2 = 25, 3 = 30, …]

sample tasks [ex. level 1 = 20; 12, 8 or 8, 4, 8; code won’t output 1, 19 or 2, 18]

no tasks can include 1 or 2 as well

#each task is made out of 2 addends

ex. level 1 = 20; tasks = 12, 8; addends = 4,8,6,2

out of all different addend combinations, the code randomly chooses 1 feasible combination

##MAIN GAME LOOP
#if timer is not yet done:

let timer continue

if px done

transition level

#else:

end game

#func _ready for initializing scenes or for testing codes

func _ready is called when nodes are ready

func _ready():

$Pill.connect(“number_inside”,$DropZone,“_on_Pill_number_inside”)

ready_level(25)
#CODE FOR TESTING; code below should be in func produce_child_nodes
var all_num_addends = num1_addends + num2_addends + num3_addends
print(all_num_addends)
print("")
for i in all_num_addends:
	var pill = pill_scene.instance()
	if all_num_addends.find(i) == 0:
		print(i)
		pill.position.x = 500
		pill.position.y = 400
		pill.number_held = i
		pill.set_collision_layer(2)
		add_child(pill)
	if all_num_addends.find(i) == 1:
		print(i)
		pill.position.x = 600
		pill.position.y = 400
		pill.number_held = i
		pill.set_collision_layer(2)
		add_child(pill)
	if all_num_addends.find(i) == 2:
		print(i)
		pill.position.x = 700
		pill.position.y = 400
		pill.number_held = i
		pill.set_collision_layer(2)
		add_child(pill)
	if all_num_addends.find(i) == 3:
		print(i)
		pill.position.x = 500
		pill.position.y = 500
		pill.number_held = i
		pill.set_collision_layer(2)
		add_child(pill)
	if all_num_addends.find(i) == 4:
		print(i)
		pill.position.x = 600
		pill.position.y = 500
		pill.number_held = i
		pill.set_collision_layer(2)
		add_child(pill)
	if all_num_addends.find(i) == 5:
		print(i)
		pill.position.x = 700
		pill.position.y = 500
		pill.number_held = i
		pill.set_collision_layer(2)
		add_child(pill)

#function for preparing a level, to be put in the main game loop
func ready_level(level):
#determine Level tasks, & num of tasks
var level_sum = 15 + (level*5)
level_tasks = determine_level_tasks(level_sum)
num_of_tasks = level_tasks.size()
print("LEVEL “,level,” TASKS: ",level_tasks)

assign num variables to task & determine addends of each task

num1 = level_tasks[0]
num2 = level_tasks[1]
if num_of_tasks == 3:
	num3 = level_tasks[2]
determine_addends_of_tasks(num1,num2,num3)

produce_child_nodes(level_tasks)

put inside ready_level

func produce_child_nodes(level_tasks):
for i in level_tasks:
print(i)
#test

#used to determine all addends of all tasks
func determine_addends_of_tasks(num1,num2,num3):
for i in range(1,num1): #addends of first number
var remainder = num1 % i
if i + remainder == num1:
num1_addends.push_back([i,remainder])
for i in range(1,num2): #addends of second number
var remainder = num2 % i
if i + remainder == num2:
num2_addends.push_back([i,remainder])

#choose final task addends to be put in the level
rng.randomize()
num1_addends = num1_addends[rng.randi_range(0,num1_addends.size()-1)]
num2_addends = num2_addends[rng.randi_range(0,num2_addends.size()-1)]

if num_of_tasks == 3:
	for i in range(1,num3): #addends of third number
		var remainder = num3 % i
		if i + remainder == num3:
			num3_addends.push_back([i,remainder])
	num3_addends = num3_addends[rng.randi_range(0,num3_addends.size()-1)]

#determine all addends of a level sum
func determine_level_tasks(level_sum):

determine 2-addend-combinations of level sum

for i in range(1,level_sum):
	var remainder = level_sum % i
	if i + remainder == level_sum:
		level_tasks.push_back([i,remainder])

add 3-addend-combinations of level sum in the array

var arr_size = level_tasks.size()
for i in level_tasks:
	if level_tasks.find(i) == arr_size:
		break
	level_tasks.push_back([i[0]/2,i[0]-i[0]/2,i[1]])

remove combinations with 1 & 2

var del_from_tasks = []
for i in level_tasks:
	if i.has(1) or i.has(2):
		del_from_tasks.push_back(i)
for i in del_from_tasks:
	level_tasks.erase(i)

out of all combinations in the array, choose 1

rng.randomize()
level_tasks = level_tasks[rng.randi_range(0,level_tasks.size()-1)]

num_of_tasks = level_tasks.size()
return level_tasks

All this code will surely be usefull, but can You narrow it down to parts regarding question topic ? And say something more about what this dropzone is supposed to be and how it should work in project ?

Inces | 2022-01-29 09:22