• 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!

Solved How to remove creature focus?

whitevo

Feeling good, thats what I do.
Joined
Jan 2, 2015
Messages
3,452
Solutions
1
Reaction score
625
Location
Estonia
I tried to remove player target, but apparently this is not simple to do with Lua.

the only function we get to use is: creature:setTarget(target)
but the problem with that is, the target must exist near the creature

Is there any network message to remove current focus target from player/creature?

If not what should be done in sources so that creature:setTarget() will remove focus from targets?

related codes in luascript.cpp and creature.cpp
Code:
int LuaScriptInterface::luaCreatureSetTarget(lua_State* L)
{
   // creature:setTarget(target)
   Creature* creature = getUserdata<Creature>(L, 1);
   if (creature) {
       Creature* target = getCreature(L, 2);
       pushBoolean(L, creature->setAttackedCreature(target));
   } else {
       lua_pushnil(L);
   }
   return 1;
}

bool Creature::setAttackedCreature(Creature* creature)
{
   if (creature) {
       const Position& creaturePos = creature->getPosition();
       if (creaturePos.z != getPosition().z || !canSee(creaturePos)) {
           attackedCreature = nullptr;
           return false;
       }

       attackedCreature = creature;
       onAttackedCreature(attackedCreature);
       attackedCreature->onAttacked();
   } else {
       attackedCreature = nullptr;
   }

   for (Creature* summon : summons) {
       summon->setAttackedCreature(creature);
   }
   return true;
}

EDIT:

Pfft didn't even matter when the creature is player.
cant change player target.

So what I did was this.

Code:
if target and target:getId() == cid then
                if enemy:isPlayer() then
                    local previousPos = enemy:getPosition()
                    teleport(enemy, tokenPos)
                    teleport(enemy, previousPos)
                else
                    target:setTarget()
                end
            end
The tokenPos is set into middle of nowhere and when player teleported there, then it will loose target.
Still looking for better solutions.
 
Last edited:
Try this method:

Code:
enemy:setTarget(nil)

local msgPlayer = NetworkMessage()
msgPlayer:addByte(0xA3)
msgPlayer:addU32(0x00)
msgPlayer:sendToPlayer(enemy)
 
I could prolly compile once a month. So its not something i can just try.
actually you dont even need to touch the source files, i just said that because is a way to solve this, passing to lua player->sendCancelTarget() but since its only a network message lua already can do this, just add this function to your lua player lib
Code:
function Player.sendCancelTarget(self)
    local msg = NetworkMessage()
    msg:addByte(0xA3)
    msg:addU32(0x00)
    msg:sendToPlayer(self)
    msg:delete()
end

an example of usage:
Code:
function onUse(player, item, fromPosition, target, toPosition, isHotkey)
    player:sendCancelTarget()
    return true
end
this should work, if dont let me know.
 
Try this method:

Code:
enemy:setTarget(nil)

local msgPlayer = NetworkMessage()
msgPlayer:addByte(0xA3)
msgPlayer:addU32(0x00)
msgPlayer:sendToPlayer(enemy)
setting nil is not necessary.
Just the network message works fine.


About network messages. Did some digging and found some useful network messages, but still don't understand what byte codes I need to use to make them work.
Making new thread for that here: How to use NetworkMessages: ...
 
Back
Top