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

Re: Lua chatbox

Post by Bearbear76 »

rjroldan1 wrote: 21 Jun 2020, 10:05
Ø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
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]
Just my opinion
retired
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 »

@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
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 »

Lobby wrote: 21 Jun 2020, 10: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
 ! 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 »

So if i don't include "_," thing, my variables turns to global variable?
 ! 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` »

Wait, what the rank and new lua functions is available on older version than 1884? :bt
Last edited by ian` on 21 Jun 2020, 10:53, edited 1 time in total.
: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 »

rjroldan1 wrote: 21 Jun 2020, 10: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!
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 »

distian wrote: 21 Jun 2020, 10:49 Wait, what the rank and new lua functions is available on older version than 1884? :bt
yes, If I understood correctly.
Just my opinion
retired
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 »

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
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:52
rjroldan1 wrote: 21 Jun 2020, 10: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
 ! 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 »

Anyway i add new function to call before it initiate the condition , hope this work
Screenshot_20200621-204754.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
Lobby
Inhabitant of a Supercluster
Reactions:
Posts: 3759
Joined: 26 Oct 2008, 10:34
Plugins: Showcase Store

Re: Lua chatbox

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
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 »

Lobby wrote: 21 Jun 2020, 12: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
 ! 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 »

Yes, because your var2 was also declared as local within a function :)
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 »

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

Im clueless what should i do :calc
 ! 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 »

Please upload your whole code as text so I can edit it :)
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 »

Lobby wrote: 21 Jun 2020, 13:02 Please upload your whole code as text so I can edit it :)
Together with json?
Last edited by Hadestia on 21 Jun 2020, 13:07, edited 1 time in total.
 ! 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 »

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
 ! 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 »

Lobby wrote: 21 Jun 2020, 12:54

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

Oh ok i get it this is just like getting the 2nd return info. To store at second variable
 ! 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 »

Hmm a lot of misplaced locals.

Local variables are local to their block no one can access them outside.
These are the basic blocks:

Code: Select all

if condition then
  -- If block
end

for i = 1, 100 do
  -- For block
end

while condition do
  -- While block
end

function foo()
  -- Function block
end
For example:

Code: Select all

local a = 12

if condition then
  local b = 13
end

local c = 20
function foo()
  c = 50
end

--> local a: number = 12
--> nil
--> local c: number = 50
As you can see the variable b cannot be accessed because it's only visible to it's own block (if statement).
But we can see c as it was declared outside of any block, though it was re-declared inside a block it still is local to the original declaration.
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 »

It works well now Thanks for your help also :)

Hope this conversation would help others too :)
 ! Message from: Hadestia
Visit my YouTube channel:
https://youtube.com/channel/UC5qG-r8y21036JZbGRClbdQ
Previous topicNext topic