# \[SOLVED\] How to create a queue of spawning game objects after one another and stopping based on a countdown timer

**URL:** <https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977>\
**Category:** Questions\
**Created:** [October 31, 2022, 3:53am UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977 "2022-10-31T03:53:39Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![ross.grams](https://forum-defold.b-cdn.net/letter_avatar_proxy/v4/letter/r/cab0a1/32.png) [@ross.grams](https://forum.defold.com/u/ross.grams)\
**Post date:** [November 2, 2022, 1:57am UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/21 "2022-11-02T01:57:17Z")

</div>

OK, I’ll stop being annoying and just explain it 😛 : ‘self’ is always given as a function argument. In other words, a local variable inside a function. The problem is, you are not passing ‘self’ on to the “generate\_book\_code()” function, so ‘self’ doesn’t exist inside that function when it runs. You want to do this:

```lua
function init(self)
	generate_book_appearance(self)
	generate_book_code(self)
end

local function generate_book_code(self)
	self.book_code = ""

```

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 2, 2022, 2:01am UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/22 "2022-11-02T02:01:58Z")

</div>

HOLY CRAP, I FEEL DUMB. Also I was having dinner and that popped into my head, I was thinking about how I had arguments inside of `insert-book-code.script` and they all passed `self`. THAT MAKES SENSE NOW. 🤦‍♂️

Wait though, would I even need to make the variables inside of `book.script` unique? Considering I’m using the factory to make a unique instance each time?

Thanks by the way for sticking it out with me.

Ok, I’m making progress though, managed to fix another error, it was the `msg.post` one, I was passing a string instead of a table and it didn’t like that, and I fixed it, still got a few errors to go though.

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/c/5/c51b97e6e8630e01ba3b8ac299c90a75823b99a8.jpeg)  
It’s spawning a book at least.

```lua
ERROR:GAMEOBJECT: Component '/instance0#random_book_code_label' could not be found when dispatching message 'set_text' sent from default:/instance0#random-book
ERROR:GAMEOBJECT: Instance '<unknown>' could not be found when dispatching message 'play_sound' sent from default:/minigame_go#insert-book-code
INFO:DLIB: SSDP: Started on address 10.0.0.78
ERROR:SCRIPT: ...games/inventory/insert-book-code/insert-book-code.script:25: Could not send message 'game_over' from 'default:/minigame_go#insert-book-code' to 'main:/main'.
stack traceback:
  [C]:-1: in function post
  ...games/inventory/insert-book-code/insert-book-code.script:25: in function <...games/inventory/insert-book-code/insert-book-code.script:24>

```

Ah ok, I’d probably have to pass in that instance for the url then.

```lua
local function generate_book_code(self)
	self.book_code = ""
	local charset = {} do
		-- Ký tự [0-9a-zA-Z] dưới dạng mã ASCII: https://www.rapidtables.com/code/text/ascii-table.html
		for c = 48, 57 do table.insert(charset, string.char(c)) end
		for c = 65, 90 do table.insert(charset, string.char(c)) end
		for c = 97, 122 do table.insert(charset, string.char(c)) end
	end
	-- [0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}
	while (string.len(self.book_code) <= 19) do 
		if (#self.book_code % 5 == 0) then
			self.book_code = self.book_code .."-" 
		else
			self.book_code = self.book_code .. charset[math.random(1, #charset)] 
		end
	end
	-- Ok, this is the issue here. I already tried to pass self"#random_book_code_label" but that didn't work
	label.set_text("#random_book_code_label", self.book_code)
	msg.post("/computer#computer", "actual_book_code", {book_code = self.book_code})
end

```

Wait nope, fixed that issue too, turns out I got the wrong name for the label. 🤣 There’s still the `play_sound` issue, will try to figure that out, in fact I’m trying to find where it is. Got the sound to work. Gotta figure out how to make my spawned game object bigger, I’ll figure that out, I think you can specify scale with factory. Unless there’s a way to do that in the game object itself. Another one is to figure out how to check to see if your typed input matches the book code, hm. Oh yea, and trying to figure out how to return to the main collection and then loading the next minigame. We’ll get to that when we get to that.

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/5/f/5f5c5d7c0dd15becb7e61edb41b9fe678cd44b3f.png)  
 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/d/2/d24e855c75c324b879f4f0fd1969ba8263a5d4cf.png)

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 3, 2022, 3:40am UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/23 "2022-11-03T03:40:01Z")

</div>

Alright, it sort of works, just one odd problem now, with the timer, after it’s done it’s giving me an error where it can’t find the main collection.

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [November 3, 2022, 5:56am UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/24 "2022-11-03T05:56:51Z")

</div>

What is the code you are running in the timer?

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 3, 2022, 12:32pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/25 "2022-11-03T12:32:50Z")

</div>

```lua
// timer
timer.delay(1, false, function()
		msg.post("main:/main#main", "game_over")
	end)

// main
function on_message(self, message_id, message, sender)
	if message_id == hash("game_over") then
		pprint("Create nested function for loading and unloading minigames?")
	end
end

ERROR:SCRIPT: ...games/inventory/insert-book-code/insert-book-code.script:24: Could not send message 'game_over' from 'default:/minigame_go#insert-book-code' to 'main:/main#main'.
stack traceback:

```

I wonder if it has to do with the directory.

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/4/9/49bff9466aad5e16861d1bb5e32cdeedee9bd1a8.png)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [November 3, 2022, 1:06pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/26 "2022-11-03T13:06:28Z")

</div>

> [@accoladious](#):
>
> I wonder if it has to do with the directory.

No no no. The way you have arranged stuff in your Assets (the panel on the left) in terms of filenames and folder structure has nothing to do with the url used in `msg.post`. It is the structure in the Outline that matter, and the ids you have assigned there.

> **[Addressing in Defold](https://defold.com/manuals/addressing/)**
>
> This manual explains how Defold has solved the problem of addressing.

> **[Messaging between game worlds - Message passing in Defold](https://defold.com/manuals/message-passing/#messaging-between-game-worlds)**
>
> Message passing is a mechanism for Defold game objects to communicate with each other. This manual assumes that you have a basic understanding of Defold’s addressing mechanism and basic building blocks. | Message passing is the mechanism used by...

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 3, 2022, 2:39pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/27 "2022-11-03T14:39:30Z")

</div>

![image](https://forum-defold-uploads.b-cdn.net/original/3X/a/1/a169abfe0fabff2e85587e017f32303f69b47d54.png)  
I see, I think I might know the issue, I’d have to load the main collection before sending message posts to it, and probably unload the current collection I’m in. Thank Britzl. Or I start from main.collection, then load in the current collection I’m on then unload the latter when the timer goes down, i.e. unload current collection then it should call inside of main

```lua
if message_id == hash("proxy_unloaded") then
       pprint("Cycle through the minigames again.")
    end

```

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 19, 2022, 3:24am UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/28 "2022-11-19T03:24:53Z")

</div>

Hate to revive a thread, solved one scenario but now have a similar scenario to this one. Basically I’m now trying to spawn a bunch of game objects that have a different sprite for each instance (basically they’re just alphabetical letters) and am using [Factory and Properties](https://github.com/britzl/publicexamples/tree/master/examples/factory_and_properties) as an example, however I don’t think I need to add functionality to the game object I’m spawning in its own script since it’s just being used in this specific collection. I would assume that me using `self.letter` would make each instance unique, so I don’t understand why it wouldn’t recognise the id, and since `self.letter` is an instance of the spawned game object, shouldn’t `go.animate` recognise that path? Thanks to anyone who responds.

```lua
local function random_position()
	return vmath.vector3(math.random(1, sys.get_config("display.width")), math.random(1, sys.get_config("display.height")), 0)
end

local function spawn_letters(self)
	local charset = {} do
		-- Ký tự [0-9a-zA-Z] dưới dạng mã ASCII: https://www.rapidtables.com/code/text/ascii-table.html
		for c = 97, 122 do table.insert(charset, string.char(c)) end
	end
	for i,v in ipairs(charset) do
		self.letter = factory.create("#alphabet_factory", random_position())
		sprite.play_flipbook(self.letter.."#alphabet_sprite","keyboard_"..v)
		go.animate(self.letter.."#alphabet_sprite", "tint", go.PLAYBACK_LOOP_PINGPONG, self.color, go.EASING_INOUTCIRC, 2)
	end
end

function init(self)
	msg.post(".", "acquire_input_focus")
	spawn_letters(self)
	timer.delay(8, false, function()
		print("Failed.")
		msg.post("main:/main#main", "game_over", {score = 0})
	end)
end

function on_input(self, action_id, action)
end

function final(self)
	msg.post(".", "release_input_focus")
end

```

Errors below:

```lua
ERROR:SCRIPT: ...est-service/where-to-find-book/where_to_find_book.script:14: Could not find any instance with id '<unknown>'.
stack traceback:
  [C]:-1: in function animate
  ...est-service/where-to-find-book/where_to_find_book.script:14: in function spawn_letters
  ...est-service/where-to-find-book/where_to_find_book.script:20: in function <...est-service/where-to-find-book/where_to_find_book.script:18>

```

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/d/a/da9f1178487938c04b4e419b19dd6f9d3ada434e.png)

---

<div class="post-metadata">

**Author:** ![britzl](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/britzl/32/23_2.png) [@britzl](https://forum.defold.com/u/britzl)\
**Post date:** [November 19, 2022, 8:14am UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/29 "2022-11-19T08:14:27Z")

</div>

> [@accoladious](#):
>
> `self.letter.."#alphabet_sprite"`

You can’t use string concatenation. Use:

msg.url(nil, self.letter, “alphabet\_sprite”)

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 19, 2022, 4:18pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/30 "2022-11-19T16:18:54Z")

</div>

> [@britzl](#):
>
> msg.url(nil, self.letter, “alphabet\_sprite”)

Thanks for replying britzl, but how would I determine what image the sprite should be set to?

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [November 19, 2022, 4:25pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/31 "2022-11-19T16:25:41Z")

</div>

Is this not what you are looking for as Britzl suggested?

```lua
local sprite_url = msg.url(nil, self.letter, “alphabet_sprite”)
sprite.play_flipbook(sprite_url,"keyboard_"..v)

```

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 19, 2022, 7:36pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/32 "2022-11-19T19:36:53Z")

</div>

> [@selimanac](#):
>
> ```lua
> local sprite_url = msg.url(nil, self.letter, “alphabet_sprite”)
> sprite.play_flipbook(sprite_url,"keyboard_"..v)
> 
> ```

Ah I see, yea I realise I made a typo on my variable name (and now I feel dumb). 😑 Thanks selimanac for replying.

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 19, 2022, 7:55pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/33 "2022-11-19T19:55:41Z")

</div>

I guess the problem is the animating part now.

```lua
ERROR:GAMEOBJECT: Properties can not be of type 'nil'.
ERROR:SCRIPT: ...est-service/where-to-find-book/where_to_find_book.script:14: only numerical values can be used as target values for animation
stack traceback:
  [C]:-1: in function animate
  ...est-service/where-to-find-book/where_to_find_book.script:14: in function spawn_letters
  ...est-service/where-to-find-book/where_to_find_book.script:20: in function <...est-service/where-to-find-book/where_to_find_book.script:18>

```

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [November 19, 2022, 8:22pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/34 "2022-11-19T20:22:59Z")

</div>

> [@accoladious](#):
>
> `self.color`

```lua
pprint(self.color)

```

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 19, 2022, 9:22pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/35 "2022-11-19T21:22:07Z")

</div>

Ah I see, it’s a nil value, ok, I see now, I should’ve been animating the position property, not the color, that was my bad. It’s working now mostly. Tried bumping up the speed of the animation but it seems like they’re still not moving. Hm, shouldn’t it move though? I called `random_position()` twice. Just debugged it, yea `random_position()` works.

```lua
go.animate(self.letter, "position", go.PLAYBACK_LOOP_PINGPONG, random_position(), go.EASING_INOUTCIRC, math.random(1200, 2400))

```

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/d/b/dbe9d1cb03fb7553325fab6dceda67a1017f25eb.png)

 ![image](https://forum-defold-uploads.b-cdn.net/original/3X/c/0/c0ae776fffd0bfad459cc8fedb6fcfff88370779.png)  
Files are unfortunately too big: [link to them](https://file.io/D32dhHXolPTX). Again, if anyone’s willing to take a look I’d appreciate it, I understand if not, admittedly my organisation’s pretty bad.

---

<div class="post-metadata">

**Author:** ![selimanac](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/selimanac/32/6397_2.png) [@selimanac](https://forum.defold.com/u/selimanac)\
**Post date:** [November 20, 2022, 6:31am UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/36 "2022-11-20T06:31:24Z")

</div>

Your URL works, `random_position()` works. So, what might be the problem?  
Are you sure this duration is proper?  
Did you try something like 1 or 2 for duration?  
Are you sure the start position and end position is different or distance between them large enough for duration 1200-2400?

```lua
math.random(1200, 2400)

```

---

<div class="post-metadata">

**Author:** ![accoladious](https://forum-defold.b-cdn.net/user_avatar/forum.defold.com/accoladious/32/30113_2.png) [@accoladious](https://forum.defold.com/u/accoladious)\
**Post date:** [November 20, 2022, 4:54pm UTC](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977/37 "2022-11-20T16:54:34Z")

</div>

Thank you for pointing that out, no it wouldn’t actually make sense for it to be that big logically speaking. So I changed it to 1-10 instead, which is a lot better now, I still have to figure out how to make it not go offscreen, I wonder if it has to do with the fact I’m using rendercam so I’d have to translate from screen view to world view like with inputs. I think in the random\_position method I’d have to tweak it so it converts from screen to world view.

I’m looking at the rendercam scripts and I don’t see a method like `GetViewportSize()`, I’ll see if I can look into it more. There’s a `set_viewport()` but I’m not sure if that’s what I’m looking for.

Also I just realised it’s theoretically possible to randomly generate a position and have a chance it be the same position twice, I’d probably have to make a method that checks to make sure that’s not the case and if it is, then rerandomise it.

> [@\[SOLVED\] Registering objects being clicked and dragged using Defold Input library as nil](https://forum.defold.com/t/registering-objects-being-clicked-and-dragged-using-defold-input-library-as-nil/72009/14):
>
> There is an example of how to use a camera and cursor at the same time in the Defold-Input project. Here (using the Orthographic camera solution, but the same thing can be done with RenderCam): [https://github.com/britzl/defold-input/blob/master/examples/cursor\_and\_camera/controller.script#L11-L16](https://github.com/britzl/defold-input/blob/master/examples/cursor_and_camera/controller.script#L11-L16) It is also mentioned in the documentation here: [https://github.com/britzl/defold-input/blob/master/in/cursor.md#input-handling](https://github.com/britzl/defold-input/blob/master/in/cursor.md#input-handling)

If I do something like this,

```lua
local screen = vmath.vector3(action.x, action.y, 0)

```

wouldn’t it be grabbing the location of the cursor, i.e. `action.x`, `action.y`? That wouldn’t be the viewport size though.

Just going to use default camera since I’m not using orthogonal or perspective view.

[Previous page](https://forum.defold.com/t/solved-how-to-create-a-queue-of-spawning-game-objects-after-one-another-and-stopping-based-on-a-countdown-timer/71977.md?page=1)
