I get this Error: main/cursor.script:26: attempt to index local 'self' (a nil value)
This might again be something simple, but I have no idea why this happens… any idea?
tL.
(EDIT: when I put the line below in the “on_message” function and trigger the “on_message()” function from another collection with msg.post() , I get this:
print("reset cursor called for y_pos: ", self.y_pos) --> prints 320
go.property("y_pos", 480)
local score = 0
function init(self)
msg.post(".", "acquire_input_focus")
self.isActive = true
self.isInside = false
self.position = go.get_position()
self.position.x = 1010
self.position.y = self.y_pos --> WORKS
print(msg.url(), "position: ", self.position.y) --> WORKS
print(msg.url(), "position property: ", self.y_pos) --> WORKS
print("collection URL:", msg.url())
self.target_layer_pos = go.get_position("target_layer")
print("target layer pos:", self.target_layer_pos) --> WORKS
local val = go.get("#", "y_pos")
print("go.get y_value: ", val)
go.set_position(vmath.vector3(1010, self.y_pos, 0.8), ".") --> WORKS
end
function randomize_target_layer(self) -- go.set_position throws an error
math.randomseed(os.time())
math.random(); math.random(); math.random()
go.set_position(vmath.vector3(self.position.x + math.random(-200,200), self.position.y, self.position.z)) --> attempt to index local 'self' (a nil value)
end
function reset_cursor(self)
print("reset_curser called for ", msg.url())
go.cancel_animations(".", "position.x")
-- print("reset cursor called for y_pos: ", self.y_pos) --> attempt to index local 'self' (a nil value)
-- go.set_position( vmath.vector3(1010, self.y_pos, 0.8), ".") --> attempt to index local 'self' (a nil value)
randomize_target_layer()
go.animate(".", "position.x", go.PLAYBACK_LOOP_PINGPONG, 270, go.EASING_INOUTSINE, 2)
end
function on_message(self, message_id, message, sender)
-- print("message_id: ", message_id, "sender: ", sender)
if message_id == hash("active") then
print("reset cursor called for y_pos: ", self.y_pos)
print("Activate ", msg.url())
self.isActive = true
go.set_position(self.position) --> works!!
-- below: attempt to index field 'target_layer' (a nil value)
go.set_position(vmath.vector3(self.target_layer_pos.x + math.random(-200,200), self.target_layer_pos.y, self.target_layer.pos.z),"target_layer")
-- reset_cursor() --> throws the same error
elseif message_id == hash("reset") then
go.cancel_animations(".")
-- reset_cursor()
end
if self.isActive then
if message_id == hash("trigger_response") then
if message.enter then
print("Inside", message.other_id)
self.isInside = true
else
print("Outside", message.other_id)
self.isInside = false
end
end
end
end
When you call reset_cursor() and randomize_target_layer() , they need to be passed the parameter: self. Because you don’t do this it defaults to a nil value.
still errors out: attempt to index field 'target_layer' (a nil value)
and if the randomize_target_layer(self) is called in the on_message() function with randomize_target_layer(self)
the same go.set_position() line in the randomize_target_layer(self) function
go.set_position( vmath.vector3(1010, self.y_pos, 0.8), ".") --> attempt to index local 'self' (a nil value)
gives still this error…
again, this is the default function on_message(), still errors out by go.set_position():
function on_message(self, message_id, message, sender)
-- print("message_id: ", message_id, "sender: ", sender)
if message_id == hash("active") then
print("reset cursor called for y_pos: ", self.y_pos)
print("Activate ", msg.url())
self.isActive = true
go.set_position(self.position) --> works!!
-- in the below go.set_position, again: attempt to index field 'target_layer' (a nil value)
go.set_position(vmath.vector3(self.target_layer_pos.x + math.random(-200,200), self.target_layer_pos.y, self.target_layer.pos.z),"target_layer")
reset_cursor(self) --> throws the same error
elseif message_id == hash("reset") then
go.cancel_animations(".")
-- reset_cursor()
end
I just recreated the code, because I am already much further, with a workaround for that. So I don;t have the original code anymore and I went with a hurry.
But this line, in my function(self) → called with (self):
go.set_position(vmath.vector3(self.position.x + math.random(-200,200), self.position.y, self.position.z)) --> attempt to index local 'self' (a nil value)
Throws still an error. I explained everything in my first post above, that was “pure vanilla” in the state where I was. There was even no “target_layer” at that point.
does not throw an error anymore. This works now. Thanks for catching this.
I do not know why this original problem still persists, but it doesn’t matter. I spent too much time to figure this out, did everything what is proposed here, still doesn’t work, so let it be the end of it.
Thanks for your time ,
tL.
(I can not recreate the state of the code anymore, because I left these lines out and did something else to prevent this error. I will do pretty soon a very simple script. If this still doesn’t work I will post the script here.)
Yeah, I did that test script right now.
test.script dropped on a go in a new collection:
-- test.script
go.property("test", 123)
function init(self)
print(self.test) --> prints: 123
self.position = go.get_position()
print(self.position) --> prints: vmath.vector3(500, 500, 0)
msg.post("#", "test")
end
function test(self)
print("in test, self.position: ", self.position) --> prints: vmath.vector3(500, 500, 0)
go.set_position(self.position)
go.set_position(vmath.vector3(self.position.x, self.position.y, self.position.z))
print("in test function, vmath: ", vmath.vector3(self.position.x, self.position.y, self.position.z)) --> vmath.vector3(500, 500, 0)
end
function on_message(self, message_id, message, sender)
if message_id == hash("test") then
print("received message `test`")
print(self.test) --> prints: 123
test(self) --> calls test(self), WORKS.
end
end
Everything works. Everything. So there is another problem in the code. I do not know what breaks the code, but this one works. So there mus be something else at fault here, very likely on my side.
But I will figure it.