Getting items from an array orderly

Godot Version

4.6.1

Question

I am making a 3d game which has a basic item system using node instances. The player has an inventory from which they can place down items from. I want one of these items to show up on an in-game map, so I created a global array where said item writes it’s position to it and likewise removes it when picked backed up. All of that works perfectly fine, but actually reading that information out of the array is what I have trouble with.

In order to make sure that a map icon only appears per each item instance, I have coded in that the script to “spawn” the map icon only runs as many times as there are items in the global array. However, I can’t figure out how to make the code read each item in the array after each other. IE: The first “motionsensoriconcreate()” reads the first item in the array, then the second function reads the second item in the array, etc…

The code currently looks like this:

func _on_viewportmanager_refreshstatusicons():
	for Vector3 in Global.motionsensorpositions:
		motionsensoriconcreate()

func motionsensoriconcreate():
	print("item in array read")

Iterate over indices instead over elements:

for i in Global.motionsensorpositions.size():

You’d also want to use proper snake_case for your names to make them easier to read, as recommended by the the official GDScript style guide. So motion_sensor_icon_create instead of motionsensoriconcreate

I’m not sure how the code you provided is helpful for my use case. I am looking to see how to read one part of the array, then another, then another. That’s what my problem is.

Sure I can spawn in three icons, but how do I make them go to the correct positions? How do I code in something like “read the first part of the array and apply that to the first icon, then read the second part and apply that to the second icon, then the third…”.

I’m not sure how the code you provided illustrates your use case. It’s not really clear what you’re trying to do.

You can iterate and pass the index to the function you’re calling.

I don’t know how iterating would help, at least from my limited understanding of the docs.

What the problem I have is figuring out how to make the first time the script runs, it gets information from the first part of the array, then if it runs again, it gets the second part, etc…
So there needs to be a way to check how many times the script will loop for, I assume at least.

Let me give an example of what I want the code to do:

  • Let’s say there are 3 motion sensors in the gameworld
  • The “Global.motionsensorpositions” array has 3 positions, each of the aformentioned motion sensors
  • The “for i in Global.motionsensorpositions.size():” is activated and runs once
  • The “motionsensor_icon_create()” is ran, and is given the FIRST item in the “Global.motionsensorpositions” array
  • Then the “for i in Global.motionsensorpositions.size():” runs for the second time
  • The “motionsensor_icon_create()” is ran, and is given the SECOND item in the “Global.motionsensorpositions” array
  • Then the “for i in Global.motionsensorpositions.size():” runs for the third and final time
  • The “motionsensor_icon_create()” is ran, and is given the THIRD (or last in this case) item in the “Global.motionsensorpositions” array

Because my vision is that an icon appears PER motion sensor, almost like they are tied to each other. If there is a motion sensor in the game world, an icon should appear on the map where the motion sensor is. Currently however I only have the “N icons should appear for N amount of motion sensors” part down.

func _on_viewport_manager_refresh_status_icons():
	for sensor_position: Vector3 in Global.motion_sensor_positions:
		motion_sensor_icon_create(sensor_position)

func motion_sensor_icon_create(at_position: Vector3):
	print("Creating sensor icon at position: ", at_position)

The same can also be done by passing an index and let the creation function get the position from the array:

func _on_viewport_manager_refresh_status_icons():
	for position_index: int in Global.motion_sensor_positions.size():
		motion_sensor_icon_create(position_index)

func motion_sensor_icon_create(position_index: int):
	var sensor_position: Vector3 = Global.motion_sensor_positions[position_index]
	print("Creating sensor icon at position: ", sensor_position)