Page 1 of 10
Lua chatbox
Posted: 18 Jun 2020, 12:22
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!
Re: Lua chatbox
Posted: 18 Jun 2020, 13:41
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 (1.11 MiB) Viewed 16196 times
Green text example:
- 20200618_202715.jpg (26.27 KiB) Viewed 16196 times
I hope this answered your question
@rjroldan1
Bear Out!
Re: Lua chatbox
Posted: 18 Jun 2020, 16:31
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
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?
Re: Lua chatbox
Posted: 18 Jun 2020, 17:38
by Lobby
Replace them with nil, that's Lua's name for "nothing", "null" or "not defined".
Re: Lua chatbox
Posted: 19 Jun 2020, 03:10
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
Re: Lua chatbox
Posted: 19 Jun 2020, 06:09
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
Re: Lua chatbox
Posted: 19 Jun 2020, 06:28
by Hadestia
Thanks
Re: Lua chatbox
Posted: 19 Jun 2020, 09:55
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]
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.
Re: Lua chatbox
Posted: 19 Jun 2020, 09:59
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]
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
Re: Lua chatbox
Posted: 19 Jun 2020, 10:08
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]
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.
Re: Lua chatbox
Posted: 19 Jun 2020, 10:15
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]
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.
Sorry about that it should be
event == [event]
Re: Lua chatbox
Posted: 19 Jun 2020, 10:56
by Hadestia
Is this works in theory ?
Re: Lua chatbox
Posted: 19 Jun 2020, 11:07
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).
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.
Re: Lua chatbox
Posted: 19 Jun 2020, 11:25
by Hadestia
What's the function of "." ?
Re: Lua chatbox
Posted: 19 Jun 2020, 11:35
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
Re: Lua chatbox
Posted: 19 Jun 2020, 11:46
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.
Everything should be set let's use the length operator now!
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
)
Re: Lua chatbox
Posted: 21 Jun 2020, 10:12
by Hadestia
Need help
@Ølsken
While coding an animation (via lua) that only occurs when City ranks is 0 and I'd encountered this:
Im using realtime editing btw here's the script
- IMG_20200621_160303.jpg (23.89 KiB) Viewed 16065 times
Re: Lua chatbox
Posted: 21 Jun 2020, 10:26
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
Re: Lua chatbox
Posted: 21 Jun 2020, 11:30
by Hadestia
I will try
Re: Lua chatbox
Posted: 21 Jun 2020, 11:46
by Hadestia