There is more than one way to do this, but the easiest, in my option, is by using the advisor.
Again, like in my other tutorials, I'm going to use an example to demonstrate how this is done. The example I'm going to use is offering the player a chance to buy a lottery ticket at the beginning of every turn.
First, we'll need to set up an advice thread. This involves adding stuff to 2 files.
1. export_descr_advice.txt
Code:
;-----------------------------------------------
AdviceThread PromptThread
GameArea Campaign
Item PromptItem1
Uninhibitable
Verbosity 0
Threshold 1
MaxRepeats 0
RepeatInterval 1
Attitude Normal
Presentation Default
Title PromptTitle1
Text PromptText1
and 2. text\export_advice.txt
Code:
{PromptTitle1}Test your luck
{PromptText1}Would you like to buy a lottery ticket for a chance to win 10k? The cost of the ticket is 1 gold. Click on the advisor portrait to try your luck, or on the dismiss button to decline.
Now, the code that you'll add to the script:
Code:
declare_counter isOfferActive
monitor_event FactionTurnStart FactionIsLocal
advance_advice_thread PromptThread 1
; This assumes that you have declare_show_me at the top of the script,
; which opens the advisor text box for you automatically.
; If you do not, uncomment the 2 lines below.
;select_ui_element advisor_portrait_button
;simulate_mouse_click lclick_up
set_counter isOfferActive 1
end_monitor
; Offer accepted
monitor_event ButtonPressed ButtonPressed advisor_portrait_button
and I_CompareCounter isOfferActive = 1
console_command add_money -1 ; Cost of the lottery ticket
if RandomPercent > 99
console_command add_money 10000 ; You won!
end_if
set_counter isOfferActive 0
end_monitor
; Offer rejected
monitor_event ButtonPressed ButtonPressed advisor_dismiss_button
and I_CompareCounter isOfferActive = 1
; greedy bastard! charge him 1 gold anyways!
console_command add_money -1
set_counter isOfferActive 0
end_monitor
And it's that simple!