Godot 4 网格基础物品放置

huangapple go评论129阅读模式
英文:

Godot 4 grid base item palcement

问题

我想用Godot 4制作一个开放式多人RPG游戏。问题是,我看过一个视频教程(这里),遇到了这个错误:

> Invalid get index '2' (on base: 'Array').

以下是我的代码:

  1. extends Node2D
  2. @onready var camp_fire = preload("res://src/Scenes/camp_fire.tscn")
  3. var tile_size = 16
  4. enum { OBSTACLE, COLLECTABLE, RESOURCE }
  5. var grid_size = Vector2(160, 160)
  6. var grid = []
  7. func _ready():
  8. for x in range(grid_size.x):
  9. grid.append([])
  10. for y in range(grid_size.y):
  11. grid[x].append(null)
  12. var positions = []
  13. for i in range(50):
  14. var xcoor = (randi() % int(grid_size.x))
  15. var ycoor = (randi() % int(grid_size.y))
  16. var grid_pos = Vector2(xcoor, ycoor)
  17. if not grid_pos in positions:
  18. positions.append(grid_pos)
  19. func _input(event):
  20. if event.is_action_pressed("LeftClick"):
  21. var mouse_pos = get_global_mouse_position()
  22. var multiX = int(round(mouse_pos.x) / tile_size)
  23. var numX = multiX * tile_size
  24. var multiY = int(round(mouse_pos.y) / tile_size)
  25. var numY = multiY * tile_size
  26. var new_pos = Vector2(multiX, multiY)
  27. var new_camp_fire = camp_fire.instance()
  28. new_camp_fire.set_position(tile_size * new_pos)
  29. grid[multiX][multiY] = OBSTACLE
  30. get_tree().root.get_node("World").get_node("PlayerBuildings").add_child(new_camp_fire)
英文:

i want to make a open multiplayer rpg game with Godot 4. The problem is that, i have seen a video tutorial (here) and i came accross with this error:

> Invalid get index '2' (on base: 'Array').

here is my code

  1. extends Node2D
  2. @onready var camp_fire = preload("res://src/Scenes/camp_fire.tscn")
  3. var tile_size =16
  4. enum {OBSTACTLE, COLLECTABLE,RESOURCE}
  5. var grid_size = Vector2(160,160)
  6. var grid = []
  7. func _ready():
  8. for x in range(grid_size.x):
  9. grid.append([])
  10. for y in range(grid_size.y):
  11. grid[x].append(null)
  12. var positions = []
  13. for i in range (50):
  14. var xcoor = (randi() % int(grid_size.x))
  15. var ycoor = (randi() % int(grid_size.y))
  16. var grid_pos = Vector2(xcoor,ycoor)
  17. if not grid_pos in positions:
  18. positions.append(grid_pos)
  19. func _input(event):
  20. if event.is_action_pressed("LeftClick"):
  21. var mouse_pos = get_global_mouse_position()
  22. var multiX = int(round(mouse_pos.x)/tile_size)
  23. var numX = multiX*tile_size
  24. var multiY = int(round(mouse_pos.y)/tile_size)
  25. var numY = multiY*tile_size
  26. var new_pos = Vector2(multiX, multiY)
  27. var new_camp_fire = camp_fire.instantiate()
  28. new_camp_fire.set_position(tile_size*new_pos)
  29. grid[multiX][multiY] = OBSTACTLE
  30. get_tree().root.get_node("World").get_node("PlayerBuildings").add_child(new_camp_fire)

答案1

得分: 1

解决方案很简单。我将此脚本附加到我希望玩家能够放置物品的位置。我将此脚本放在了生成地图中物品的Node2D上!!!

英文:

The solution was simply. I had attach this script at the item which i want player to be able to place. I put this script at Node2D where i spawn the item in map!!!

huangapple
  • 本文由 发表于 2023年6月13日 03:17:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459701.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定