[Resolved]script:click() doesn't work

Plugin specific problems will be solved here.

Moderator: Plugin Moderators

User avatar
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 20:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

[Resolved]script:click() doesn't work

#1

Post by Temgamer »

I've been writing some simple script, which will activate, when player tap on a building on some coordinates.

Code: Select all

function script:click(10,10)
  Debug.toast("click test")
end
But when I'm trying to start the game it returns this error:
Screenshot_20200624-173751_TheoTown.jpg
Where is the problem? There's nothing said about <name> in "script:click()" documentation.
Last edited by Temgamer on 25 Jun 2020, 01:06, edited 1 time in total.

User avatar
CommanderABab
AB
Reactions:
Posts: 11102
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: script:click() doesn't work

#2

Post by CommanderABab »

Could you post or send the contents of Oxadg_cat.json?

User avatar
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 20:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

Re: script:click() doesn't work

#3

Post by Temgamer »

Yes, there is the building, which uses that script:

Code: Select all

{
"id":"$radioo2",
 "type":"decoration",
 "category":"$0xadg_cat",
 "frames":[{"bmp":"radio.png"}],
 "width":1,
 "height":1,
 "title":"lua test",
  "text":"hope this works",
  "script":"soundplay.lua"
}

User avatar
CommanderABab
AB
Reactions:
Posts: 11102
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: script:click() doesn't work

#4

Post by CommanderABab »

Needs [ at the start and ] at the end if that is the complete json. :)

User avatar
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 20:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

Re: script:click() doesn't work

#5

Post by Temgamer »

CommanderABab wrote:
24 Jun 2020, 19:09
Needs [ at the start and ] at the end if that is the complete json. :)
It's a part of bigger json file, only it uses that script. Also, 6th line in .lua file is where the "script:click()" is located

User avatar
CommanderABab
AB
Reactions:
Posts: 11102
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: script:click() doesn't work

#6

Post by CommanderABab »

OK. What does the lua function look like? :)

User avatar
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 20:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

Re: script:click() doesn't work

#7

Post by Temgamer »

CommanderABab wrote:
24 Jun 2020, 19:51
OK. What does the lua function look like? :)
It should show me a toast, when I'm tapping at something at x:10 and y:10

Code: Select all

function script:click(10,10)
  Debug.toast("click test")
end

User avatar
CommanderABab
AB
Reactions:
Posts: 11102
Joined: 07 Jun 2016, 21:12
Plugins: Showcase Store
Version: Beta

Plugin Creator

Platform

Re: script:click() doesn't work

#8

Post by CommanderABab »

It might be expecting the id of a building and not coordinates.

User avatar
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 20:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

Re: script:click() doesn't work

#9

Post by Temgamer »

CommanderABab wrote:
24 Jun 2020, 20:34
It might be expecting the id of a building and not coordinates.
I've tried entering id of that building, also a name of it, for some reason, but it still doesn't work.
That's description of click() on the Lua wiki, in parameters there is only numbers
Screenshot_20200624-221554_Chrome.jpg
Also, I checked script:tap() and it returns the same error. Maybe they're unsupported yet

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

Plugin Creator

Platform

Re: script:click() doesn't work

#10

Post by Bearbear76 »

Ah you're getting a little bit confused with how TheoTown uses functions.
You don't put values in script:click(x, y, level) instead you put in variables like x, y, level (a variable is a word that can have a mutateable (changeable) value). The general rule would be when you declare functions (using the function keyword) you use variables in the parameters (round brackets).

Code: Select all

--- Declaring a function
function sum(a, b) -- use variables in here
  return a + b
end
But when calling (using) a function you use normal values or variables that contain values.

Code: Select all

local number = 12 -- declare variable

--- Calling a function
foo(50, number) -- Use values or variables that were declared

--> res0: number = 62
-- Output of foo(50, number) is 62
The same logic applies when using script:tap(tileX, tileY, x, y) (using tap as it's easier to explain).
Let's say you had this code:

Code: Select all

function script:tap(tileX, tileY, x, y)
  Debug.toast("You tapped: "..x..","..y)
end
What this would do is that whenever TheoTown detects that the user tapped the screen it would call this function (hence the name "tap"). Then TheoTown gives you information about the tap. That's what's going to be inside the parameters.
Let's say if you tapped (20, 40) on the screen TheoTown will put those values inside the x, y of the parameter so that you can use it.

This is probably hard to understand by just reading. I think it would be easier to understand by experiments so try this code out and try to figure out how it works.

Code: Select all

function script:tap(tileX, tileY, x, y)
  local name = "TheoTown"
  Debug.toast(name.." detected that you tapped: ("..tileX..","..tileY..")")
end

User avatar
Temgamer
Villager
Reactions:
Posts: 12
Joined: 16 May 2017, 20:26
Location: Saint-Petersburg, Russia
Plugins: Showcase Store

Re: script:click() doesn't work

#11

Post by Temgamer »

Ølsken wrote:
25 Jun 2020, 00:28
Ah you're getting a little bit confused with how TheoTown uses functions.
You don't put values in script:click(x, y, level) instead you put in variables like x, y, level (a variable is a word that can have a mutateable (changeable) value). The general rule would be when you declare functions (using the function keyword) you use variables in the parameters (round brackets).

Code: Select all

--- Declaring a function
function sum(a, b) -- use variables in here
  return a + b
end
But when calling (using) a function you use normal values or variables that contain values.

Code: Select all

local number = 12 -- declare variable

--- Calling a function
foo(50, number) -- Use values or variables that were declared

--> res0: number = 62
-- Output of foo(50, number) is 62
The same logic applies when using script:tap(tileX, tileY, x, y) (using tap as it's easier to explain).
Let's say you had this code:

Code: Select all

function script:tap(tileX, tileY, x, y)
  Debug.toast("You tapped: "..x..","..y)
end
What this would do is that whenever TheoTown detects that the user tapped the screen it would call this function (hence the name "tap"). Then TheoTown gives you information about the tap. That's what's going to be inside the parameters.
Let's say if you tapped (20, 40) on the screen TheoTown will put those values inside the x, y of the parameter so that you can use it.

This is probably hard to understand by just reading. I think it would be easier to understand by experiments so try this code out and try to figure out how it works.

Code: Select all

function script:tap(tileX, tileY, x, y)
  local name = "TheoTown"
  Debug.toast(name.." detected that you tapped: ("..tileX..","..tileY..")")
end
Thank you very much, I forgot about that function parameters in Lua

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

Plugin Creator

Platform

Re: script:click() doesn't work

#12

Post by ian` »

Code: Select all

script:click(x,y,level)  -- this is will detect attached building positions every where on your city.

script:click(10,10,level) -- this is will detect only on x:10 y:10 coordinate on your city, but level will detect to attached building or road position.

script:click(10,y,level) or script:click(x,10,level) -- this is will be like line, just detect 10 on x or y only, but will detect attached building positions on unfilled x or y.

script:click(x+10,y+10,level) -- this is will detect attached building positions + 10. Your building will be x:0 and y:0 then when you click x:10 and y:10, the script will be work. If your building position in your city is x:50 and y:70, so the script will be work on x:60 and y:80. Other calculation will be work, like - * or / depend on where you need coordinates you want to script will work.
You can use lua real time editing on viewtopic.php?f=115&t=10834 to see what your script errors or when you want to change any stuff on your lua script without restart the games. It's work on desktop or mobile.

Post Reply Previous topicNext topic

Return to “Problems”