Rules:
- Must be about Lua, we have a general chatbox for other stuff.
- Be nice to each other!
viewtopic.php?f=81&t=10561rjroldan1 wrote: 19 Jun 2020, 01:10 Can i ask on how lua script(attached to a building via call lua fun) is only
What do you mean "script:event"?and about to enable if some events occurs with that building ?
Im clueless
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
endCode: Select all
function script:event(x, y, level, event)
if event == Script.EVENT_PLACED then
<...>
end
end
Code: Select all
[
{
"id": "$example_res00",
...
"script": "example_script.lua" // attaches script to draft
},
{
"id": "$example_res01",
...
// doesn't have a script
}
]
Oh, I thought script are separated with Plugins:)Ølsken wrote: 19 Jun 2020, 07:55 The usage of script:event() is bit wrong so I'll explain it.
Lua (example_script.lua):First you need to use event == [event]Code: Select all
function script:event(x, y, level, event) if event == Script.EVENT_PLACED then <...> end end
Also you can only use events to drafts that are attached to the script.
JSON (example.json):So now the event will only be called for $example_res00 and not for $example_res01.Code: Select all
[ { "id": "$example_res00", ... "script": "example_script.lua" // attaches script to draft }, { "id": "$example_res01", ... // doesn't have a script } ]
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.
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.Ølsken wrote: 19 Jun 2020, 07:55 The usage of script:event() is bit wrong so I'll explain it.
Lua (example_script.lua):First you need to use event == [event]Code: Select all
function script:event(x, y, level, event) if event == Script.EVENT_PLACED then <...> end end
Sorry about that it should be event == [event]distian wrote: 19 Jun 2020, 08:08I see on your script (image) in luacheck linting post viewtopic.php?f=115&t=11437 and there are just Script.EVENT_PLACED without event.Ølsken wrote: 19 Jun 2020, 07:55 The usage of script:event() is bit wrong so I'll explain it.
Lua (example_script.lua):First you need to use event == [event]Code: Select all
function script:event(x, y, level, event) if event == Script.EVENT_PLACED then <...> end end![]()
<!> match1 = string.match(..cname, "^Aos") <-- "..cname" would give you an error, you don't need the ".." part
Code: Select all
cname:match("^Aos")
-- Is the same as
string.match(cname, "^Aos")
Code: Select all
if cname:match("^Aos") then
Debug.toast("working")
end
"." would mean find the following inside the table
Code: Select all
local foo = {"Olsken", x = 12, hello = function(t) print("x value of this table is: "..t.x) end}
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]
Code: Select all
local foo = {1, 2, 3, 4, 5}
print(#foo)
--- Outputs: 5
Code: Select all
local foo = {x = 12, y = 12}
print(#foo)
--- Outputs: 0
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
Code: Select all
local mt = {
__len = function()
-- This function will be called when the length operator is used
end
}
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
}
Code: Select all
setmetatable(foo, mt)
Code: Select all
print(#foo)
--- Outputs: 2
Try this: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
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
endCode: Select all
local x = 42
local y
if x > 7 then
y = 'greater than seven'
else
y = 'no'
end
Code: Select all
y = x > 7 ? "greater than seven" : "no" -- C, Java, C#, ...
Code: Select all
if x then print('x is true') end
Code: Select all
a and b
Code: Select all
a or b
Code: Select all
local y = x > 7 and 'great than seven' or 'no'
Code: Select all
local function foo()
return 42
end
local y
if foo then
y = foo()
else
y = 0
end
Code: Select all
local y = foo and foo() or 0
Code: Select all
local income = City and City.getIncome() or 0
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)
Is the flag an animated or static?
Code: Select all
flag1 = Draft.getDraft("Aosflag4"):getFrame(1)
-- or
Drawing.drawImage(flag1:getFrame(1), x, y)
Ølsken wrote: 21 Jun 2020, 10:02Is the flag an animation or static?
If static you forgotgetFrame([number]).
SoCode: Select all
flag1 = Draft.getDraft("Aosflag4"):getFrame(1) -- or Drawing.drawImage(flag1:getFrame(1), x, y)