• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

"RegisterEvent" on bad self (chat message on spawn)

Status
Not open for further replies.

Mahado

Epic Member
Hello, I have a problem with my npc. I can't figure out what is going wrong with this, tried to search solutions and tried to do various things, but nothing seems to help. I want it to repeatedly check for chance to say something if player is within its radius.

http://pastebin.com/Nq5DDKMy

- Mahado

ps. Do not mind about the chat strings that probably are messed up too. But, of course I'll accept any tips how to set it so that it would add Message + player's name.
 
Last edited:

uDev

Illustrious Member
There you go :)
Code:
local NPC_ID = 1 -- Edit ID with id of your npc
 
function Checking_Close(event, creature, player)
        if creature:IsWithinDistInMap(player, 15)) then
			local chance = math.random(1,3)
				if chance == 1 then
					creature:SendUnitSay("Greetings", 0) 
                elseif chance == 2 then
                    creature:SendUnitSay("Greetingsxx", 0)
                elseif chance == 3 then
                    creature:SendUnitSay("Greetingsx", 0)
				end	
		end
end
   
RegisterCreatureEvent(NPC_ID, 27, Checking_Close)
 

Mahado

Epic Member
Thank you! That works perfectly. :)

I still wonder what caused the "on bad self" thing on the first version of the script. It didn't not want to read the creature:RegisterEvent with CREATURE_EVENT_ON_RESET = 23 at all.
 

uDev

Illustrious Member
You registered wrong event :) and also this is wrong creature:GetNearestPlayer(10) :D

And no problem, glad i managed to fix your problem :)
 

Mahado

Epic Member
Event choice might've been bad. Yeah, my main problem was that it didn't go any further from the very first OnReset function, not with RegisterEvent. That is what I want to know. Ex:

Code:
    local guardi = 45009
     
    function GuardTalking_Event(event, creature)
            creature:RegisterEvent("Checking_Close", 5000, 1)
    end
     
    function Checking_Close(event, delay, pCall, creature)
              creature:SendUnitSay("Greetings", 0)
    end
     
    RegisterCreatureEvent(guardi, 23, GuardTalking_Event)

Shouldn't this work normally? But it didn't, it errors.
 

Tommy

Founder
Event choice might've been bad. Yeah, my main problem was that it didn't go any further from the very first OnReset function, not with RegisterEvent. That is what I want to know. Ex:

Code:
    local guardi = 45009
     
    function GuardTalking_Event(event, creature)
            creature:RegisterEvent("Checking_Close", 5000, 1)
    end
     
    function Checking_Close(event, delay, pCall, creature)
              creature:SendUnitSay("Greetings", 0)
    end
     
    RegisterCreatureEvent(guardi, 23, GuardTalking_Event)

Shouldn't this work normally? But it didn't, it errors.

It will work, yes. However, MoveInLOS creature hook would be better than using a RegisterEvent. Since RegisterEvent calls when you set it up to call, it is quite pointless in this case. However, move in LOS ticks every second, so you can easily set it to talk when you want it to.

Code:
local guardi = 45009
local ticks = 0
     
function MoveInLOS(event, creature, unit)
    ticks = ticks + 1
    if (unit:GetUnitType() == "Player" and creature:IsWithinDistInMap(unit, 5) and ticks >= 5) then
        ticks = 0
        creature:SendUnitSay("Greetings..", 0)
    end
end
     
RegisterCreatureEvent(guardi, 27, GuardTalking_Event)

Now, "IsWithinDistInMap" will get the unit in range. The range is 5. More on this function here: http://wiki.emudevs.com/doku.php?id=eluna_unit_class_iswithindistinmap
 

Foereaper

Founder
RegisterEvent and CreateLuaEvent isn't working 100% as it should, it can be a bit iffy. This has been rewritten on the mangos version, and will be implemented in Trinity as well once Mangos development is done :)
 

Mahado

Epic Member
It will work, yes. However, MoveInLOS creature hook would be better than using a RegisterEvent. Since RegisterEvent calls when you set it up to call, it is quite pointless in this case. However, move in LOS ticks every second, so you can easily set it to talk when you want it to.

Code:
local guardi = 45009
local ticks = 0
     
function MoveInLOS(event, creature, unit)
    ticks = ticks + 1
    if (unit:GetUnitType() == "Player" and creature:IsWithinDistInMap(unit, 5) and ticks >= 5) then
        ticks = 0
        creature:SendUnitSay("Greetings..", 0)
    end
end
     
RegisterCreatureEvent(guardi, 27, GuardTalking_Event)

Now, "IsWithinDistInMap" will get the unit in range. The range is 5. More on this function here: http://wiki.emudevs.com/doku.php?id=eluna_unit_class_iswithindistinmap

Oh, there is something like that as well, I mean the ticking thing. Good to know, thanks. :)

RegisterEvent and CreateLuaEvent isn't working 100% as it should, it can be a bit iffy. This has been rewritten on the mangos version, and will be implemented in Trinity as well once Mangos development is done :)

No wonder then, I guess. :D Guess I'll focus on working other things as I have ton of stuff to do meanwhile.
 

Rochet2

Moderator / Eluna Dev
Everyone should note that NO FUNCTION IS PASSED AS STRING NAME.
Basically you provided the name of the function, not a function. That's what caused your script to error.
 

Mahado

Epic Member
Everyone should note that NO FUNCTION IS PASSED AS STRING NAME.
Basically you provided the name of the function, not a function. That's what caused your script to error.

Oh my god, I noticed that difference when registering different events (when naming them), but not there, lolz. Gotta learn out of Arcemu's ways.
 

Rochet2

Moderator / Eluna Dev
Btw. Arcemu accepts both string and function. Using string was originally the only way and was left for backwards compatibility... As far as I know.

The good thing about accepting a function is that you can use local and tabled.. Etc functions.
 

Mahado

Epic Member
Btw. Arcemu accepts both string and function. Using string was originally the only way and was left for backwards compatibility... As far as I know.

The good thing about accepting a function is that you can use local and tabled.. Etc functions.

Alright, this has been very teaching. Thanks. ^^

UPDATE: The problem "RegisterEvent on bad self" persists to exist even now, I can't do a shit with registerevent. But I guess that will be changed in the future's patch (trinity).
 
Last edited:
Status
Not open for further replies.
Top