Lua chatbox

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

Moderator: Plugin Moderators

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Lua chatbox

#1

Post by Bearbear76 »

A place to ask and share stuff about Lua!

Rules:
  • Must be about Lua, we have a general chatbox for other stuff.
  • Be nice to each other!

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#2

Post by Bearbear76 »

How to use City.earnMoney(amount, x, y, showOverlay, budgetItem)

In order to use City.earnMoney() in the first place your script would need to be privileged

JSON:

Code: Select all

"id": "$script",
"type": "script",
"script": "example.lua",
"privileged": [privilege key] // <-- You need this to use this function
Note: privileged keys are only given to trusted members!

Once you make your script privileged you can the function City.earnMoney()

parameters:
  • amount [number]: The amount of money you will earn when the function is called.
  • x [number]: X value of where the green text will appear.
  • y [number]: Y value of where the green text will appear.
  • showOverlay [boolean]: Whether to show the overlay or not. (default false)
  • budgetItem [draft]: *I have no idea what this is*
showOverlay example:
showOverlay.gif
showOverlay.gif (1.11 MiB) Viewed 14321 times
Green text example:
20200618_202715.jpg
20200618_202715.jpg (26.27 KiB) Viewed 14321 times
I hope this answered your question @rjroldan1

:jb: Bear Out!

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#3

Post by Hadestia »

Ølsken wrote:
18 Jun 2020, 13:41
How to use City.earnMoney(amount, x, y, showOverlay, budgetItem)

In order to use City.earnMoney() in the first place your script would need to be privileged

JSON:

Code: Select all

"id": "$script",
"type": "script",
"script": "example.lua",
"privileged": [privilege key] // <-- You need this to use this function
Note: privileged keys are only given to trusted members!

Once you make your script privileged you can the function City.earnMoney()

parameters:
  • amount [number]: The amount of money you will earn when the function is called.
  • x [number]: X value of where the green text will appear.
  • y [number]: Y value of where the green text will appear.
  • showOverlay [boolean]: Whether to show the overlay or not. (default false)
  • budgetItem [draft]: *I have no idea what this is*
showOverlay example:
showOverlay.gif

Green text example:
20200618_202715.jpg

I hope this answered your question @rjroldan1

:jb: Bear Out!
Great😍 thanks

But what if i want only to show overlay than to place in green text should i leave x and y empty?

User avatar
Lobby
Developer
Reactions:
Posts: 3705
Joined: 26 Oct 2008, 12:34
Plugins: Showcase Store
Version: Beta

Platform

Re: Lua chatbox

#4

Post by Lobby »

Replace them with nil, that's Lua's name for "nothing", "null" or "not defined".

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#5

Post by Hadestia »

Can i ask on how lua script(attached to a building via call lua fun) is only and about to enable if some events occurs with that building ?

Im clueless

User avatar
ian`
Supporter
Reactions:
Posts: 117
Joined: 04 Apr 2020, 17:36
Location: Indonesien
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#6

Post by ian` »

rjroldan1 wrote:
19 Jun 2020, 03:10
Can i ask on how lua script(attached to a building via call lua fun) is only
viewtopic.php?f=81&t=10561
and about to enable if some events occurs with that building ?

Im clueless
What do you mean "script:event"?
I think you can use "and"

Code: Select all

// Script event :
function script:event(x,y,level,event)
if Script.EVENT_UPGRADE and City.countBuildings(draft) >= 1 then
actions
end
end

// with 2 conditions:
function script:update()
if City.countBuildings(draft) == 1 and City.isOnline() then
actions
end
end
Last edited by ian` on 19 Jun 2020, 08:06, edited 2 times in total.

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#7

Post by Hadestia »

Thanks

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#8

Post by Bearbear76 »

The usage of script:event() is bit wrong so I'll explain it.

Lua (example_script.lua):

Code: Select all

function script:event(x, y, level, event)
  if event == Script.EVENT_PLACED then
    <...>
  end
end
First you need to use event == [event] :P

Also you can only use events to drafts that are attached to the script.

JSON (example.json):

Code: Select all

[
  {
    "id": "$example_res00",
    ...
    "script": "example_script.lua"  // attaches script to draft
  },
  {
    "id": "$example_res01",
    ... 
    // doesn't have a script
  }
]
So now the event will only be called for $example_res00 and not for $example_res01.
So when $example_res00 is placed script:event(x, y, level, event) will return EVENT_PLACED for event. but nil when $example_res01 is placed.

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#9

Post by Hadestia »

Ølsken wrote:
19 Jun 2020, 09:55
The usage of script:event() is bit wrong so I'll explain it.

Lua (example_script.lua):

Code: Select all

function script:event(x, y, level, event)
  if event == Script.EVENT_PLACED then
    <...>
  end
end
First you need to use event == [event] :P

Also you can only use events to drafts that are attached to the script.

JSON (example.json):

Code: Select all

[
  {
    "id": "$example_res00",
    ...
    "script": "example_script.lua"  // attaches script to draft
  },
  {
    "id": "$example_res01",
    ... 
    // doesn't have a script
  }
]
So now the event will only be called for $example_res00 and not for $example_res01.
So when $example_res00 is placed script:event(x, y, level, event) will return EVENT_PLACED for event. but nil when $example_res01 is placed.
Oh, I thought script are separated with Plugins:)
Btw thanks this really answered my question :)

User avatar
ian`
Supporter
Reactions:
Posts: 117
Joined: 04 Apr 2020, 17:36
Location: Indonesien
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#10

Post by ian` »

Ølsken wrote:
19 Jun 2020, 09:55
The usage of script:event() is bit wrong so I'll explain it.

Lua (example_script.lua):

Code: Select all

function script:event(x, y, level, event)
  if event == Script.EVENT_PLACED then
    <...>
  end
end
First you need to use event == [event] :P
I see on your script (image) in luacheck linting post viewtopic.php?f=115&t=11437 and there are just Script.EVENT_PLACED without event. :bt

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#11

Post by Bearbear76 »

distian wrote:
19 Jun 2020, 10:08
Ølsken wrote:
19 Jun 2020, 09:55
The usage of script:event() is bit wrong so I'll explain it.

Lua (example_script.lua):

Code: Select all

function script:event(x, y, level, event)
  if event == Script.EVENT_PLACED then
    <...>
  end
end
First you need to use event == [event] :P
I see on your script (image) in luacheck linting post viewtopic.php?f=115&t=11437 and there are just Script.EVENT_PLACED without event. :bt
Sorry about that it should be event == [event] :P

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#12

Post by Hadestia »

Is this works in theory ?
Screenshot_20200619-165330.png

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#13

Post by Bearbear76 »

rjroldan1 wrote:
19 Jun 2020, 10:56
Is this works in theory ?Screenshot_20200619-165330.png
<!> match1 = string.match(..cname, "^Aos") <-- "..cname" would give you an error, you don't need the ".." part

I would also recommend using ":" instead of "."

Code: Select all

cname:match("^Aos")

-- Is the same as

string.match(cname, "^Aos")
Basically the ":" will put cname in the first parameter.
So Lua puts cname inside the parameter for you.
It just looks cleaner this way. ;)

Code: Select all

if cname:match("^Aos") then
  Debug.toast("working")
end
This would make it a bit more cleaner too.
Also this will work if the name of your city is "Aos" but it would also work if the name is "Aoset". (Unless this is your intention)
As it matches any string that starts with "Aos" since you didn't specify the ending.
If you only wanted to match "Aos" use "^Aos$" ($ = ending of string)

matching is a cool feature but it's a bit difficult the first time you use it (at least for me). :lol:

Also wouldn't recommend using Debug.toast in script:update if your Android version is 10+
Since the app won't close until all the toasts are displayed (which takes a long long time). so you can exit the game and still get toast messages as it's running in the background.
In these cases you would have to force stop TheoTown to resolve the issue.

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#14

Post by Hadestia »

What's the function of "." ?

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#15

Post by Bearbear76 »

rjroldan1 wrote:
19 Jun 2020, 11:25
What's the function of "." ?
"." would mean find the following inside the table

for example:

Code: Select all

local foo = {"Olsken", x = 12, hello = function(t) print("x value of this table is: "..t.x) end}
Inside foo we have 3 elements, a string a value and a function, we can access the
values which have a key: (key = value)

Code: Select all

print(foo[1])
--- Outputs: Olsken [string]

print(foo.x)
--- Outputs: 12 [number]

print(foo["x"])  	<--- This does the same thing as "." would
--- Outputs: 12 [number]

print(foo.hello())
--- Error: attempt to index a nil value (local 't')

print(foo:hello())
--- Output: x value of this table is: 12 [string]
In the second print we forgot to pass a table inside the parameter so it gave us an error saying "Where is the table?"
But why did it work in the third print?
It's because Lua automatically puts foo (itself) inside the parameter to make print(foo.hello(foo))

Lua does this:
foo:hello() ---> foo.hello(foo)

This is really hard to understand at first. trust me I was confused too :lol:

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5730
Joined: 10 Feb 2017, 14:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

#16

Post by Bearbear76 »

Lua metatables:

Lua tables have more functionality than just storing values. they can also be modified via metatables.

For example: let's say you want to count the number of elements in a table.

Code: Select all

local foo = {1, 2, 3, 4, 5}
print(#foo)

--- Outputs: 5
You can get the number of elements using the length operator (#).
But what if you stored key, value pairs inside a table.

Code: Select all

local foo = {x = 12, y = 12}
print(#foo)

--- Outputs: 0
The length operator doesn't see/count key values pairs for some reason so you would have to manually make
a function to get the table length.

Code: Select all

function table_size(t)
  local count = 0
  for key, value in pairs(t) do
    count = count + 1
  end
  return count
end

local foo = {x = 12, y = 12}
print(table_size(foo))

--- Outputs: 2
Great! now it counts the numbers of elements properly.
But If you want to use the length operator instead of table_size() you can use metatables.

Metatables are just tables that contain functions for some specific operation.
So in this case we will use the __len metamethod which is for the length operator.

first we have to create a new metatable which is just a normal table with some special keys (metamethods)

Code: Select all

local mt = {
  __len = function()
    -- This function will be called when the length operator is used
  end
}
Now lets add the previous code to count the number of elements in table and insert it inside the function.

Code: Select all

local mt = {
  __len = function(t)
    local count = 0
    for key, value in pairs(t) do
      count = count + 1
    end
    return count
  end
}
Note: the t inside the function parameter is the table itself (in this case: foo)

Now lets give this functionality to a table using the setmetatable() function.

Code: Select all

setmetatable(foo, mt)
Everything should be set let's use the length operator now!

Code: Select all

print(#foo)

--- Outputs: 2
I found this very helpful went trying to use metatables: https://developer.roblox.com/en-us/articles/Metatables
(it explains it better than I can :))

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#17

Post by Hadestia »

Need help @Ølsken

While coding an animation (via lua) that only occurs when City ranks is 0 and I'd encountered this:
Screenshot_20200621-160143.png
Im using realtime editing btw here's the script
IMG_20200621_160303.jpg
IMG_20200621_160303.jpg (23.89 KiB) Viewed 14190 times

User avatar
ian`
Supporter
Reactions:
Posts: 117
Joined: 04 Apr 2020, 17:36
Location: Indonesien
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

#18

Post by ian` »

rjroldan1 wrote:
21 Jun 2020, 10:12
Need help @Ølsken

While coding an animation (via lua) that only occurs when City ranks is 0 and I'd encountered this:
Screenshot_20200621-160143.png
Im using realtime editing btw here's the script
IMG_20200621_160303.jpg
Try this:

Code: Select all

local flag1 = Draft.getDraft('$Aosflag4')

function script:update()
local rank = City.getRank()
end

function script:draw(x,y,level)
... Drawing stuff ...
Drawing.drawImage('flag1')

if rank >= 1 then
Drawing.reset()
end
end

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#19

Post by Hadestia »

I will try

User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 727
Joined: 17 Jul 2017, 16:16
Location: Philippines
Plugins: Showcase Store
Contact:

Plugin Creator

Platform

Re: Lua chatbox

#20

Post by Hadestia »

Still not work @Ølsken

Post Reply Previous topicNext topic

Return to “Lua Scripting”