Help with string.match (SOLVED)

#1

i tryed string.match(s,"\(.*?)\]") for [/collection2/go] but it returns nil
i need /collection2/go

0 Likes

#2

You should not use string manipulation on hash and URL objects as that doesn’t work in release builds.

1 Like

#3

Ok, but i use the monarch and when: menu->game and after game->menu->game, url of game changes and i can’t use object factory for spawn, couse i don’t know new adress

0 Likes

#4

If you have an object with id game in your scene it should always be reachable with the the URL /game from inside the collection itself.

If game is instead created by a factory then you should be able to capture the ID returned by create().

1 Like

#5

Yes, this is good, but i need have access to factory :rofl::rofl::rofl:. And factory adress changes by monarch and i am don’t have new adress, what i need to do?

1 Like

#6

Do you want to send messages to a script that is in you game collection?

One way to do this is by storing the urls with something like this https://gist.github.com/Jerakin/0ff51c5cd96e4daf5d933eecf86bf74a

local url = require "utils.url" -- Path to the gist


-- game.script | The script in the collection you want to send a message to
function init(self)
  url.GAME = msg.url()
end

function on_message(self, message_id, message)
  if message_id == hash("something_happend") then
    -- do something
  end
end


------------------------------------------------
-- other.script | In any script you want to send a message from
function init(self)
  msg.post(url.GAME, "something_happend")
end

1 Like

#7

You need to explain in detail what it is you wish to do. I assume the following:

  • You are using Monarch to load screens. You are using Collection Proxies to load the screens.
  • In a loaded screen (ie collection) you have a game object containing a factory.
  • You want to use the factory to spawn a game object
  • You do not know how to address the factory in your call to factory.create()

Is this correct?

0 Likes

#8

Yes, shure, and the adress that i pass to factory.create changes in ever i change a screen( and i don’t know how to bring new adress

0 Likes

#9

No. If you are loading the screen using a collection proxy the URL will always be the same. If you have a collection with id foo containing a game object boo and a factory with id zoo then the URL to the factory will always be foo://boo#zoo.

If you are using a collection factory to create the screen then the URL will change every time.

0 Likes

#10

image
after called it. The adress of game changes.


popup->game->menu->popup->game
and after it i can’t use my adresses. ;((
game changes collection2 -> collection5

0 Likes

#11

Can you please share the project? Either here or with me (bjorn@defold.com).

0 Likes

#12

ok

0 Likes

#13

I took a look at the project and you are in fact using collection factories for the screens and not collection proxies:

35

If you change from collection factory to collection proxy and change from screen_factory.script to screen_proxy.script you will always get the same URL to your screens.

Another thing to note is that you are using the absolute URL when posting the messages in question:

GAMESCREEN = "main:/collection2/go#gameMain"
msg.post(GAMESCREEN, "stopGame")

There is no need for this. The script that is posting the message (game.gui_script) is on the same game object as the receiver (gameMain.script). It is enough to change GAMESCREEN to:

GAMESCREEN = "go#gameMain"
msg.post(GAMESCREEN, "stopGame")

You can even write:

GAMESCREEN = "#gameMain"
msg.post(GAMESCREEN, "stopGame")
3 Likes

#14

wow, your my hero

1 Like

#15

and one more moment. You said that i need more local functions. but i have problem with it. timer.delay can’t call local functions, it want’s only global.

0 Likes

#16

Sure it can. You must make sure the function is declared before you pass it to the timer callback or invoke it from within your timer callback.

1 Like

#17

thank you

0 Likes