Jump to content

[help] How to create a pop-up menù?


Recommended Posts

Hello guys, today I was trying to make a menu appear, so I searched in the "end game screen".

The screens and text appears, but not the buttons (and after some seconds the game freezes)

 

here's the script (I copied it from endgamedialog.lua)

 

 

local Screen = require "widgets/screen"
local Button = require "widgets/button"
local AnimButton = require "widgets/animbutton"
local ImageButton = require "widgets/imagebutton"
local Text = require "widgets/text"
local Image = require "widgets/image"
local UIAnim = require "widgets/uianim"
local Widget = require "widgets/widget"
local Menu = require "widgets/menu"
local PopupDialogScreen = require "screens/popupdialog"
local buttons = 2

local CustomScreen= Class(Screen, function(self, buttons)
    Screen._ctor(self, "CustomScreen")

    --darken everything behind the dialog
    self.black = self:AddChild(Image("images/global.xml", "square.tex"))
    self.black:SetVRegPoint(ANCHOR_MIDDLE)
    self.black:SetHRegPoint(ANCHOR_MIDDLE)
    self.black:SetVAnchor(ANCHOR_MIDDLE)
    self.black:SetHAnchor(ANCHOR_MIDDLE)
    self.black:SetScaleMode(SCALEMODE_FILLSCREEN)
    self.black:SetTint(0,0,0,1)    
    
    self.proot = self:AddChild(Widget("ROOT"))
    self.proot:SetVAnchor(ANCHOR_MIDDLE)
    self.proot:SetHAnchor(ANCHOR_MIDDLE)
    self.proot:SetPosition(0,0,0)
    self.proot:SetScaleMode(SCALEMODE_PROPORTIONAL)

    --throw up the background
    self.bg = self.proot:AddChild(Image("images/globalpanels.xml", "panel_upsell.tex"))
    self.bg:SetVRegPoint(ANCHOR_MIDDLE)
    self.bg:SetHRegPoint(ANCHOR_MIDDLE)
    self.bg:SetScale(0.8,0.4,0.8)
    
    --title    
    self.title = self.proot:AddChild(Text(TITLEFONT, 50))
    self.title:SetPosition(0, 180, 0)
    self.title:SetString("Hello")

    --text
    self.text = self.proot:AddChild(Text(BODYTEXTFONT, 30))
    self.text:SetVAlign(ANCHOR_TOP)

    local character = GetPlayer().profile:GetValue("characterinthrone") or "wilson"

    self.text:SetPosition(0, -60, 0)
    self.text:SetString("I'm testing some stuff")
    self.text:EnableWordWrap(true)
    self.text:SetRegionSize(700, 350)
    
    
    --create the menu itself
    local button_w = 200
    local space_between = 20
    local spacing = button_w + space_between
    
    self.menu = self.proot:AddChild(Widget("menu"))
    local total_w = #buttons*button_w
    if #buttons > 1 then
        total_w = total_w + space_between*(#buttons-1)
    end
    
    self.menu:SetPosition(100, -220,0)
    
    local pos = Vector3(0,0,0)
    for k,v in ipairs(buttons) do
        local button = self.menu:AddChild(ImageButton())
        button:SetPosition(pos)
        button:SetText(v.text)
        button:SetOnClick( function() TheFrontEnd:PopScreen(self) v.cb() end )
        button.text:SetColour(0,0,0,1)
        button:SetFont(BUTTONFONT)
        button:SetTextSize(40)    
        pos = pos + Vector3(spacing, 0, 0)  
        
        self.default_focus = button
    end

    self.buttons = buttons
end)

return CustomScreen

 

 

and this is the log

 

customscreen.lua:62: attempt to get length of local 'buttons' (a nil value)
LUA ERROR stack traceback:
        D:/Programmi/Steam/steamapps/common/dont_starve/data/../mods/www/scripts/screens/customscreen.lua(62,1) in function '_ctor'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/class.lua(98,1) in function 'customscreen'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/../mods/www/scripts/prefabs/hellshop.lua(136,1) in function 'fn'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(126,1)
        =[C] in function 'SpawnPrefab'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/mainfunctions.lua(156,1) in function 'SpawnPrefab'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/util.lua(23,1) in function 'DebugSpawn'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/consolecommands.lua(24,1) in function 'c_spawn'
        c_spawn("hellshop")(1,1) in main chunk
        =[C] in function 'pcall'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/screens/consolescreen.lua(88,1) in function 'Run'
    ...
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/widgets/textedit.lua(70,1) in function 'OnRawKey'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/widgets/widget.lua(94,1) in function 'OnRawKey'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/widgets/widget.lua(94,1) in function 'OnRawKey'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/widgets/widget.lua(94,1) in function 'OnRawKey'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/screens/consolescreen.lua(40,1) in function 'OnRawKey'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(699,1) in function 'OnRawKey'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/frontend.lua(117,1) in function 'fn'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/events.lua(46,1) in function 'HandleEvent'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/input.lua(157,1) in function 'OnRawKey'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/input.lua(324,1)
scripts/frontend.lua(717,1) SCRIPT ERROR! Showing error screen    
.../common/dont_starve/data/scripts/consolecommands.lua:25: attempt to index local 'inst' (a nil value)
...mapps/common/dont_starve/data/scripts/stategraph.lua:110: calling 'ProfilerPush' on bad self (string expected, got nil)
LUA ERROR stack traceback:
        =[C] in function 'ProfilerPush'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/stategraph.lua(110,1) in function 'Update'
        D:/Programmi/Steam/steamapps/common/dont_starve/data/scripts/update.lua(127,1)
Could not unload undefined prefab 0x350a87f2 (note)
Could not unload undefined prefab 0x350a87f2 (note)
Could not unload undefined prefab 0x350a87f2 (note)
Could not unload undefined prefab 0x350a87f2 (note)
Could not unload undefined prefab 0x350a87f2 (note)
Could not unload undefined prefab 0x350a87f2 (note)
scripts/mods.lua(293,1) unloading prefabs for mod MOD_www

 

Also tried to edit the pausescreen with this:

local Screen = require "widgets/screen"
local Button = require "widgets/button"
local AnimButton = require "widgets/animbutton"
local Menu = require "widgets/menu"
local Text = require "widgets/text"
local Image = require "widgets/image"
local UIAnim = require "widgets/uianim"
local Widget = require "widgets/widget"
local PopupDialogScreen = require "screens/popupdialog"

local customscreen = Class(Screen, function(self)
    Screen._ctor(self, "customscreen")

    self.active = true
    SetPause(true,"pause")
    
    --darken everything behind the dialog
    self.black = self:AddChild(Image("images/global.xml", "square.tex"))
    self.black:SetVRegPoint(ANCHOR_MIDDLE)
    self.black:SetHRegPoint(ANCHOR_MIDDLE)
    self.black:SetVAnchor(ANCHOR_MIDDLE)
    self.black:SetHAnchor(ANCHOR_MIDDLE)
    self.black:SetScaleMode(SCALEMODE_FILLSCREEN)
    self.black:SetTint(0,0,0,.75)    

    self.proot = self:AddChild(Widget("ROOT"))
    self.proot:SetVAnchor(ANCHOR_MIDDLE)
    self.proot:SetHAnchor(ANCHOR_MIDDLE)
    self.proot:SetPosition(0,0,0)
    self.proot:SetScaleMode(SCALEMODE_PROPORTIONAL)

    --throw up the background
    self.bg = self.proot:AddChild(Image("images/globalpanels.xml", "small_dialog.tex"))
    self.bg:SetVRegPoint(ANCHOR_MIDDLE)
    self.bg:SetHRegPoint(ANCHOR_MIDDLE)
    self.bg:SetScale(1.5,1.2,1.2)
    
    --title    
    self.title = self.proot:AddChild(Text(TITLEFONT, 50))
    self.title:SetPosition(0, 50, 0)
    self.title:SetString("Do you need something?")
    
    --create the menu itself
    local player = GetPlayer()
    local button_w = 160
    
    local buttons = {}
    table.insert(buttons, {text="Button1", cb=function() TheFrontEnd:PopScreen(self) if not self.was_paused then SetPause(false) end end })
        
    self.menu = self.proot:AddChild(Menu(buttons, button_w, true))
    self.menu:SetPosition(-(button_w*(#buttons-1))/2, -65, 0)

    TheInputProxy:SetCursorVisible(true)
    self.default_focus = self.menu
end)

function customscreen:OnControl(control, down)
    if customscreen._base.OnControl(self,control, down) then return true end

    if (control == CONTROL_PAUSE or control == CONTROL_CANCEL) and not down then    
        self.active = false
        TheFrontEnd:PopScreen()
        SetPause(false)
        return true
    end

end

function customscreen:OnUpdate(dt)
    if self.active then
        SetPause(true)
    end
end

function customscreen:OnBecomeActive()
    customscreen._base.OnBecomeActive(self)
    -- Hide the topfade, it'll obscure the pause menu if paused during fade. Fade-out will re-enable it
    TheFrontEnd:HideTopFade()
end

return customscreen

 

And in this case it wont even show up (and the log says nothing)

 

..

 

Of course, this is the first time I'm trying to add a menù screen, so I have no clue... Can anyone help me? Thanks

Link to comment
Share on other sites

Why are you doing this?

local total_w = #buttons*button_wif #buttons > 1 then    total_w = total_w + space_between*(#buttons-1)end

The error is because you are trying to get the length of the "buttons" table, which you haven't declared.

Link to comment
Share on other sites

Why are you doing this?

local total_w = #buttons*button_wif #buttons > 1 then    total_w = total_w + space_between*(#buttons-1)end

The error is because you are trying to get the length of the "buttons" table, which you haven't declared.

 

Yes, I know that (as the log said), but I can't understand where "buttons" is declared in the original lua file (I can't find it anywhere)

Link to comment
Share on other sites

Yes, I know that (as the log said), but I can't understand where "buttons" is declared in the original lua file (I can't find it anywhere)

 

It's declared in the function parameters. Where it's getting it from, I'm not sure.

 

What are you trying to do with this? It'd be easier to write your own screen than it would to work with an already existing one.

Link to comment
Share on other sites

It's declared in the function parameters. Where it's getting it from, I'm not sure.

 

What are you trying to do with this? It'd be easier to write your own screen than it would to work with an already existing one.

 

I made a prefab that should ask the player

 

"do you need help?"

 

with two possible answers ("yes" or "no")

 

and of course, with a function-call if the choose is "Yes"

Link to comment
Share on other sites

I made a prefab that should ask the player

 

"do you need help?"

 

with two possible answers ("yes" or "no")

 

and of course, with a function-call if the choose is "Yes"

 

Ah, that's cake, you don't even need a new screen! You may need to add GLOBAL to some of these functions, but your log will tell you which ones.

local PopupDialogScreen = require "screens/popupdialog"local function dialogchoice(inst)    local function yeschoice(inst)        SetPause(false)        TheFrontEnd:PopScreen()        --Stuff    end    local function nochoice(inst)        SetPause(false)        TheFrontEnd:PopScreen()        --Stuff    end    SetPause(true)    local options = {        {text="Yes.", cb = yeschoice},        {text="No.", cb = nochoice},    }    TheFrontEnd:PushScreen(PopupDialogScreen(    "Title of the dialog.",    "Whatever else you want to put here.",    options))enddialogchoice(inst)   
Link to comment
Share on other sites

 

Ah, that's cake, you don't even need a new screen! You may need to add GLOBAL to some of these functions, but your log will tell you which ones.

local PopupDialogScreen = require "screens/popupdialog"local function dialogchoice(inst)    local function yeschoice(inst)        SetPause(false)        TheFrontEnd:PopScreen()        --Stuff    end    local function nochoice(inst)        SetPause(false)        TheFrontEnd:PopScreen()        --Stuff    end    SetPause(true)    local options = {        {text="Yes.", cb = yeschoice},        {text="No.", cb = nochoice},    }    TheFrontEnd:PushScreen(PopupDialogScreen(    "Title of the dialog.",    "Whatever else you want to put here.",    options))enddialogchoice(inst)   

 

Ah, that's good (the fact that I don't need a new screen) but uhm... Nothing happens when I spawn the prefab, and the log doesn't report errors :s

Link to comment
Share on other sites

Could you post the prefab?

 

Here

 

 

local PopupDialogScreen = require "screens/popupdialog"

 

(...)

 

local function dialogchoice(inst)

    

    print("something should happen here")

    

    local function yeschoice(inst)

        SetPause(false)

        TheFrontEnd:PopScreen()

        --Stuff

    end

 

    local function nochoice(inst)

        SetPause(false)

        TheFrontEnd:PopScreen()

        --Stuff

    end

 

    SetPause(true)

    local options = {

        {text="Yes.", cb = yeschoice},

        {text="No.", cb = nochoice},

    }

 

    TheFrontEnd:PushScreen(PopupDialogScreen(

 

    "Title of the dialog.",

    "Whatever else you want to put here.",

 

    options))

end

 

local function fn(Sim)

    

    local inst = CreateEntity()

    local minimap = inst.entity:AddMiniMapEntity()

    minimap:SetPriority( 5 )

    minimap:SetIcon( "pigking.png" )

    minimap:SetPriority( 1 )

    inst.entity:AddTransform()

    inst.entity:AddAnimState()

    inst.entity:AddSoundEmitter()

    inst.entity:AddDynamicShadow()

    inst.DynamicShadow:SetSize( 10, 5 )

    

    MakeObstaclePhysics(inst, 2, .5)

    inst.Transform:SetScale(2,2,2)

    

    inst.AnimState:SetBank("thing")

    inst.AnimState:SetBuild("thing")

    --inst.AnimState:PlayAnimation("idle", true)

    

    inst:SetStateGraph("SGthingthing")

    

    --inst:AddComponent("inspectable")

    --inst:AddComponent("talkable")

    

    inst:AddComponent("talker")

    inst.components.talker.fontsize = 33

    inst.components.talker.font = TALKINGFONT

    inst.components.talker.offset = Vector3(0,-400,0)

    

    inst.soundsname = "wallace"

    

    inst.task2 = inst:DoPeriodicTask(math.random(10,20), function() Talk(inst) end)

    

    dialogchoice(inst)

    

    return inst

end

 

Link to comment
Share on other sites

You're calling it too quickly.

inst:DoTaskInTime(1, function() dialogchoice(inst) end)

That will work.

 

Oh o_o yes now it works, thanks a lot again, Debugman ^^

I couldn't think that is necessary to wait to call a popup-function :/

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

Please be aware that the content of this thread may be outdated and no longer applicable.

×
  • Create New...