+1 vote

So, I made a position randomizing code for my project and this error came out:

Invalid set index 'global_position' (on base: 'Area2D') with value of type 'Nil'.

extends Node2D

export(String) var currentGun
export(String) var currentMelee
export(String) var currentProjectile
export(String) var currentPower

const AK47 : Texture = preload("res://Art/Weapons/Guns/AK47.pxo")
const Pistol : Texture = preload("res://Art/Weapons/Guns/Pistol.pxo")
const Flamethrower : Texture = preload("res://Art/Weapons/Guns/Flamethrower.pxo")
const LaserRifle : Texture = preload("res://Art/Weapons/Guns/Laser Rifle.pxo")
const LaserSniper : Texture = preload("res://Art/Weapons/Guns/Laser Sniper.pxo")
const Minigun : Texture = preload("res://Art/Weapons/Guns/Minigun.pxo")
const Shotgun : Texture = preload("res://Art/Weapons/Guns/Shotgun.pxo")
const Sniper : Texture = preload("res://Art/Weapons/Guns/Sniper.pxo")
const Taser : Texture = preload("res://Art/Weapons/Guns/Taser.pxo")
const RocketLauncher : Texture = preload("res://Art/Weapons/Guns/Rocket Launcher.pxo")

const BattleAxe : Texture = preload("res://Art/Weapons/Melees/BattleAxe.pxo")
const Hammer : Texture = preload("res://Art/Weapons/Melees/Hammer.pxo")
const Katana : Texture = preload("res://Art/Weapons/Melees/Katana.pxo")
const Knife : Texture = preload("res://Art/Weapons/Melees/Knife.pxo")
const Spear : Texture = preload("res://Art/Weapons/Melees/Spear.pxo")

const Landmine : Texture = preload("res://Art/Weapons/Explosives/Landmine.pxo")
const Charges : Texture = preload("res://Art/Weapons/Explosives/Charges.pxo")
const Grenade : Texture = preload("res://Art/Weapons/Explosives/Grenade.pxo")
const Flashbang : Texture = preload("res://Art/Weapons/Explosives/Flashbang.pxo")
const Dynamite : Texture = preload("res://Art/Weapons/Explosives/Dynamite.pxo")

const Speed : Texture = preload("res://Art/Power-Ups/SpeedBoost.pxo")
const JumpBoost : Texture = preload("res://Art/Power-Ups/JumpBoost.pxo")
const Health25 : Texture = preload("res://Art/Power-Ups/HeartBoost25.pxo")
const Health50 : Texture = preload("res://Art/Power-Ups/HeartBoost50.pxo")
const Health100 : Texture = preload("res://Art/Power-Ups/HeartBoost100.pxo")
const Strength : Texture = preload("res://Art/Power-Ups/Strength.pxo")
const Swap : Texture = preload("res://Art/Power-Ups/PlayerSwap.pxo")
const Shield : Texture = preload("res://Art/Power-Ups/Shield.pxo")
const PoisonBullet : Texture = preload("res://Art/Power-Ups/ToxicBullets.pxo")
const StunBullet : Texture = preload("res://Art/Power-Ups/StunBullet.pxo")
const Wall : Texture = preload("res://Art/Power-Ups/Wall.pxo")
const Shock : Texture = preload("res://Art/Power-Ups/Shockwave.pxo")
const TP : Texture = preload("res://Art/Power-Ups/Teleportation.pxo")

onready var Spawn1 : Vector2 = get_node("SpawnerPoints/1").global_position
onready var Spawn2 : Vector2 = get_node("SpawnerPoints/2").global_position
onready var Spawn3 : Vector2 = get_node("SpawnerPoints/3").global_position
onready var Spawn4 : Vector2 = get_node("SpawnerPoints/4").global_position
onready var Spawn5 : Vector2 = get_node("SpawnerPoints/5").global_position
onready var Spawn6 : Vector2 = get_node("SpawnerPoints/6").global_position
onready var Spawn7 : Vector2 = get_node("SpawnerPoints/7").global_position

onready var GunSpawner = $Spawners/Gun
onready var MeleeSpawner = $Spawners/Melee
onready var ProjectileSpawner = $Spawners/Projectile
onready var PowerSpawner = $Spawners/Power

var g_texture
var m_texture
var p_texture
var power_texture

var g_position = Vector2.ZERO 
var m_position = Vector2.ZERO
var p_position = Vector2.ZERO
var power_position = Vector2.ZERO

var gun_textures = [AK47, Pistol, Flamethrower, LaserRifle, LaserSniper, Minigun, Shotgun, Sniper, Taser, RocketLauncher]
var guns = ["AK47", "Pistol", "Flamethrower", "LaserRifle", "LaserSniper", 'Minigun', "Shotgun", "Sniper", "Taser", "RocketLauncher"]

var melee_textures = [BattleAxe, Hammer, Katana, Knife, Spear]
var melees = ["BattleAxe", "Hammer", "Katana", "Knife", "Spear"]

var projectile_textures = [Landmine, Charges, Grenade, Flashbang, Dynamite]
var projectiles = ["Landmine", "Charges", "Grenade", "Flashbang", "Dynamite"]

var power_textures = [Speed, JumpBoost, Health25, Health50, Health100, Strength, Shield, PoisonBullet, StunBullet, Wall, Shock, TP]
var powers = ["Speed", "JumpBoost", "Health25", "Health50", "Health100", "Strength", "Shield", "PoisonBullet", "StunBullet", "Wall", "Shock", "TP"]

var SpawnPoints = [Spawn1, Spawn2, Spawn3, Spawn4, Spawn5, Spawn6, Spawn7]

signal GunGet1
signal GunGet2
signal MeleeGet1
signal MeleeGet2
signal ProjectileGet1
signal ProjectileGet2
signal PowerGet1
signal PowerGet2

func _ready():
    randomize()
    g_texture = null
    m_texture = null
    p_texture = null


func _process(delta):
    $Spawners/Gun/Sprite.texture = g_texture
    $Spawners/Melee/Sprite.texture = m_texture
    $Spawners/Projectile/Sprite.texture = p_texture
    $Spawners/Power/Sprite.texture = power_texture

    $Spawners/Gun.global_position = g_position
    $Spawners/Melee.global_position = m_position
    $Spawners/Projectile.global_position = p_position
    $Spawners/Power.global_position = power_position


func _gunSpawn():
    var RanNum = rand_range(0, 10)
    var RanNum1 = rand_range(0, 7)
    g_texture = gun_textures[RanNum]
    currentGun = guns[RanNum]
    g_position = SpawnPoints[RanNum1]
    GunSpawner.show()
    GunSpawner.set_block_signals(false)


func _meleeSpawn():
    var RanNum = rand_range(0, 5)
    var RanNum1 = rand_range(0, 7)
    m_texture = melee_textures[RanNum]
    currentGun = melees[RanNum]
    m_position = SpawnPoints[RanNum1]
    MeleeSpawner.show()
    MeleeSpawner.set_block_signals(false)


func _projectileSpawn():
    var RanNum = rand_range(0, 5)
    var RanNum1 = rand_range(0, 7)
    p_texture = projectile_textures[RanNum]
    currentGun = projectiles[RanNum]
    p_position = SpawnPoints[RanNum1]
    ProjectileSpawner.show()
    ProjectileSpawner.set_block_signals(false)
    print("LOL IT WORKS!")


func _powerSpawn():
    var RanNum = rand_range(0, 12)
    var RanNum1 = rand_range(0, 7)
    power_texture = power_textures[RanNum]
    currentGun = powers[RanNum]
    power_position = SpawnPoints[RanNum1]
    PowerSpawner.show()
    PowerSpawner.set_block_signals(false)


func _on_GunTimer_timeout():
    _gunSpawn()


func _on_MeleeTimer_timeout():
    _meleeSpawn()


func _on_ProjectileTimer_timeout():
    _projectileSpawn()
    print("IT DOES WORK...")

func _on_PowerTimer_timeout():
    _powerSpawn() 


func _on_Gun_body_entered(body):
    if body.is_in_group("Player1"):
        emit_signal("GunGet1")
        GunSpawner.hide()
        GunSpawner.set_block_signals(true)
    elif body.is_in_group("Player2"):
        emit_signal("GunGet2")
        GunSpawner.hide()
        GunSpawner.set_block_signals(true)
    else:
        pass


func _on_Melee_body_entered(body):
    if body.is_in_group("Player1"):
        emit_signal("MeleeGet1")
        MeleeSpawner.hide()
        MeleeSpawner.set_block_signals(true)
    elif body.is_in_group("Player2"):
        emit_signal("MeleeGet1")
        MeleeSpawner.hide()
        MeleeSpawner.set_block_signals(true)
    else:
        pass


func _on_Projectile_body_entered(body):
    if body.is_in_group("Player1"):
        emit_signal("ProjectileGet1")
        ProjectileSpawner.hide()
        ProjectileSpawner.set_block_signals(true)
    elif body.is_in_group("Player2"):
        emit_signal("ProjectileGet2")
        ProjectileSpawner.hide()
        ProjectileSpawner.set_block_signals(true)
    else:
        pass


func _on_Power_body_entered(body):
    if body.is_in_group("Player1"):
        emit_signal("PowerGet1")
        PowerSpawner.hide()
        PowerSpawner.set_block_signals(true)
    elif body.is_in_group("Player2"):
        emit_signal("PowerGet2")
        PowerSpawner.hide()
        PowerSpawner.set_block_signals(true)
    else:
        pass

This is the code

Need help...

Godot version 3.4
in Engine by (17 points)

That's a lot of code. Can you indicate what line has the error?

The error message is saying that whatever you're assigning to position, it has no value.

2 Answers

+1 vote

I dont believe Area2D nodes have a positional vector method. They have a gravitational vector but not a positional one so you are trying to assign an position equal to a node which has no position method.

https://docs.godotengine.org/en/stable/classes/class_area2d.html

Try putting a postion2d node at the positions and setting the position to that instead.

by (1,341 points)

Ugh, I can't believe I missed that. Well spotted. Tip of the cap.

0 votes

As kidscancode said, way too much code (try to only include the relevant snippet) but I'm a glutton for punishment.

There's not much to go on but I suspect it's this line:

onready var Spawn1 : Vector2 = get_node("SpawnerPoints/1").global_position

My guess is path is wrong so godot isn't finding a node there (the path is relative to the node you're on). You've told Godot that Spawn1 only takes Vector2s so crashy-crash when it gets nil instead . Just check the path. I don't know the error line or your node tree so can't be sure but that's what I'd check first.

Unrelated, but generally programmers prefer not to repeat code. You can use loops, arrays, things like that and this code could be a lot more concise which makes it that much easier to debug and code. Just something to bear in mind, the most important thing is that it does what you want so don't worry overly.

by (1,695 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.