How to change variable of instantiated object

Godot Version

v4.1.1.stable.official [bd6af8e0e]

Question

how do i change the variable of instantiated object, i wanna change the speed of my mob with button

This is my main node


extends Node2D

var mob=preload("res://mob.tscn")

func _on_timer_timeout():
	var spawnmob=mob.instantiate()
	$Path2D/Spawn.progress_ratio = randf()
	spawnmob.global_position=$Path2D/Spawn.global_position
	add_child(spawnmob)


func _on_button_pressed():
	$MainCharacter.speed=1500

func _on_button_2_pressed():
	mob.speed=500

This is my mob node

extends CharacterBody2D

var speed =100

func _physics_process(delta):
	var direction=Vector2.DOWN
	velocity=direction*speed
	move_and_slide()

after adding it as child, just do spawnmob.velocity = <your value>, but I’m not sure if that’s what you really want

thx for answering but sorry that’s not what i meant, in this case i wanna make my character has a skill to slow all the enemies for a while before returning their speed back(with pressing button), so i need to change the variable value more than once

I think you are looking for signal in an Area2d

when the area triggers, you can check collided object for specific properties.

# this is an enemy

func _on_Area2D_area_entered(area): 
	if(area.is_in_group("DecreaseSpeedMagic")):
             speed = 2
            $SpeedResistanceTimer.start()

After check, i would set a Timer for how long you want to decrease the speed.

func _on_Timer_timeout():
       speed = 4


/// if you want to control that from player side you could do is like that:

func _on_Area2D_area_entered(area): 
	if(area.is_in_group("Enemy")):
            speed -= 1
            area.resistanceSpeedTimer.start() # to reset is to normal speed after a while.

Some code like that, could work for an ice spell or something like that.

edit: dont know how to create the code snippets, so Im sorry for my code xD

Place your code between ```:

```gdscript
< your code goes here >
```

talk with @discobot for more information

1 Like

Hi! To find out what I can do, say @discobot display help.

print("thank you :D")

Oh cool. Now i know :see_no_evil:

this works thanks a lot

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.