View Full Version : [SOLVED] A script linked to some NPCs

02-19-2016, 02:10 PM
I have a question related to making scripts. If you want to link a script with gossips to a NPC you usually do that:

local UnitEntry = 60006


RegisterCreatureGossipEvent(UnitEntry, 1, OnGossipHello)
RegisterCreatureGossipEvent(UnitEntry, 2, OnGossipSelect)

My question is: Can I do that?

local UnitEntry1 = 60006
local UnitEntry2 = 60007
local UnitEntry3 = 60008
local UnitEntry4 = 60009


RegisterCreatureGossipEvent(UnitEntry1, 1, OnGossipHello)
RegisterCreatureGossipEvent(UnitEntry1, 2, OnGossipSelect)

RegisterCreatureGossipEvent(UnitEntry2, 1, OnGossipHello)
RegisterCreatureGossipEvent(UnitEntry2, 2, OnGossipSelect)

RegisterCreatureGossipEvent(UnitEntry3, 1, OnGossipHello)
RegisterCreatureGossipEvent(UnitEntry3, 2, OnGossipSelect)

RegisterCreatureGossipEvent(UnitEntry4, 1, OnGossipHello)
RegisterCreatureGossipEvent(UnitEntry4, 2, OnGossipSelect)

My goal is to create 4 differents NPCs with the same Script linked.

02-19-2016, 03:06 PM
Yes that will work. Seems a bit like a silly question though, you could've just tried it? :confused2:

02-19-2016, 05:37 PM
I tested it and worked perfectly, the question was more about stability of the core than usability...

02-19-2016, 06:26 PM
Well I apologize then. I can't think of any reason why it would be less stable, on the contrary.
Doing it this way is good coding practice, so you're good to go.

02-19-2016, 06:57 PM
np m8 and thanks for the answer :)

02-19-2016, 07:56 PM
Imo you could do a for loop and increment + 1 since the ids are so close together.

For example:

local UnitEntry = 60006
for i = 1,4 do
RegisterCreatureGossipEvent(UnitEntry, 1, OnGossipHello)
RegisterCreatureGossipEvent(UnitEntry, 2, OnGossipSelect)
UnitEntry = UnitEntry + 1

Not exactly sure how the result will be as I've never registered multiple events like that. Though, it is a better idea than spamming the same code over and over again (which overall will look ugly).

02-19-2016, 10:28 PM
Maybe Rochet2 can explain me about the stability of assing multiple events in a script.

02-19-2016, 10:38 PM
Maybe Rochet2 can explain me about the stability of assing multiple events in a script.

There's nothing wrong with doing that. If it was such a huge "stability" issue we'd recommend not doing it or if possible fix the issue..

02-19-2016, 11:01 PM
Yeah, there is no stability issue with that. Infact using the same function for multiple NPCs is more efficient since you only need one function instead of X amount of functions. So you save RAM.
And like Grandelf said and Tommy implied, its a good practice not to repeat stuff.

In general there should be no "stability issues" other than from something we have overlooked or your code does wrong in the logic.
There are ways your code could set something in an incoherent state or you could possibly not notice some small detail.
For example you could use async and sync database queries and possibly read old data, save new data and then save the old data you read over the new data.
Or you could set a timed event go forever and ever cancel it. They do keep firing even if the NPC is dead for example.
Or you could use the addvendoritem - or whats the method called - to add items to the vendor to make a "multivendor" but at the same time you destroy the chance for multiple ppl to use the vendor.

Other than that there might be RAM consumed if your code is wrong or needs to hold a lot of DB data. Eluna in itself does need some RAM as well.
Also efficiency can be limiting as checking hooks and such on events is done etc.

One thing new programmers etc. do often miss is that all variables in the scripts are SHARED.
This means that if you do

local entry
function OnSpawn(creature)
entry = creature:GetEntry()

RegisterCreatureEvent(ONSPAWN, 123, OnSpawn)
RegisterCreatureEvent(ONSPAWN, 564, OnSpawn)
Then in this case the entry is shared and the value of that variable changes over time depending on which entry NPC was last spawned ingame.
So if you register a timed event that makes the NPCs say the value of the entry variable, they will keep saying different things depending on ingame spawns and they will NOT only say their own entry (even though when testing the script it might seem so)

Also with same logic, passing stuff through the global or locally global scope between different events or functions is not a good idea. Same thing was basically happening with the multivendor example I said above.

If you know of some stability or other issues, do report them :) Preferrably on our issue tracker: