Self.boolean value in gui script doesn't change as expected (SOLVED)

I just declare a self.boolean variable in init function in gui script. I changed the self.boolean variable in a local function, it does change in the local function scope but when it goes to on_input function, it does not change as expected. Can someone explain this to me? So far as I know, I can’t use go.property in gui script code. If I want boolean or int value to change gui according to gui operation, what should I do in defold.

Can you post the script?

I am afraid not. That’s long code .
I can write simple code to describe it.

function init(self)
    self.enabled = false
end

local function change(self)
    if not self.enabled then
        self.enabled = true
    end
end

function on_message(self, message_id, message, sender)
    change(self)
end

function on_input(self, action_id, action)
    if action_id == hash("touch") and action.pressed then
        if self.enabled then
            --TODO
        end
    end
end

Well, it must be something in your big script then. I extended your script only slightly in order to actually test it:

function init(self)
	msg.post(".", "acquire_input_focus")
    self.enabled = false
end

local function change(self)
    if not self.enabled then
        self.enabled = true
    end
end

function on_message(self, message_id, message, sender)
    change(self)
end

function on_input(self, action_id, action)
    if action_id == hash("touch") and action.pressed then
        if self.enabled then
            print("yes")
		else
			print("no")
        end
    end
end

Output on click:

no

Then, I add a message to trigger the change function:

function init(self)
	msg.post(".", "acquire_input_focus")
    self.enabled = false
	msg.post(".", "message_to_trigger_change_function")
end

local function change(self)
    if not self.enabled then
        self.enabled = true
    end
end

function on_message(self, message_id, message, sender)
    change(self)
end

function on_input(self, action_id, action)
    if action_id == hash("touch") and action.pressed then
        if self.enabled then
            print("yes")
		else
			print("no")
        end
    end
end

Output:

yes
2 Likes

Use the debugger and set breakpoints or add a few print() statements. Check that on_message() is called.

1 Like

I don’t know why I can’t print boolean value in string.

function on_input(self, action_id, action)
    if action_id == hash("touch") and action.pressed then
        print("enabled: "..toString(self.enabled))
        if self.enabled then
            print("yes")
		else
			print("no")
        end
    end
end

It provide attempt to call global ‘toString’ (a nil value) error.

tostring(), not toString().

1 Like

My bad. I just type variable word wrong in the long script. The problem should not exist if I type it correct.

Also note that the function is a bit over complicated, which might confuse things.
Since it only ever sets the variable to true, you can replace it with:

function on_message(self, message_id, message, sender)
    self.enabled = true
end
3 Likes