Help me please with message passing (SOLVED)

I’m stuck with passing message data.

Here is the console log:

INFO:DLIB: Log server started on port 50236
INFO:ENGINE: Engine service started on port 50237
INFO:ENGINE: Defold Engine 1.2.118 (0faa10d)
INFO:ENGINE: Loading data from: build/default
INFO:ENGINE: Initialised sound device 'default'

DEBUG:SCRIPT: ambient sound stoped
DEBUG:SCRIPT: hash: [stop_sound]
INFO:DLIB: SSDP: Started on address 192.168.0.101
INFO:DLIB: SSDP: Started on address 192.168.0.200
DEBUG:SCRIPT: 
{
  sound_name = boat_station_day_sound,
}

DEBUG:SCRIPT: dim_slavya
DEBUG:SCRIPT: hash: [play_sound]
DEBUG:SCRIPT: 
{
  gain = 1,
  delay = 0,
}

ERROR:SCRIPT: /main/scripts/sound/ambient_sound.script:10: attempt to concatenate field 'sound_name' (a nil value)
stack traceback:
	/main/scripts/sound/ambient_sound.script:10: in function 'play_sound'
	/main/scripts/sound/ambient_sound.script:27: in function </main/scripts/sound/ambient_sound.script:17>
INFO:DLIB: SSDP: Done on address 192.168.0.101
INFO:DLIB: SSDP: Done on address 192.168.0.200

Code is pretty simple.

caller:
function play_ambient_sound(ambient_sound_name)
	local msg_data = { sound_name = ambient_sound_name }
	pprint(msg_data)
	msg.post("ambient#script", "play_sound", msg_data)
end

callee:
go.property("gain", 1.0)


function init(self)
	self.sound_name = "ext_road_day_sound"
	play_sound(self)
end

function play_sound(self)
    msg.post( "#"..self.sound_name, "play_sound", {gain = self.gain})
end

function stop_sound(self)
	msg.post("#"..self.sound_name, "stop_sound" )
end

function on_message(self, message_id, message, sender)

	print(message_id)
	if( message_id == hash("stop_sound"))then
		stop_sound(self)
	end

	if( message_id == hash("play_sound"))then
		pprint(message)
		self.sound_name = message.sound_name
		play_sound(self)
	end
end

I do not understand why it does not pass string as param. Sending “gain” is works, as sending message itself.

Thank you in advance.

Your error is saying that self.sound_name is set to nil. Does this issue appear after play_ambient_sound is called? The only thing I can see is that play_ambient_sound might be being called with a nil argument.

Thank you for an answer. As you can see in log sound name is normally assigned and sent as a param to play_ambient_sound.
I do not know why, but when I send “gain” param - it works (message field “gain” is set validly).

There’s a potential problem with the play_sound() and stop_sound() functions since they are declared as global functions. You should make a habit of adding a local keyword in front of function declarations unless you’re absolutely certain that you want it to be a global function.

1 Like

Thank you, I will try to make them local instead.

Nothing helps. Will make workaround - make them as part of message hash.
Thank you all for spending time trying to help me. This is a very heartwarming experience.
Good luck.

Feel free to share the project with me (Bjorn.ritzl@king.com) and I’ll take a look.

Sorry not going too long. Thank you for a kind proposal. I have solved it by using as part of message hash. Maybe I use it incorrectly :slight_smile: but I can not afford to distract you from a job.
You are making a great job and I understand, that engine (this size and optimization) is pretty difficult to make and learn others to use it properly is a difficult task.

4 Likes