Page 1 of 1
How can I make a notification recommend a building to a player in Lua?
Posted: 13 Jun 2020, 19:19
by KINGTUT10101
Hello,
I noticed that some in-game notifications give players tips about which buildings they should use, with some of them including a shortcut to a certain building:
That made me wonder, how can I do this in Lua? I think it would be a nice feature to use in my Prison Expansion plug-in. Any help would be appreciated because for
some weird reason, this Lua functionality isn't documented.
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 13 Jun 2020, 20:11
by CommanderABab
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 14 Jun 2020, 02:58
by Bearbear76
Maybe this was added?
JustAnyone wrote: ↑14 Jun 2020, 01:11
Version 1.8.78
- Added support for own action buttons in notifications
The notifications are hardly documented which is strange but I hardly use it so I didn't think much of it.
From digging up some old code I found how to use notifications (
generally):
Code: Select all
City.showNotification{icon = [Draft], showOnce = [bool], id = [string], text = [string]}
foo{...}
-- is the same as
foo({...})
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 14 Jun 2020, 13:31
by Lobby
So let's change that then, shall we?
The game object I attach the script to is an animation. This is useful since it can hold some frames for us that we will use in the Lua code. There's no need to do it that way
Code: Select all
[
{
"id":"$luanotificationstest00",
"type":"animation", // Use animation to store frames in it
"frames":[{"bmp":"frames.png","w":32,"count":4}],
"script":"#LuaWrapper",
"meta":{
"luawrapper":{
"script":"script.lua",
"dev":true // Allows us to change the code while running,
// Don't use in production!
}
}
}
]
The 4 frames used in that example are provided by that image:

- frames.png (2.93 KiB) Viewed 7981 times
Each frame has a size of 32x32 pixels.
The
script.lua script that spawns a notification when you tap somewhere in your city
Code: Select all
local frameDraft = script:getDraft()
-- Show the notification when we tap somewhere in the city
function script:tap(x, y, lvl)
City.showNotification{
-- Image for the notification, can be extracted from a draft using :getFrame
icon = frameDraft:getFrame(1),
-- Text of the notification
text = 'hi',
-- All of the following attributes are optional...
-- Title in case of an immersive notification
title = 'Title of the window',
-- Whether to draw it with a red background (default: false)
important = true,
-- Whether to offer yes and no buttons (default: false)
question = true,
-- Whether to offer an 'ok' button (default: value of question)
optionOk = true,
-- Whether to offer a close/cancel button (default: true)
closeable = true,
-- If defined: offer a location button that will jump to that location
locationX = 42, locationY = 42,
-- If true: shows the notification as a dialog (default: false)
immersive = false,
-- Will be called if the ok button was pressed
onOk = function() Debug.toast('Ok') end,
-- Will be called if the cancel button was pressed
onCancel = function() Debug.toast('Cancel') end,
-- Will be called if the notification was closed without a button press
onClose = function() Debug.toast('Closed') end,
-- A new feature of version 1.8.78 that allows to add own buttons
actions = {
{
-- Image for the button
icon = frameDraft:getFrame(2),
-- Will be called when button is pressed
onClick = function() Debug.toast('Triangle was clicked') end
},
{
icon = frameDraft:getFrame(3),
onClick = function() Debug.toast('Circle was clicked') end
},
{
icon = frameDraft:getFrame(4),
onClick = function() Debug.toast('Cross was clicked') end
}
}
}
end
In case you are wondering what this syntax means and whether Lua supports named parameters now:
It's just syntactic sugar for
that is calling a function using an in-place defined table as an argument. This is a common way to "fake" named parameters in Lua and as this example illustrates it can be quite useful when you have a lot of optional arguments.
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 14 Jun 2020, 13:54
by Bearbear76
That's one populated table

Re: How can I make a notification recommend a building to a player in Lua?
Posted: 19 Jun 2020, 09:24
by Hadestia
Lobby wrote: ↑14 Jun 2020, 13:31
In case you are wondering what this syntax means and whether Lua supports named parameters now:
It's just syntactic sugar for
that is calling a function using an in-place defined table as an argument. This is a common way to "fake" named parameters in Lua and as this example illustrates it can be quite useful when you have a lot of optional arguments.
What's foo mean?
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 19 Jun 2020, 10:04
by Bearbear76
foo,
bar are usually used to replace "something" in examples.
Meaning it can be anything it's just a placeholder.
Class example in Lua.
Code: Select all
local foo = {}
function foo.new()
local self = setmetatable({}, {__index = foo})
self.bar = 12
return self
end
function foo:print_bar()
print(self.bar)
end
return foo
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 19 Jun 2020, 10:09
by Hadestia
Ølsken wrote: ↑19 Jun 2020, 10:04
foo,
bar are usually used to replace "something" in examples.
Meaning it can be anything it's just a placeholder.
Class example in Lua.
Code: Select all
local foo = {}
function foo.new()
local self = setmetatable({}, {__index = foo})
self.bar = 12
return self
end
function foo:print_bar()
print(self.bar)
end
return foo
Is these are same as json "meta"{} ?
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 19 Jun 2020, 10:16
by Bearbear76
rjroldan1 wrote: ↑19 Jun 2020, 10:09
Is these are same as json "meta"{} ?
Classes?
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 19 Jun 2020, 10:17
by Hadestia
Ølsken wrote: ↑19 Jun 2020, 10:16
rjroldan1 wrote: ↑19 Jun 2020, 10:09
Is these are same as json "meta"{} ?
Classes?
I mean meta tags?
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 19 Jun 2020, 10:18
by Bearbear76
rjroldan1 wrote: ↑19 Jun 2020, 10:17
Ølsken wrote: ↑19 Jun 2020, 10:16
rjroldan1 wrote: ↑19 Jun 2020, 10:09
Is these are same as json "meta"{} ?
Classes?
I mean meta tags?
Ah metatables?
I'll explain in the chatbox
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 24 Jun 2020, 14:40
by Hadestia
Lobby wrote: ↑14 Jun 2020, 13:31
So let's change that then, shall we?
image.png
The game object I attach the script to is an animation. This is useful since it can hold some frames for us that we will use in the Lua code. There's no need to do it that way
Code: Select all
[
{
"id":"$luanotificationstest00",
"type":"animation", // Use animation to store frames in it
"frames":[{"bmp":"frames.png","w":32,"count":4}],
"script":"#LuaWrapper",
"meta":{
"luawrapper":{
"script":"script.lua",
"dev":true // Allows us to change the code while running,
// Don't use in production!
}
}
}
]
main.json
The 4 frames used in that example are provided by that image:
frames.png
Each frame has a size of 32x32 pixels.
The
script.lua script that spawns a notification when you tap somewhere in your city
Code: Select all
local frameDraft = script:getDraft()
-- Show the notification when we tap somewhere in the city
function script:tap(x, y, lvl)
City.showNotification{
-- Image for the notification, can be extracted from a draft using :getFrame
icon = frameDraft:getFrame(1),
-- Text of the notification
text = 'hi',
-- All of the following attributes are optional...
-- Title in case of an immersive notification
title = 'Title of the window',
-- Whether to draw it with a red background (default: false)
important = true,
-- Whether to offer yes and no buttons (default: false)
question = true,
-- Whether to offer an 'ok' button (default: value of question)
optionOk = true,
-- Whether to offer a close/cancel button (default: true)
closeable = true,
-- If defined: offer a location button that will jump to that location
locationX = 42, locationY = 42,
-- If true: shows the notification as a dialog (default: false)
immersive = false,
-- Will be called if the ok button was pressed
onOk = function() Debug.toast('Ok') end,
-- Will be called if the cancel button was pressed
onCancel = function() Debug.toast('Cancel') end,
-- Will be called if the notification was closed without a button press
onClose = function() Debug.toast('Closed') end,
-- A new feature of version 1.8.78 that allows to add own buttons
actions = {
{
-- Image for the button
icon = frameDraft:getFrame(2),
-- Will be called when button is pressed
onClick = function() Debug.toast('Triangle was clicked') end
},
{
icon = frameDraft:getFrame(3),
onClick = function() Debug.toast('Circle was clicked') end
},
{
icon = frameDraft:getFrame(4),
onClick = function() Debug.toast('Cross was clicked') end
}
}
}
end
script.lua
In case you are wondering what this syntax means and whether Lua supports named parameters now:
It's just syntactic sugar for
that is calling a function using an in-place defined table as an argument. This is a common way to "fake" named parameters in Lua and as this example illustrates it can be quite useful when you have a lot of optional arguments.
How many buttons it can support?
Also is it possible to hide the "X,✓" button then replaced by the custom buttons?
Re: How can I make a notification recommend a building to a player in Lua?
Posted: 24 Jun 2020, 15:12
by Lobby
Simply use
in the call to showNotification to get red of the close button. Just add your own buttons to replace it
There's no technical limit on the amount of buttons that it supports, but on smaller screens you will run into displaying issues.