Page 1 of 1

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

Posted: 24 Jun 2020, 16:41
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.

Re: script:click() doesn't work

Posted: 24 Jun 2020, 18:29
by CommanderABab
Could you post or send the contents of Oxadg_cat.json?

Re: script:click() doesn't work

Posted: 24 Jun 2020, 18:55
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"
}

Re: script:click() doesn't work

Posted: 24 Jun 2020, 19:09
by CommanderABab
Needs [ at the start and ] at the end if that is the complete json. :)

Re: script:click() doesn't work

Posted: 24 Jun 2020, 19:35
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

Re: script:click() doesn't work

Posted: 24 Jun 2020, 19:51
by CommanderABab
OK. What does the lua function look like? :)

Re: script:click() doesn't work

Posted: 24 Jun 2020, 20:19
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

Re: script:click() doesn't work

Posted: 24 Jun 2020, 20:34
by CommanderABab
It might be expecting the id of a building and not coordinates.

Re: script:click() doesn't work

Posted: 24 Jun 2020, 21:27
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

Re: script:click() doesn't work

Posted: 25 Jun 2020, 00:28
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

Re: script:click() doesn't work

Posted: 25 Jun 2020, 01:05
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

Re: script:click() doesn't work

Posted: 25 Jun 2020, 01:21
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.