I created an NPC with patrol and pursuit mechanics. As well as the player having the ability to shoot and kill the created NPC. The problems started when it was necessary to copy an object, for example, using the collectionfactory tool. There is a health scale object in the collection of the created NPC. It turns out that the main script of the NPC communicates with the bullet released by the player and with its own health scale. In the case of a single object, everything works fine. But as soon as the second instance appears, the message between the scripts breaks. I understand that the problem lies precisely in these messages. But I don’t understand how to rewrite scripts so that each newly created script communicates with its own pair in messages. Please help me to rewrite this part for correct copying of the NPC.
When you spawn a collection of game objects using the collection factory tool you get a look-up table that can be used to get the urls of the spawned game object instances based on the known ids from the collection prototype.
Instead of hardcoding “main:/collection0/soldier” you need to look up the correct url path to the spawned object. Have a look at the manual for the collection factory:
Something like this:
local soldier_ids = collectionfactory.create("#soldier_factory")
local url = msg.url(nil, soldier_ids[hash("/soldier")], "healtbar")
msg.post(url, "health_1\\7")
1 Like