• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Lua addEvent + creture:say problem

Theralion

New Member
Joined
Feb 12, 2016
Messages
8
Reaction score
0
Hi there. I want to make my function run in addEvent but i get problems.
I have added a global function in global.lua that i want to use for other things
Code:
function monsterSayPos(creature,text,position)
creature:say(text,TALKTYPE_ORANGE_1,false,nil,position)
end
and when people enter a certain SQM i want orange text(monster alike) to pop at a certain position

Code:
function onStepIn(creature, item, position, fromPosition)
    local pos1 = { x = position.x + 3, y = position.y, z = position.z}
    local pos2 = { x = position.x + 5, y = position.y, z = position.z}
   
   
    if creature:getStorageValue(2) == 3 then
        creature:setStorageValue(2,4)
        monsterSayPos(creature,"temp 1", pos1)
        addEvent(monsterSayPos,2000,creature,"temp2",pos2)
    end
end

The first message appears without problems . then i get a warning that argument #3 is unsafe in addEvent and later in my function (monsterSayPos) attempt to index local 'creature' (a number value)
 
https://github.com/otland/forgottenserver/issues/235
https://github.com/otland/forgottenserver/issues/1157

Okay so all i did is changed my function to use cid and create a variable creature with that cid. Apparently you can't pass userdata in this secnario.

Edit 1:
After adding 3 actions with my function i get error at fourth call.

Code:
        monsterSayPos(creature,"I have almost escaped. Where is that exit ?", pos1)
        addEvent(monsterSayPos,3000,cid,"There you are...",pos2)
        addEvent(monsterSayPos,5000,cid,"my precious experiment!",pos2)
        addEvent(monsterSayPos,6500,cid,"NO, NOT AGAIN ",pos1)


monsterSayPos function in compat.lua
Code:
function monsterSayPos(cid,text,position)   -- mowi pomaranczowym napisem w danej pozycji dany text
    local creature = Creature(cid)
    if creature ~= nil then
        creature:say(text,TALKTYPE_ORANGE_1,false,nil,position)
        return true
    else
    print("Creature logged out or disappeared/died")
    end
end

ERROR MESSAGE : https://imgur.com/SZL9G1Z

PS: what is the difference where i place my global function? To global.lua, compat.lua or any other lua file ?
 
Last edited:
@Theralion there is no difference, it is the same thing, global.lua will load lib/lib.lua that will load compat.lua, only for keep files clean, compat are there just to make compatibility with older scripts.
 
Last edited:
Code:
monsterSayPos(creature
here is the problem, you must follow your funcion parameters.
Code:
monsterSayPos(cid
 
No, the problem is not that, he can use the userdata if its not an addevent.

The problem is probably that these tables (pos1 or pos2) are changing somehow to nil? Can't tell for sure without seeing all the code but you can do something like:
Code:
monsterSayPos(creature,"I have almost escaped. Where is that exit ?", Position(pos1.x, pos1.y, pos1.z))
addEvent(monsterSayPos,3000,cid,"There you are...",  Position(pos2.x, pos2.y, pos2.z))
addEvent(monsterSayPos,5000,cid,"my precious experiment!",Position(pos2.x, pos2.y, pos2.z))
addEvent(monsterSayPos,6500,cid,"NO, NOT AGAIN ", Position(pos1.x, pos1.y, pos1.z))
 
No, the problem is not that, he can use the userdata if its not an addevent.

The problem is probably that these tables (pos1 or pos2) are changing somehow to nil? Can't tell for sure without seeing all the code but you can do something like:
Code:
monsterSayPos(creature,"I have almost escaped. Where is that exit ?", Position(pos1.x, pos1.y, pos1.z))
addEvent(monsterSayPos,3000,cid,"There you are...",  Position(pos2.x, pos2.y, pos2.z))
addEvent(monsterSayPos,5000,cid,"my precious experiment!",Position(pos2.x, pos2.y, pos2.z))
addEvent(monsterSayPos,6500,cid,"NO, NOT AGAIN ", Position(pos1.x, pos1.y, pos1.z))
This solution works, but i would like to understand why it fixes the whole thing.The missing part of the script was in the first post and it looks like this:
Code:
function onStepIn(creature, item, position, fromPosition)
    local pos1 = { x = position.x + 3, y = position.y, z = position.z}
    local pos2 = { x = position.x + 5, y = position.y, z = position.z}
Thanks a lot :)
 
This solution works, but i would like to understand why it fixes the whole thing.The missing part of the script was in the first post and it looks like this:
Code:
function onStepIn(creature, item, position, fromPosition)
    local pos1 = { x = position.x + 3, y = position.y, z = position.z}
    local pos2 = { x = position.x + 5, y = position.y, z = position.z}
Thanks a lot :)
The problem is, when you use it, the function in C++ will pop (lua_pop) those values from the table and the next time it wont have those values and it will be an empty table.
I think its that, can't test it now.
https://github.com/otland/forgottenserver/blob/master/src/luascript.cpp#L732-L741
 
@MatheusMkalo is creature a pre-defined userdata?, my bet was lua created the creature table and that was empty because could not index with the parameter.
No creature is never a table. It was error in [C] "say" it was indexing nil inside the source. Some parameter was nil, and the code was trying to index it the only option was the position parameter.
 
Back
Top