Godot Version
4.2.2
Question
So for a while I’ve been trying to figure out how generate some items i’ve created randomly across my tile maps. Like random loot lying around. Basically, when the game starts I plan for the items to be spread across the map for you to pick them up whilst being restricted by the boarders of the walls.
I can’t get them to randomly appear let alone move around the tilemap. I’m able to call them in with instantiate but they are just stagnant and refuse to move position from when I called them from the scene.
My item Code:
extends Area2D
class_name Items
@export var item_name: String
@export var itemRarity: String
@export var itemType: String
var player_in_range = false
var player_ref = null
var pickedUp = false
func _ready():
self.body_entered.connect(_on_body_entered)
self.body_exited.connect(_on_body_exited)
func _on_body_entered(body):
if body.name == "robber 1":
player_in_range = true
player_ref = body
print("Press E or A to pick up %s" % item_name)
func _on_body_exited(body):
if body.name == "robber 1":
player_in_range = false
player_ref = null
func _process(delta):
if player_in_range and Input.is_action_just_pressed("Interact"):
pickedUp = true
if player_ref:
print("Picked up: %s" % item_name)
queue_free()
I’m not too sure what to code to make the items move around a certain location especially if they’re from foreign scene. All help would be greatly appreciated!!
Can you post the code where you instantiate the items? I don’t see it here. Also, when you refer to movement, what are you referring to? I don’t see any code to move the items here either.
Thats my world code from another scene
extends Node2D
var items = preload("res://scenes/inventory.tscn")
func _ready():
var instance = items.instantiate()
add_child(instance)
Does the inventory scene have the script in the first post? It looks like you just make one instance.
more so just a place holder for now. it doesnt really do anything but just be a hub to call the multiple scenes (items) below
Does the inventory have the script that instantiates the items randomly? Or have you not written it yet?
No it doesn’t I’m not sure what to write
Okay, well I think you should use a for loop to go through every child of the Items node and set a random position for them with randf_range()
. You also need to get the boundaries of the tilemap with TileMapLayer.get_used_rect()
.
sorry, how would i loop it?
A for loop.
func _ready():
for item in $Items.get_children():
pass # do stuff here
If you don’t know what one is, you should go through a basic scripting tutorial.
so now i’ve created a for loop, how would I apply randf_range() and tilemaplayer
You’ll need to get a reference to the TileMapLayer so you can call the functions it has. Usually references are obtained with get_node()
or $
followed by a NodePath.
Then you need to pass the boundaries of the TileMapLayer into the Inventory script so that you can use as the limits for the randf_range()
function. You’ll need to call it twice, since position has both an x and a y coordinate.
im using godot 4.2.2 i dont think i have TileMapLayer if it’s a node youre talking about
would it be something like this or I need to instantiate it first
The node you have is called Items
, not items
. You don’t need to instantiate anything inside the Inventory script since all nodes are already instantiated. Also, you need to pass in the boundaries of the TileMap since the Inventory can’t access it easily through a NodePath since they are in different scenes. Just typing TileMap
gets you a reference to the class itself, not the actual TileMap you are using.