What is the best way to get information from a table?

Hi!

I have a table (which can be saved) in a collection. The table has 150 entries, each entry corresponds to a level, and each entry is a number (0, 1 or 2), saying whether a level is unavailable, available, or completed. So in my loader script, i have :

self.savefile= { 2,2,2,2,1,0,0,0,0,0,0,0,0,0,0... 0} -- etc., etc. 
	for key, value in pairs(self.savefile) do --calculates total number of levels
		self.numberoflevels= self.numberoflevels+1
		print(self.numberoflevels.." this is the total number of levels")
	end

Now, I have a level select screen with 150 GOs, each with a label, collision object, and script. There is one GO for each level. They are the level select buttons. Each one is sent a message by the loader script.

	elseif message_id == hash("getinfo") then  
		for key, value in pairs(self.savefile) do 
			local msgurl = "levelselect:/LSbutton/LSGObutton"..key.."#LSbutton" --creates URL for each level select button
			msg.post(msgurl, "yourinfo", {value}) --sends each level select buttons it value
			if value ==1.5 then --this line gets levels that have just become available, and makes them available
				self.savefile[key] = 2
			end
			if key == self.numberoflevels then 
				local my_file_path = sys.get_save_file("bv2game", "savefile")
				if sys.save(my_file_path, self.savefile) then
					print("save success!")
				else
					print("save has failed!")
				end
			end
		end

But it’s not working. I think because too many messages are being sent. Can anyone advise how to do this better?

Thanks!

No, you can send any number of messages, with a performance penalty. The only limit on message passing is how many times the engine runs through the message queue each frame. See Message passing in Defold

If you want to reduce the number of messages you could:

  1. Pass a single message with the state of all buttons (send the whole table).
  2. Move the state data table to the GUI so it can set the button states directly without passing messages.

As for what is not working here. Do the messages reach their destination?

As a simple fix you can implement a property on every button. And instead of sending a message you can set a property of the level id for the button.

Level count can be calculated with a simple # sign. local count = #self.savefile.

Probably the best way would be to use a button factory. When you create a new button with factory.create() you can specify level id in that call. So the button would know right from init() which level is it.

1 Like

You’re going to be really disappointed right now, but, the buttons are not GUI elements, they are normal GOs. I suppose your first point also relates to GUI objects.

Sergey: I will look into that. Thanks!

Maybe, it depends how you want to change the button states. You can do that in a button manager script and send it the full state, then have it manipulate the button game objects directly.

Using

go.set(url, property, value)

right?