Cannot disable sprite

Yo.
Im trying to disable a Sprite, but I can’t seem to get it to co-operate.
Just started Defold :stuck_out_tongue:
This is my code:

function init(self)

local TimeL = 0

end

function update(self, dt)

TimeL = TimeL + dt

if TimeL > 1 then
msg.post("/main/Map/Room1/light#sprite" , “disable”)
end
if
TimeL > 2 then
msg.post("/main/Map/Room1/light#sprite" , “enable”)
TimeL = 0
end

function on_input(self, action_id, action)
– Add input-handling code here
– Remove this function if not needed
end

end

1 Like

What is happening and do you get any messages in the console?

I don’t get any errors, and the sprites are simply enabled permanently

Try to print TimeL in update() and see what values it get.

Print also did not give me anything at all -.-
but i got this new error:

main/Map/Room1/light/light.script:12: in function <main/Map/Room1/light/light.script:9>
ERROR:SCRIPT: main/Map/Room1/light/light.script:12: attempt to index global ‘TimeL’ (a nil value)
stack traceback:

Yes. You declare TimeL as a local in init(). It won’t exist outside that function. If you want to store state in a game object instance you should set it in the self table:

function init(self)
    self.TimeL = 0
end
1 Like

Hi!

I’m here from the past to note something weird:

there’s a difference between the speech marks here. look at this

“disable”

and look at this

“/main/Map/Room1/light#sprite”.

The speech marks around disable cause an error. Isn’t that weird?

You mean the future? :slight_smile:

Not weird “” are special characters and not "" or '' it must be this forum software messing it up when copied, and user copied the special chars into a code block.

2 Likes