0 votes

I've tried to work it out myself but I'm just stuck on what I'm misunderstood.

The code:

func _ready():  
randomize()
for i in range(3):
    Agent["Name"] = Names[randi() % Names.size()]
    Agent["Persuade"] = randi() % 10 + 1
    Agent["Combat"] = randi() % 10 + 1
    Agent["Stealth"] = randi() % 10 + 1
    Agent["Hacking"] = randi() % 10 + 1
    AgentList.append(Agent)
    i += 1

The result:

[{Combat:5, Hacking:3, Image:1, Name:Test, Persuade:6, Stealth:9}, {Combat:5, Hacking:3, Image:1, Name:Test, Persuade:6, Stealth:9}, {Combat:5, Hacking:3, Image:1, Name:Test, Persuade:6, Stealth:9}]

Each time I run the program it will generate different results, but within the actual generation the numbers will always be the same. I want them to be different for each agent.

in Engine by (12 points)

1 Answer

+2 votes

Your issue has nothing to do with the random functions of GDScript.

You are appending three time the same "Agent" object. The list is not creating a copy, it only store a reference to the object. So in reality in each pass of the loop you are modifying the same object. You have to instantiate a new object at the beginning of the for.

by (1,045 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.