Lua chatbox

The Lua scripting language allows you to give your plugin more advanced features.
Previous topicNext topic
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Lua chatbox

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!
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

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
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
ian`
Metropolitan
Reactions:
Posts: 117
Joined: 04 Apr 2020, 15:36
Location: 日本
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

Post by ian` »

rjroldan1 wrote: 19 Jun 2020, 01: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, 06:06, edited 2 times in total.
:bc
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Thanks
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

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.
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Ølsken wrote: 19 Jun 2020, 07: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 :)
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
ian`
Metropolitan
Reactions:
Posts: 117
Joined: 04 Apr 2020, 15:36
Location: 日本
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

Post by ian` »

Ølsken wrote: 19 Jun 2020, 07: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
:bc
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Bearbear76 »

distian wrote: 19 Jun 2020, 08:08
Ølsken wrote: 19 Jun 2020, 07: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
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Is this works in theory ?
Screenshot_20200619-165330.png
You do not have the required permissions to view the files attached to this post.
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Bearbear76 »

rjroldan1 wrote: 19 Jun 2020, 08: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.
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

What's the function of "." ?
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Bearbear76 »

rjroldan1 wrote: 19 Jun 2020, 09: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:
Just my opinion
retired
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

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 :))
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

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
You do not have the required permissions to view the files attached to this post.
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
ian`
Metropolitan
Reactions:
Posts: 117
Joined: 04 Apr 2020, 15:36
Location: 日本
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: Lua chatbox

Post by ian` »

rjroldan1 wrote: 21 Jun 2020, 08: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
:bc
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

I will try
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Still not work @Ølsken
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
User avatar
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3759
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Lua chatbox

Post by Lobby »

Ternary operator
Many languages support a feature called ternary operator (although that's technically a broader term) which allows you to simplify if condtions that are only used to decide which of two values to return/assign.

Consider the following code:

Code: Select all

local x = 42
local y
if x > 7 then
  y = 'greater than seven'
else
  y = 'no'
end
All it does is to fill the variable y with 'greater than seven' or 'no' dependent on whether x is greater than 7.

In other languages the assignment of y could be written like this:

Code: Select all

y = x > 7 ? "greater than seven" : "no"           -- C, Java, C#, ...
The operator itself consists out of the symbold ? and : and takes three values as input. That's why it's called ternary operator.


and and or
Now the thing is, that Lua does not have a dedicated ternary operator. However, we can mimic its behavior by using the and and or binary comparison operators. In Lua these behave a bit differently than in other language in terms of what they return. To understand why that is we have to take a closer look at what Lua "considers to be true":

Code: Select all

if x then print('x is true') end
Lua will consider "x to be true" whenever x is not nil and not 0. In other words, there are no dedicated values for true and false in Lua, just "nil or 0" (false) and "not nil and not 0" (true).

For that reason an expression like

Code: Select all

a and b
can just return a if it considers a to be "false" (because then a and b has to be false), and b otherwise (because then it only depends on the truthness of b whether a and b is true).

Similarly

Code: Select all

a or b
returns a if a is true (since then a or b is true, too), and b otherwise (because then a or b only depends on b).

Bringing it together
Now we can rewrite the Lua code from above as

Code: Select all

local y = x > 7 and 'great than seven' or 'no'
and in this case it would actually behave exactly the same as if we'd use the if condition.


Expressions
I used an italic font for values because what we're actually talking about here are expressions. The difference is that you can get a value out of an expression, but you don't have to. That makes an huge difference when your expression e.g. contains functions that can modify state somewhere else. If the value of an expression is not needed, it will not be executed. That can be both, useful for optimization but also for error prevention.

An example:

Code: Select all

local function foo()
  return 42
end
local y

if foo then
  y = foo()
else
  y = 0
end
Let's assume that we cannot be sure about whether foo is a function or nil. Therefore we have to check if it is not nil before we can call it. However, since expressions are only evaluated if needed (lazy evaluation; short circuit evaluation in case of and and or operators to be more precise) we can write:

Code: Select all

local y = foo and foo() or 0
That can also be useful for the TheoTown library where you cannot always be sure if e.g. the City library does exist:

Code: Select all

local income = City and City.getIncome() or 0

Default function arguments
You often want to have some sort of "default" values for function parameters that were not provided by the caller (and are therefore nil). You can do that conveniently by using the behavior of the or operator:

Code: Select all

function foo(str, tbl, num)
  str = str or 'I was not set'    -- Use 'I was not set' as default value for str
  tbl = tbl or {}                 -- Create a table if none was given
  num = num == nil and 42 or num  -- Use 42 if num was not provided; note that we have to be careful if we want num to be able to be 0
  -- Do something with str, tbl and num...
end

-- Can be called in different ways:
foo()
foo('hi')
foo('hi', { 1, 2, 3 })
foo('hi', { 1, 2, 3 }, 7)

 ! Message from: Lobby
This replacement works well in a lot of cases and can help to make code more concise. However, be cautious when using it with values that can be 0 because then you have a non nil value that still is treated as false. :teach
User avatar
Bearbear76
Inhabitant of a Multiverse
Reactions:
Posts: 5701
Joined: 10 Feb 2017, 12:53
Location: L2 cache
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Bearbear76 »

rjroldan1 wrote: 21 Jun 2020, 09:46 Still not work @Ølsken
Is the flag an animated or static?
If static you forgotgetFrame([number]).

So

Code: Select all

flag1 = Draft.getDraft("Aosflag4"):getFrame(1)
-- or
Drawing.drawImage(flag1:getFrame(1), x, y)
Just my opinion
retired
User avatar
Hadestia
Inhabitant of a Megalopolis
Reactions:
Posts: 722
Joined: 17 Jul 2017, 14:16
Location: Philippines
Plugins: Showcase Store

Plugin Creator

Platform

Re: Lua chatbox

Post by Hadestia »

Ølsken wrote: 21 Jun 2020, 10:02
rjroldan1 wrote: 21 Jun 2020, 09:46 Still not work @Ølsken
Is the flag an animation or static?
If static you forgotgetFrame([number]).

So

Code: Select all

flag1 = Draft.getDraft("Aosflag4"):getFrame(1)
-- or
Drawing.drawImage(flag1:getFrame(1), x, y)

An animation it works well if i exclude the rank condition but i dont want it to be like that
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
Previous topicNext topic