Hey everyone, I’m running into a very frustrating issue. I’m trying to figure out why for some reason my script attached to multiple collections never is running the init function. I’ve posted a video here showing the problem:
Does any script attached to them run init?
go.property()
needs to be run outside of any functions
Thanks I just added that but moving it outside doesn’t affect this issue. I created a “test” script and attached it to one of the maps, and it does initialize. Not sure why this script isn’t working. I want to use the same script for all the instances since the logic there is the same for all maps. Is this not supported?
EDIT: I attached the same test script to all instances and it inits each time. Why is this script not doing anything? There’s no error printout it just doesn’t activate for some reason.
This kind of thing is supported, the only thing to really keep in mind is that any global variables will be shared across each instance. Does it work if you use print statements instead of breakpoints?
Yeah printing is fine, but the debugger doesn’t stop in the init function so I have no idea of why the script won’t work when I load a second level. I’ve been running into quite a few debug issues though.
Aside from the breakpoint not working and some console errors, I don’t see anything going wrong in the video. Could you explain more about what the issue is?
The logic for the script isn’t consistent when I initialize for a second map. Do you know if there’s a way to create a new script instance for an object? When I load a level the second time, it fails to spawn the player even though it will work if it’s the first map to be loaded w/ that script attached.
Like I said before, global variables will be kept between script instances. There’s probably something in your logic that breaks because of that. The self
table doesn’t carry between instances, so use that.
Here’s the entire script for the dungeon instance. I have some globals, but I want to affect these objects/variables interdependently from the level instance:
require "helpers"
require "level_helpers"
require "progress"
require "singletons"
go.property("sub_level", 0)
function init(self)
self.entities = {}
level_ref = msg.url()
self.level_init = true
self.pusher_block_nodes = {}
table.insert(self.pusher_block_nodes, { pos = vmath.vector3(46 * wall_width, 16 * wall_height, 0.5), time_scalar = 250 } )
table.insert(self.pusher_block_nodes, { pos = vmath.vector3(46 * wall_width, 14 * wall_height, 0.5), time_scalar = 50 } )
table.insert(self.pusher_block_nodes, { pos = vmath.vector3(4 * wall_width, 14 * wall_height, 0.5), time_scalar = 250 } )
table.insert(self.pusher_block_nodes, { pos = vmath.vector3(4 * wall_width, 16 * wall_height, 0.5), time_scalar = 50 } )
end
function update(self, dt)
if self.level_init then
level_helpers.init(self)
self.y_max = self.y_max + 2
self.x_max = self.x_max + 2
self.portal_locs = {}
local portal_locs = level_helpers.find_portals(self)
level_helpers.spawn_entities(self)
msg.post(main_ref, hash("load_fx_descent"))
local portal_1 = level_helpers.multiply_portal_loc(portal_locs[1].x, portal_locs[1].y)
table.insert(self.portal_locs, { level = hash("use_map_main"), x = portal_1.x, y = portal_1.y, spawn = true } )
msg.post(main_ref, hash("set_portals"), self.portal_locs)
msg.post(main_ref, hash("set_death_event"), { event = hash("load_level"), level = hash("overworld"), sub_level = hash("base") })
prg_keys = 0
self.level_init = false
end
end
function on_message(self, message_id, message, sender)
if message_id == hash("add_entity") then
table.insert(self.entities, message.id)
elseif message_id == hash("spawn_switch") then
self.switch_id = factory.create("#switchfactory", vmath.vector3(message.x, message.y, 0.5), nil, nil, 1)
table.insert(self.entities, self.switch_id)
elseif message_id == hash("spawn_key") then
local key_id = factory.create("#keyfactory", vmath.vector3(message.x, message.y, 0.5), nil, nil, 1)
table.insert(self.entities, key_id)
elseif message_id == hash("switch_triggered") then
if self.sub_level == 5 then
for x=12,14 do
tilemap.set_tile("#map", "walls", x, 8, 0)
end
for x=22,24 do
tilemap.set_tile("#map", "walls", x, 8, 0)
end
for x=33,35 do
tilemap.set_tile("#map", "walls", x, 8, 0)
end
end
elseif message_id == hash("get_path_nodes") then
msg.post(sender, hash("start_moving_on_path"), self.pusher_block_nodes )
end
end
function final(self)
go.delete_all(self.entities)
if player_ref ~= nil then
msg.post(player_ref, hash("heal_player"))
end
end
EDIT: Would anyone be available for a video conference to try to sort this out? This seems like an engine bug but I have put too much work into this project to simply post it online.
Which part of the logic? How is it not consistent?
I managed to fix it. I had to re-construct the entire parent collection from an external script and pass the sub_level property into the main script. It seems the issue is creating a collectionfactory from within another collectionfactory. Not sure exactly why it didn’t work the original way.