Selfs and go.property but still attempt to index local 'self' (a nil value) (SOLVED)

Hello,

I am realy confused.
First i though i had a solution found here

however i wanted to have multple parameter in my own function

So just to be clear, when you do
self.value = value
at the init part of the script it will be created for the whole script.
just like a go.property is doing,

But with both i got the , cant get the value.

now i do think its about how i work with the parameters.
Goal.
I have a function called engine to do the engine sound.
On movement the function will be called with on
and on release it will be called with off

depending on the esound boolean it starts or stop playing.
however. i dont seems to get it working correctly.

Any tips?

go.property ("next",hash("stone"))
go.property ("hight", 500)
go.property("canbuild", true)
go.property("esound", false)

function engine (a, self)
	--print(self.esound)
	print(a)
	if a == "on" and self.esound == false then
		sound.plays("/sounds#engine",{gain = 0.4})
		self.esound = true 
		print("play")
	end
	if a == "off" and self.esound == true then
		sound.stop("/sounds#egine")
		self.esound = false 
		print("stop")
	end
	

	
end

function on_input(self, action_id, action)
	--timer.delay(0.5, false, function()
	--mouse movement
	--[[if message_id == nil then
		self.speed = 200
		local current_pos = go.get_position() 
		local target_pos = vmath.vector3(action.x, self.hight, 0) -- <5>
		local distance = vmath.length(target_pos - current_pos) -- <6>
		local duration = distance / self.speed -- <7>
		go.animate(".", "position", go.PLAYBACK_ONCE_FORWARD, target_pos, go.EASING_LINEAR, duration, 0)--end)
	end
	--]]
	
	-- keybord movement
	if action_id == hash("up") then
		self.pos.y = 5
		msg.post("#alien", "play_animation", {id = hash("stil")})
		particlefx.play("#movingup")
		engine("on")
	end
	if  action_id == hash("left") then
		self.pos.x = -5
		msg.post("#alien", "play_animation", {id = hash("left")})
		particlefx.play("#movingleft")
		engine("on")
	end
	if  action_id == hash("right") then
		self.pos.x = 5
		msg.post("#alien", "play_animation", {id = hash("right")})
		particlefx.play("#movingright")
		engine("on")
	end
if action_id == hash("down") then
	self.pos.y = -5
	msg.post("#alien", "play_animation", {id = hash("stil")})
	particlefx.play("#movingdown")
	engine("on")
	end

	if action.released then
		self.pos = vmath.vector3(0, 0, 0)
		local animation = go.get("#alien", "animation")
		particlefx.stop("#movingleft")
		particlefx.stop("#movingup")
		particlefx.stop("#movingdown")
		particlefx.stop("#movingright")
		engine("off")
		if animation == hash("left") then msg.post("#alien", "play_animation", {id = hash("leftstil")}) end 
		if animation == hash("right") then msg.post("#alien", "play_animation", {id = hash("rightstil")}) end 
	end

This function requires two arguments: 1) a and 2) self

Here you are calling engine() with only one argument, which means that the second which is self will be nil.

1 Like

Looks like a typo

Also, unless you want “function engine” to be global, I suggest using “local function engine()”

And, you never pass “self” to the function, which should make it nil.
It itsn’t defined automatically. Perhaps you’ve assigned it somewhere as a global?

Thanks for the fast responce,

I now send both:
engine(“on”, self.esound)

And get this error:

attempt to index local ‘self’ (a boolean value)
stack traceback:


Thank for your fast responce,

I now have set it local and indeed was an type error but i dint even get this far in the code yet.

That doesn’t match your function though? “function engine(a, self)”
It should be engine("on", self)

Thank this helps getting a step furder,

Next problem is that it cant read the self.esound in the if statement

if a == “on” and self.esound == false then
sound.play(“sounds#engine”,{gain = 0.4})
self.esound = true
print(“play”)

Gets
attempt to index local ‘self’ (a boolean value)
stack traceback:

Show your updated code please

@Mathias_Westerdahl @britzl

I got it working, last error was because extra old play call

So, the self enter here is like the ability to read the self vaulues because your not giving a specifc one.

Thanks these are insight for a coder noob that makes things really more clear