Lua chatbox

The Lua scripting language allows you to give your plugin more advanced features.

Moderator: Plugin Moderators

User avatar
KINGTUT10101
1,000,000 inhabitants
Reactions:
Posts: 2069
Joined: Thu Jul 07, 2016 22:50
Location: 'Merica
Plugins: Show
Version: Beta
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#101

Post by KINGTUT10101 »

Dang, I was hoping it was just a simple command. I actually had the same idea to make rounded corners that way, but I thought I would ask you about it first.

User avatar
Uncle Koala
Metropolitan
Reactions:
Posts: 135
Joined: Sun Apr 05, 2020 14:53
Location: Pitcairn Islands
Plugins: Show
Version: Beta
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#102

Post by Uncle Koala »

Spent quite some time trying to get this to work
sand.gif
sand.gif (2.63 MiB) Viewed 730 times
I wonder why does Builder.buildGround remove trees? Would be cooler if it didn't but then we wouldn't have this GIF :space
Lua is easy! :lua:

Code: Select all

if Car.GoingToCrashIntoEachOther()
   then Car.DontCrash
end

User avatar
ian`
Townsman
Reactions:
Posts: 81
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#103

Post by ian` »

Uncle Koala wrote:
Mon Aug 17, 2020 0:03
Spent quite some time trying to get this to work

sand.gif

I wonder why does Builder.buildGround remove trees? Would be cooler if it didn't but then we wouldn't have this GIF :space
For now, you can try like this:

Code: Select all    Reset

local tree local treeFrame function script:earlyTap(x,y) if Tile.isTree(x,y) then -- condition if any trees in position tree = Tile.getTreeDraft(x,y) -- get tree draft treeFrame = Tile.getTreeFrame(x,y) -- get tree frame Builder.buildGround('$asphalt00',x,y) Builder.buildTree(tree,x,y,treeFrame) -- build tree after build ground else -- condition if no tree in position Builder.buildGround('$asphalt00',x,y) end return false end
Interactive Lua editor
Run
:fire

User avatar
Uncle Koala
Metropolitan
Reactions:
Posts: 135
Joined: Sun Apr 05, 2020 14:53
Location: Pitcairn Islands
Plugins: Show
Version: Beta
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#104

Post by Uncle Koala »

Thanks! That works nicely!
Lua is easy! :lua:

Code: Select all

if Car.GoingToCrashIntoEachOther()
   then Car.DontCrash
end

User avatar
Uncle Koala
Metropolitan
Reactions:
Posts: 135
Joined: Sun Apr 05, 2020 14:53
Location: Pitcairn Islands
Plugins: Show
Version: Beta
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#105

Post by Uncle Koala »

How can I get the y position of a building using City.getBuilding? I can get x by doing

Code: Select all

 local draft = $ktt_koala01
City.getBuilding(1, draft)
but I don't know how to get y value
Lua is easy! :lua:

Code: Select all

if Car.GoingToCrashIntoEachOther()
   then Car.DontCrash
end

User avatar
ian`
Townsman
Reactions:
Posts: 81
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#106

Post by ian` »

Code: Select all    Reset

local x, y = City.getBuilding(1, draft) local x,y,scale = City.getView() local draft, number = City.getRank()
Interactive Lua editor
Run
:fire

User avatar
Uncle Koala
Metropolitan
Reactions:
Posts: 135
Joined: Sun Apr 05, 2020 14:53
Location: Pitcairn Islands
Plugins: Show
Version: Beta
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#107

Post by Uncle Koala »

I knew I was missing something simple. Thanks! :space
town of marble.gif
town of marble.gif (11.09 MiB) Viewed 692 times
Lua is easy! :lua:

Code: Select all

if Car.GoingToCrashIntoEachOther()
   then Car.DontCrash
end

User avatar
Uncle Koala
Metropolitan
Reactions:
Posts: 135
Joined: Sun Apr 05, 2020 14:53
Location: Pitcairn Islands
Plugins: Show
Version: Beta
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#108

Post by Uncle Koala »

Builder.BuildBuilding doesn't seem to work with RCI.

Building a park works...

Code: Select all    Reset

function script:nextDay() draft = Draft.getDraft("$park01") Debug.toast("Building", draft) if Builder.isBuildingBuildable(draft, 0, 0) then Debug.toast("Buildable!" ) else Debug.toast("Nope!" ) end Builder.buildBuilding(draft, 0, 0, 0) end
Interactive Lua editor
Run
... but if I change it to a residential house, it doesn't work.

Code: Select all    Reset

function script:nextDay() draft = Draft.getDraft("$res01") Debug.toast("Building", draft) if Builder.isBuildingBuildable(draft, 0, 0) then Debug.toast("Buildable!" ) else Debug.toast("Nope!" ) end Builder.buildBuilding(draft, 0, 0, 0) end
Interactive Lua editor
Run
I also tried manually placing a zone across the entire map, but it still doesn't want to build residential stuff on it.
Lua is easy! :lua:

Code: Select all

if Car.GoingToCrashIntoEachOther()
   then Car.DontCrash
end

User avatar
CommanderABab
AB
Reactions:
Posts: 8962
Joined: Tue Jun 07, 2016 21:12
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#109

Post by CommanderABab »

Is it zoned?
Image

User avatar
Uncle Koala
Metropolitan
Reactions:
Posts: 135
Joined: Sun Apr 05, 2020 14:53
Location: Pitcairn Islands
Plugins: Show
Version: Beta
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#110

Post by Uncle Koala »

Yes, everything is zoned. I also tried adding Builder.buildZone to my script. The zone is built but the house isn't, and the toast still says Nope! which means Lua doesn't see that tile as buildable even if it's zoned.
Lua is easy! :lua:

Code: Select all

if Car.GoingToCrashIntoEachOther()
   then Car.DontCrash
end

User avatar
CommanderABab
AB
Reactions:
Posts: 8962
Joined: Tue Jun 07, 2016 21:12
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#111

Post by CommanderABab »

Why don't you try testing for buildable park and if that is true, build the zone and the res01 anyway. :)

Is it next to a road? :)
Image

User avatar
Uncle Koala
Metropolitan
Reactions:
Posts: 135
Joined: Sun Apr 05, 2020 14:53
Location: Pitcairn Islands
Plugins: Show
Version: Beta
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#112

Post by Uncle Koala »

Thanks 🆎, it only builds if there's a road next to that tile. A bit weird, but understandable. I was trying to build RCI stuff just to see if they will be set as "Untouchable" if they are built by Lua. Turns out they won't be set as untouchable. :space

Suggestion to devs: please add something like Tile.setUntouchable(x, y) :ugeek:
Lua is easy! :lua:

Code: Select all

if Car.GoingToCrashIntoEachOther()
   then Car.DontCrash
end

MarioBitMap
Villager
Reactions:
Posts: 16
Joined: Fri Jul 31, 2020 22:54
Location: Spain
Plugins: Show
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#113

Post by MarioBitMap »

Why isn't my script working?
When playing, I can Access to the menu I've created, but when pressing the button it doesn't shows the next menu.

Code: Select all    Reset

local function showMenu(dialog) local parent = GUI.get'$menuparent' local d = '$townhall01' local x = City.countBuildings(d) GUI.createMenu{ source = parent, actions = { {icon = Icon.BUILD, text = 'Town Halls:' , onClick = function() Debug.toast(x) end}, } } end local function showDialog() local dialog dialog = GUI.createDialog{ icon = script:getDraft():getPreviewFrame(), title = 'Test 1', text = 'Will this work? lol', width = 250, height = 120, actions = { { id = '$menuparent', icon = Icon.BUILD, text = equis, onClick = function() showMenu(dialog) end, autoClose = false }, } } closeDialog = dialog.close end function script:init() if closeDialog then closeDialog() closeDialog = nil showDialog() end end function script:event(_, _, _, event) if event == Script.EVENT_TOOL_ENTER then GUI.get('cmdCloseTool'):click() showDialog() end end
Interactive Lua editor
Run
Thanks in advance!! :lua:

Code: Select all    Reset

[ { "id":"MarioBitMap", "category":"Plugin_Creator", "skills":[{"skill":"Pixel_Artist"},{"skill":"Learning_C++_&_TTJson/Lua"}] } ]
JSON checker
Check

User avatar
Lobby
Developer
Reactions:
Posts: 3610
Joined: Sun Oct 26, 2008 12:34
Plugins: Show
Version: Beta

Platform

Re: Lua chatbox

#114

Post by Lobby »

Hi, I guess it's because of these lines:

Code: Select all

local d = '$townhall01'
local x = City.countBuildings(d)
City.countBuildings expects a draft rather than the id of a draft. This should work:

Code: Select all

local d = Draft.getDraft('$townhall01')
local x = City.countBuildings(d)
=^._.^= ∫

MarioBitMap
Villager
Reactions:
Posts: 16
Joined: Fri Jul 31, 2020 22:54
Location: Spain
Plugins: Show
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#115

Post by MarioBitMap »

Oh I see, thankyou very much!
Just testing the implementation of a new resource as a variable through :lua:

Code: Select all    Reset

[ { "id":"MarioBitMap", "category":"Plugin_Creator", "skills":[{"skill":"Pixel_Artist"},{"skill":"Learning_C++_&_TTJson/Lua"}] } ]
JSON checker
Check

MarioBitMap
Villager
Reactions:
Posts: 16
Joined: Fri Jul 31, 2020 22:54
Location: Spain
Plugins: Show
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#116

Post by MarioBitMap »

:lua: Is there any way to load and image with lua and use it as an icon? I mean, can I assign an image to a variable wich will be shown when I use that variable in
... icon = Icon.EXAMPLE, ...
or are Icon.EXAMPLE a type of harcoded resource that can't be added by users?
Thanks in advance!

Code: Select all    Reset

[ { "id":"MarioBitMap", "category":"Plugin_Creator", "skills":[{"skill":"Pixel_Artist"},{"skill":"Learning_C++_&_TTJson/Lua"}] } ]
JSON checker
Check

User avatar
ian`
Townsman
Reactions:
Posts: 81
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#117

Post by ian` »

I think Icon.EX is hardcoded, because its actually return number.

If you want to add your icons, you still have to write the Json code to making a draft for the image.

Code: Select all    Reset

[ { "id":"iconId", "type":"animation", "frames":[{"bmp":"icon.png", "w":24, "h":24}] } ]
JSON checker
Check
If you want to use it on multiple line, you can assigned it to a variable.

Code: Select all    Reset

-- you also can make the Json code from lua and call it with Draft.append local iconImage = [[ [ { "id":"iconId", "type":"animation", "frames":[{"bmp":"icon.png", "w":24, "h":24}] } ] ]] Draft.append(iconImage) local yourIcon = Draft.getDraft('iconId'):getFrame(1) -- Or you can make it to be a table local icon = {} icon.FIRST_ICON = Draft.getDraft('iconId'):getFrame(1) icon.SECOND_ICON = Draft.getDraft('iconId'):getFrame(1) icon.ANOTHER_ICON = Draft.getDraft('iconId'):getFrame(1) -- To use GUI.createDialog{ icon = icon.FIRST_ICON }
Interactive Lua editor
Run
:fire

MarioBitMap
Villager
Reactions:
Posts: 16
Joined: Fri Jul 31, 2020 22:54
Location: Spain
Plugins: Show
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#118

Post by MarioBitMap »

I'll test it tomorrow, it's 4am lol. It makes sense I have to draft the json and get the image from there.
Thanks for the help and spending your time into this, hope you have a great day/night!
:bd

Code: Select all    Reset

[ { "id":"MarioBitMap", "category":"Plugin_Creator", "skills":[{"skill":"Pixel_Artist"},{"skill":"Learning_C++_&_TTJson/Lua"}] } ]
JSON checker
Check

MarioBitMap
Villager
Reactions:
Posts: 16
Joined: Fri Jul 31, 2020 22:54
Location: Spain
Plugins: Show
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#119

Post by MarioBitMap »

:lua: There's an error. It says attempt to call a nil value, at
Draft .append(iconImage)
However, the forum's lua tester doesn't agree with the error given at the game with loading the lua file.
Looks like the game doesn't recognizes Draft functions.

When cut- pasting the code into a local function the error disappears, but the game isn't able to show the icon or open the menu it is linked to.
Show
Code

Code: Select all    Reset

--This is a test for TT:TT plugin, (TheoTown Time Travel) --Sill got to move part of this into a data type json object, not a menu. local function showMenu(dialog) local parent = GUI.get'$menuparent' local d = Draft.getDraft('$res00') --Will be woodcutter00 in a future. local x = City.countBuildings(d) local x2 = 1.15 local wood = math.floor(x2*x + 0.51) local woodIconJson = [[ [ { "id":"WCRIcon", "type":"animation", "frames":[{"bmp":"ancienticon.png"}] } ] ]] Draft.append('woodIconJson') local iconWOOD = Draft.getDraft('WCRIcon'):getFrame(1) GUI.createMenu{ source = parent, actions = { {icon = iconWOOD, text = "Houses: "..wood, onClick = function() end}, } } end local function showDialog() local dialog dialog = GUI.createDialog{ icon = script:getDraft():getPreviewFrame(), title = 'Test 1', text = 'Will this work? lol', width = 250, height = 120, actions = { { id = '$menuparent', icon = IconWOOD, text = equis, onClick = function() showMenu(dialog) end, autoClose = false }, } } closeDialog = dialog.close end function script:init() if closeDialog then closeDialog() closeDialog = nil showDialog() end end function script:event(_, _, _, event) if event == Script.EVENT_TOOL_ENTER then GUI.get('cmdCloseTool'):click() showDialog() end end
Interactive Lua editor
Run

Code: Select all    Reset

[ { "id":"MarioBitMap", "category":"Plugin_Creator", "skills":[{"skill":"Pixel_Artist"},{"skill":"Learning_C++_&_TTJson/Lua"}] } ]
JSON checker
Check

User avatar
ian`
Townsman
Reactions:
Posts: 81
Joined: Sat Apr 04, 2020 17:36
Location: Indonesien
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#120

Post by ian` »

Code: Select all    Reset

Draft.append(woodIconJson) -- variable name aren't string, don't use quotation marks.
Interactive Lua editor
Run
:fire

Post Reply Previous topicNext topic

Return to “Lua Scripting”