Item instantiate problem

Godot Version

4.3

Question

`
Recording 2025-09-18 173823

im making one an infinate run for mobile and my main problem is with speed_item and obsticale_item instantiate. I wanted them to be inside the road and one of my first thoughts was to add a area2d (yellow) and code the items to be instantiate inside it. here is the instantiate code:

func generate_items(player):
	items_generated = true
	
	# Get the collision shape of the ItemGenerator
	var collision_shape = $"Item-generator/CollisionShape2D"
	if not collision_shape:
		push_error("CollisionShape2D not found in ItemGenerator!")
		return
	
	var shape = collision_shape.shape
	if not shape is RectangleShape2D:
		push_error("ItemGenerator shape is not a RectangleShape2D!")
		return
	
	var rect = shape.get_rect()
	var area_global_pos = $"Item-generator".global_position
	var area_start_x = area_global_pos.x + rect.position.x
	var area_end_x = area_start_x + rect.size.x
	
	# Get the lane positions
	var lane1_y
	var lane2_y
    if road_type == "easy":
	# Easy road has only 2 ColorRects
	      lane1_y = $ColorRect.position.y
	      lane2_y = $ColorRect2.position.y
    else:
	# Medium road has 5 ColorRects
	      lane1_y = $ColorRect2.position.y
	      lane2_y = $ColorRect4.position.y
	var player_lane = player.lane
	var target_lane_y = lane1_y if player_lane == 0 else lane2_y

	var should_generate_obstacles = player.current_speed > 10

	var num_items = randi() % 3 + 2 
	
	for i in range(num_items):
		# Random X position within the ItemGenerator area
		var x_pos = randf_range(area_start_x + 50, area_end_x - 50)
		
		# Adjust probabilities based on player speed
		if should_generate_obstacles:
			# Normal mode: 30% chance for speed items, 70% for obstacles
			if randf() < 0.3:
				var item = SPEED_ITEM_SCENE.instantiate()
				item.position = Vector2(x_pos, target_lane_y)
				get_parent().add_child(item)  # Add to the map generator node
			else:
				var obstacle = OBSTACLE_SCENE.instantiate()
				obstacle.position = Vector2(x_pos, target_lane_y)
				get_parent().add_child(obstacle)  # Add to the map generator node
		else:
			# Low speed mode: 90% chance for speed items, no obstacles
			if randf() < 0.9:
				var item = SPEED_ITEM_SCENE.instantiate()
				item.position = Vector2(x_pos, target_lane_y)
				get_parent().add_child(item)  # Add to the map generator node

after a while i have no idea what to do anymore. also the items should have generated on the next road only but they also get generated on the previouse road.
`

The target_lane_y doesn’t make sense to me. You set it to either lane1_y or lane2_y, but neither of them got any values assigned?

And if you’re using a rectangle shape as spawn area, I don’t quite get why you modify the x coordinate like that:

		var x_pos = randf_range(area_start_x + 50, area_end_x - 50)

In that code lane1_y and lane2_y are both null, so target_lane_y is also null regardless of the value of player.lane.

oh sorry i didnt send this part:
var lane1_y
var lane2_y

if road_type == "easy":
	# Easy road has only 2 ColorRects
	lane1_y = $ColorRect.position.y
	lane2_y = $ColorRect2.position.y
else:
	# Medium road has 5 ColorRects
	lane1_y = $ColorRect2.position.y
	lane2_y = $ColorRect4.position.y

because there are 2 difficulties, based on time player is running, I made this. colorRects are the ground that the player is running on it

yeah i edit the code, forgot to add something

It’s really hard to tell, because scene tree structure and multiple node’s properties could cause this, but I’d guess the ColorRect.position.y you use for the spawned items might be just 0?

1 Like

fixed it with your help. The issue was that I was trying to implement two different approaches. even though ColorRect was my first aprouch and I use the area2d afterwards… so yeah i just deleted the 2 lane aprouch. thanks for pointing it out mate

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.