Lua chatbox

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

Moderator: Plugin Moderators

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

Platform

Re: Lua chatbox

#21

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
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#22

Post by Bearbear76 »

rjroldan1 wrote:
Sun Jun 21, 2020 11: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)
ImageJust my opinion

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#23

Post by rjroldan1 »

Ølsken wrote:
Sun Jun 21, 2020 12:02
rjroldan1 wrote:
Sun Jun 21, 2020 11: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
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#24

Post by Bearbear76 »

rjroldan1 wrote:
Sun Jun 21, 2020 12:05
Ølsken wrote:
Sun Jun 21, 2020 12:02
rjroldan1 wrote:
Sun Jun 21, 2020 11: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
Ah I see City.getRank() returns 2 values (https://doc.theotown.com/modules/City.html#getRank)

This is why it doesn't work rank is a draft

Code: Select all

rank = City.getRank() -- returns first value

--> rank: draft = [draft]
Try out one of these:

Code: Select all

_, rank = City.getRank() -- returns draft for "_" and number for "rank"

--> _: draft = [draft]
--> rank: number = [number]

Code: Select all

rank = select(2, City.getRank()) -- returns second value

--> rank: number = [number]
ImageJust my opinion

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

Platform

Re: Lua chatbox

#25

Post by Lobby »

@rjroldan1

I'd say more like that:

Code: Select all

local _,rank

function script:update()
  _,rank = City.getRank()
end
  • You have to define rank as local outside of script:update() if you want to use it in another function (it's nil there otherwise)
  • City.getRank() does return the rank index as second return value, therefore the strange variable _ here
=^._.^= ∫

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#26

Post by rjroldan1 »

Lobby wrote:
Sun Jun 21, 2020 12:13
@rjroldan1

I'd say more like that:

Code: Select all

local _,rank

function script:update()
  _,rank = City.getRank()
end
  • You have to define rank as local outside of script:update() if you want to use it in another function (it's nil there otherwise)
  • City.getRank() does return the rank index as second return value, therefore the strange variable _ here
This really answers my questions
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#27

Post by rjroldan1 »

So if i don't include "_," thing, my variables turns to global variable?
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

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

#28

Post by ian` »

Wait, what the rank and new lua functions is available on older version than 1884? :bt
Last edited by ian` on Sun Jun 21, 2020 12:53, edited 1 time in total.
:fire

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#29

Post by Bearbear76 »

rjroldan1 wrote:
Sun Jun 21, 2020 12:40
So if i don't include "_," thing, my variables turns to global variable?
Anything that doesn't have local in front of it is a global value in Lua.
_ is just a variable name like rank since Lua allows alphanumeric and _ for variable names (cannot start with a number though)
_ in Lua is a common placeholder variable. (usually a variable you won't use)
You can use whatever as long as it sounds like it's going to not be used like:
  • ghost
  • blackhole
  • temp
  • junk
If you're working on the code by yourself use whatever you want get creative (as long as you can tell what it is).
But, make you won't go "hmm what is stardust??" after revisiting your code after a week or so. :lol:
If you're working with a group using _ might be a better idea.

Code: Select all

magic_number = 12 -- ok
_LUARESERVED = "dont write like this Lua uses this" -- ok
base64 = "QmVhcmJlYXI2NQ==" -- ok
8bit = "00001100" -- error: malformed number near '8b'
$name = "Olsken" --error: unexpected symbol near '$'
So any variables after local will be declared as a local value

Code: Select all

local x, y, z = 1, 2, 3

--> local x: number = 1
--> local y: number = 2
--> local z: number = 3
Also try to avoid global values as much as possible!
ImageJust my opinion

User avatar
Bearbear76
Former Bearbear65
Reactions:
Posts: 5296
Joined: Fri Feb 10, 2017 14:53
Plugins: Show
Version: Beta

Platform

Plugin Creator

Re: Lua chatbox

#30

Post by Bearbear76 »

distian wrote:
Sun Jun 21, 2020 12:49
Wait, what the rank and new lua functions is available on older version than 1884? :bt
yes, If I understood correctly.
ImageJust my opinion

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

Platform

Re: Lua chatbox

#31

Post by Lobby »

No, the new Lua functions (like City.getIncome()) are only available with version 1.8.84 and newer. However, City.getRank() does exists for a while, now :)
=^._.^= ∫

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#32

Post by rjroldan1 »

Ølsken wrote:
Sun Jun 21, 2020 12:52
rjroldan1 wrote:
Sun Jun 21, 2020 12:40
So if i don't include "_," thing, my variables turns to global variable?
Anything that doesn't have local in front of it is a global value in Lua.
_ is just a variable name like rank since Lua allows alphanumeric and _ for variable names (cannot start with a number though)
_ in Lua is a common placeholder variable. (usually a variable you won't use)
You can use whatever as long as it sounds like it's going to not be used like:
  • ghost
  • blackhole
  • temp
  • junk
If you're working on the code by yourself use whatever you want get creative (as long as you can tell what it is).
But, make you won't go "hmm what is stardust??" after revisiting your code after a week or so. :lol:
If you're working with a group using _ might be a better idea.

Code: Select all

magic_number = 12 -- ok
_LUARESERVED = "dont write like this Lua uses this" -- ok
base64 = "QmVhcmJlYXI2NQ==" -- ok
8bit = "00001100" -- error: malformed number near '8b'
$name = "Olsken" --error: unexpected symbol near '$'
So any variables after local will be declared as a local value

Code: Select all

local x, y, z = 1, 2, 3

--> local x: number = 1
--> local y: number = 2
--> local z: number = 3
Also try to avoid global values as much as possible!
Its basically like java custom variable rule
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#33

Post by rjroldan1 »

Anyway i add new function to call before it initiate the condition , hope this work
Screenshot_20200621-204754.png
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

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

Platform

Re: Lua chatbox

#34

Post by Lobby »

Code: Select all

...
local _, rank, var2

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

local var = function()
  var2 = rank * 1
end
...
I you define a variable as local in a function it won't be accessible from outside of that function :teach
=^._.^= ∫

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#35

Post by rjroldan1 »

Lobby wrote:
Sun Jun 21, 2020 14:54

Code: Select all

...
local _, rank

function script:update()
  _, rank = City.getRank()
end
...
:teach

I try this but it gives me an issue that like "expected 'then at line (where the condition are) " even it has already
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

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

Platform

Re: Lua chatbox

#36

Post by Lobby »

Yes, because your var2 was also declared as local within a function :)
=^._.^= ∫

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#37

Post by rjroldan1 »

Lobby wrote:
Sun Jun 21, 2020 14:57
Yes, because your var2 was also declared as local within a function :)

Im clueless what should i do :calc
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

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

Platform

Re: Lua chatbox

#38

Post by Lobby »

Please upload your whole code as text so I can edit it :)
=^._.^= ∫

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#39

Post by rjroldan1 »

Lobby wrote:
Sun Jun 21, 2020 15:02
Please upload your whole code as text so I can edit it :)
Together with json?
Last edited by rjroldan1 on Sun Jun 21, 2020 15:07, edited 1 time in total.
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

User avatar
rjroldan1
Inhabitant of a Megalopolis
Reactions:
Posts: 625
Joined: Mon Jul 17, 2017 16:16
Location: Antipolo, Philippines
Plugins: Show
Contact:

Platform

Plugin Creator

Re: Lua chatbox

#40

Post by rjroldan1 »

Code: Select all

local flag1 = Draft.getDraft("AoSflag1") 
local flag2 = Draft.getDraft("AoSflag2") 
local flag3 = Draft.getDraft("AoSflag3") 
local flag4 = Draft.getDraft("AoSflag4") 

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

local var = function ()
 local var2 = rank * 1
end

function script:draw(tileX, tileY) 
Drawing.setTile(tileX, tileY, 17, -40) 
Drawing.setColor(255,255,255) 
Drawing.setAlpha(1.0) 

var()
 if rank1 == 0 then 
   Drawing.drawImage(flag1) 
 elseif var2 == 1 then 
   Drawing.drawImage(flag2)
 elseif var2 == 2 then
   Drawing.drawImage(flag3)
 elseif var2 == 3 then
  Drawing.drawImage(flag4)
 end 
Drawing.reset() 
end 
 

Here lobby sorry i think im lacking of IQ maybe it because of sodium foods
T.P.C.

Theotown Philippine Corporation
╔═╗─────────────╔╗────────
║╬║─╔╗╔╦╗╔═╗╔╗─╔╝║╔═╗─╔═╦╗
║╗╣─╠╣║╔╝║╬║║╚╗║╬║║╬╚╗║║║║
╚╩╝╔╝║╚╝─╚═╝╚═╝╚═╝╚══╝╚╩═╝
───╚═╝────────────────────

Post Reply Previous topicNext topic

Return to “Lua Scripting”