• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

ModalWindow help

Otfan125

Well-Known Member
Joined
Mar 1, 2008
Messages
171
Solutions
1
Reaction score
56
Location
Thais
Hello guys,

So I've been trying to make a NPC send a ModalWindow to the player once the player says a keyword, but I seem to be getting some problems. I'm relatively new to lua, but I can understand languages quickly if they are explained to me. Here is the .lua
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end
local player = Player(cid)
local function sendModalWindowToPlayer(player)
   player:registerEvent("ModalWindow_Tutorial")
    local title = "Pick your favorite color!"
    local message = "You get a free tapestry in the color of your choice!"
    local window = ModalWindow(1000, title, message)

    window:addButton(100, "Confirm")
    window:addButton(101, "Cancel")
    window:addChoice(1, "Red")
    window:addChoice(2, "Orange")
    window:addChoice(3, "Yellow")
    window:addChoice(4, "Green")
    window:addChoice(5, "Blue")
    window:addChoice(6, "Purple")
    window:setDefaultEnterButton(100)
    window:setDefaultEscapeButton(101)
    window:sendToPlayer(player)
end
function creatureSayCallback(cid, type, msg)
     if not npcHandler:isFocused(cid) then
         return false
     end

     local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

     -- here comes what the NPC should do
     if msgcontains(msg,"here") then
        selfSay("Here you are", cid);
        sendModalWindowToPlayer(player)
        end
     return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())


My guess is that player = Player(cid) makes the variable player BE the information you need to mess around with the actual player, and that functions here are called just like in any other languages

This script works up until i say "here", but when it tries to call sendModalWindowToPlayer, it gives me errors:

Code:
Lua Script Error: [Npc interface]
data/npc/scripts/bad demon.lua:onCreatureSay
data/npc/scripts/bad demon.lua:11: attempt to index local 'player' (a nil value)

stack traceback:
        [C]: in function '__index'
        data/npc/scripts/bad demon.lua:11: in function 'sendModalWindowToPlayer'

        data/npc/scripts/bad demon.lua:43: in function 'callback'
        data/npc/lib/npcsystem/npchandler.lua:411: in function 'onCreatureSay'
        data/npc/scripts/bad demon.lua:7: in function <data/npc/scripts/bad demo
n.lua:7>


any help is greatly appreciated, thank you
 
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end

local function sendModalWindowToPlayer(player)
    if player == nil then
        return
    end

    player:registerEvent("ModalWindow_Tutorial")

    local title = "Pick your favorite color!"
    local message = "You get a free tapestry in the color of your choice!"
    local window = ModalWindow(1000, title, message)

    window:addButton(100, "Confirm")
    window:addButton(101, "Cancel")
    window:addChoice(1, "Red")
    window:addChoice(2, "Orange")
    window:addChoice(3, "Yellow")
    window:addChoice(4, "Green")
    window:addChoice(5, "Blue")
    window:addChoice(6, "Purple")
    window:setDefaultEnterButton(100)
    window:setDefaultEscapeButton(101)
    window:sendToPlayer(player)
end

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    -- here comes what the NPC should do
    if msgcontains(msg, "here") then
        selfSay("Here you are", cid)
        sendModalWindowToPlayer(Player(cid))
        end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

cid is not defined outside of the creatureSayCallback scope, so constructing a player object (Player(cid)) will return nil there.
 
Cant you just do something like this?
Code:
local function creatureSayCallback(cid, type, msg)
    local player = Player(cid)
    if msgcontains(msg, "<Whatever word you want>") then
        player:sendModalWindow()
    end
end
 
Code:
local keywordHandler = KeywordHandler:new()
local npcHandler = NpcHandler:new(keywordHandler)
NpcSystem.parseParameters(npcHandler)

function onCreatureAppear(cid)              npcHandler:onCreatureAppear(cid)            end
function onCreatureDisappear(cid)           npcHandler:onCreatureDisappear(cid)         end
function onCreatureSay(cid, type, msg)      npcHandler:onCreatureSay(cid, type, msg)    end
function onThink()                          npcHandler:onThink()                        end

local function sendModalWindowToPlayer(player)
    if player == nil then
        return
    end

    player:registerEvent("ModalWindow_Tutorial")

    local title = "Pick your favorite color!"
    local message = "You get a free tapestry in the color of your choice!"
    local window = ModalWindow(1000, title, message)

    window:addButton(100, "Confirm")
    window:addButton(101, "Cancel")
    window:addChoice(1, "Red")
    window:addChoice(2, "Orange")
    window:addChoice(3, "Yellow")
    window:addChoice(4, "Green")
    window:addChoice(5, "Blue")
    window:addChoice(6, "Purple")
    window:setDefaultEnterButton(100)
    window:setDefaultEscapeButton(101)
    window:sendToPlayer(player)
end

function creatureSayCallback(cid, type, msg)
    if not npcHandler:isFocused(cid) then
        return false
    end

    local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid

    -- here comes what the NPC should do
    if msgcontains(msg, "here") then
        selfSay("Here you are", cid)
        sendModalWindowToPlayer(Player(cid))
        end
    return true
end

npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
npcHandler:addModule(FocusModule:new())

cid is not defined outside of the creatureSayCallback scope, so constructing a player object (Player(cid)) will return nil there.

Hey ! this worked, thanks!
I'm a little confused on how "player" works, it's not defined as "local" or anything, so i'm guessing its syntax within .lua? Also, what is "cid"? I see it used a lot....

Also, if I wanted the npc to send another modwindow after the player clicks "Confirm", i would have to create an "if else" statement right?

if window::addbutton(100, "Why am i here?") == true
then "send another mod window, with different options"


basically i'm trying to create a dialogue between npc and players
 
Hey ! this worked, thanks!
I'm a little confused on how "player" works, it's not defined as "local" or anything, so i'm guessing its syntax within .lua? Also, what is "cid"? I see it used a lot....

Also, if I wanted the npc to send another modwindow after the player clicks "Confirm", i would have to create an "if else" statement right?

if window::addbutton(100, "Why am i here?") == true
then "send another mod window, with different options"


basically i'm trying to create a dialogue between npc and players
What you are doing is something i literally started yesterday hahaha you should use this system for your windows https://otland.net/threads/tfs-1-2-modal-window-helper-lib.238773/
It will make your life so much easier... trust me.. Take a look at my addon doll and mount doll scripts hopefully they will help you. I have given detail on what each function does so it should be easy to change.
 
Here's basically what I'm trying to do, If the player selects one of the options and clicks corfirm, a new dialoque block should appear, and new options should be given!

pretty cool idea huh? I'm sure i'm not the first to come up with this, but I'd love to see it work !

I'd also like to see it work by instead of syaing "Hi" to the npc, Right clicking him would do the job!

http://s33.postimg.org/pg5mcver3/test.png
test.png
 
What you are doing is something i literally started yesterday hahaha you should use this system for your windows https://otland.net/threads/tfs-1-2-modal-window-helper-lib.238773/
It will make your life so much easier... trust me.. Take a look at my addon doll and mount doll scripts hopefully they will help you. I have given detail on what each function does so it should be easy to change.

Hey dude! Thanks for the offer, I have a lot of experience with c++ and C, but ever since I became more and more aware of how this works, I've become addicted to writing lua scripts and watching them work on tibia!

I'll have a look at your scripts, hopefully I'll learn a thing or two! :)
 
Hey dude! Thanks for the offer, I have a lot of experience with c++ and C, but ever since I became more and more aware of how this works, I've become addicted to writing lua scripts and watching them work on tibia!

I'll have a look at your scripts, hopefully I'll learn a thing or two! :)
That is exactly what i have half done lol you will learn and love life trust me.
 
I'll have a look at that once I get this other problem solved :P

I'm trying to make some type of script that when you right click a NPC, you will go into Dialogue with him/her
 
I'll have a look at that once I get this other problem solved :p

I'm trying to make some type of script that when you right click a NPC, you will go into Dialogue with him/her
Dont think possible without source edits.
unless you can give npcs a action/uid
 
Dont think possible without source edits.
unless you can give npcs a action/uid
Sorry, but what exactly do you mean by SourceEdit? I'd like to be able to configure some NPC's to be "usable", as in, the player can right click them and they do something...


edit: I mean i'm sure you understood what i want, but it shouldn't seem so hard hmmmm
 
Sorry, but what exactly do you mean by SourceEdit? I'd like to be able to configure some NPC's to be "usable", as in, the player can right click them and they do something...


edit: I mean i'm sure you understood what i want, but it shouldn't seem so hard hmmmm

Your best bet is to make the NPC not walk and then put the action id on the floot under it. Thus giving the illusion you are using the NPC
 
Your best bet is to make the NPC not walk and then put the action id on the floot under it. Thus giving the illusion you are using the NPC
Hey that sounds like it would work! However, since the NPC is in the way, wouldn't it right-click on the NPC instead of directly to the floor? I'm going to try it and see what happens :-)
 
Hey that sounds like it would work! However, since the NPC is in the way, wouldn't it right-click on the NPC instead of directly to the floor? I'm going to try it and see what happens :)
I tested it before i said it with a lever it works =) you cant "use" an npc when you click on him and press use you are really just using the floor
 
I tested it before i said it with a lever it works =) you cant "use" an npc when you click on him and press use you are really just using the floor

I see what you mean! I tested it by putting an apple underneath the npc, CTRL+right clicking on him and selecting "use", and my character ate the apple! So I see what the solution can be! However!!!, I want to be able to "use" the floor underneath the npc without having to do CTRL+RIGHTCLICK... do i make sense? If i only right click where the NPC is at, i will "attack" the npc, and changing the value (attackable="1" to attackable="0") in my npc.lua, it wouldn't change this! :/
 
source edit You have to edit the files that create TFS compiler. That TFS thing you click to open the server.....Edit the files that make that.
 
I really dont tihnk there is a way to do this. to be able to "right click" an NPC to use it is client side maybe OTclient can do it. Your best solution to do this in tibia client is to make players right click use or CTRL + click.
 
Back
Top