Godot 4 网格基础物品放置

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

Godot 4 grid base item palcement

问题

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

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

以下是我的代码:

extends Node2D

@onready var camp_fire = preload("res://src/Scenes/camp_fire.tscn")
var tile_size = 16

enum { OBSTACLE, COLLECTABLE, RESOURCE }
var grid_size = Vector2(160, 160)
var grid = []

func _ready():
    for x in range(grid_size.x):
        grid.append([])
        for y in range(grid_size.y):
            grid[x].append(null)
    var positions = []
    for i in range(50):
        var xcoor = (randi() % int(grid_size.x))
        var ycoor = (randi() % int(grid_size.y))
        var grid_pos = Vector2(xcoor, ycoor)
        if not grid_pos in positions:
            positions.append(grid_pos)

func _input(event):
    if event.is_action_pressed("LeftClick"):
        var mouse_pos = get_global_mouse_position()
        var multiX = int(round(mouse_pos.x) / tile_size)
        var numX = multiX * tile_size
        var multiY = int(round(mouse_pos.y) / tile_size)
        var numY = multiY * tile_size
        var new_pos = Vector2(multiX, multiY)
        var new_camp_fire = camp_fire.instance()
        new_camp_fire.set_position(tile_size * new_pos)
        grid[multiX][multiY] = OBSTACLE
        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

extends Node2D

@onready var camp_fire = preload("res://src/Scenes/camp_fire.tscn")
var tile_size =16

enum {OBSTACTLE, COLLECTABLE,RESOURCE}
var grid_size = Vector2(160,160)
var grid = []


func _ready():
	for x in range(grid_size.x):
		grid.append([])
		for y in range(grid_size.y):
			grid[x].append(null)
	var positions = []
	for i in range (50):
		var xcoor = (randi() % int(grid_size.x))
		var ycoor = (randi() % int(grid_size.y))
		var grid_pos = Vector2(xcoor,ycoor)
		if not grid_pos in positions:
			positions.append(grid_pos)

func _input(event):
	if event.is_action_pressed("LeftClick"):
		var mouse_pos = get_global_mouse_position()
		var multiX = int(round(mouse_pos.x)/tile_size)
		var numX = multiX*tile_size
		var multiY = int(round(mouse_pos.y)/tile_size)
			var numY = multiY*tile_size
		var new_pos = Vector2(multiX, multiY)
		var new_camp_fire = camp_fire.instantiate()
		new_camp_fire.set_position(tile_size*new_pos)
		grid[multiX][multiY] = OBSTACTLE
		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:

确定