Jump to content

Entity's childs "Start" function does not get called!


Phodex Games
 Share

Recommended Posts

Hi Leadwerkers,

 

I have a problem. I would like to load my player prefab file, when the map loads and put it to a specific position. So I created a script for that. It loads my player prefab on start. The problem is, the Start function of its entities does not get called. I was able to fix it in a way but I still have a porblem. I told my main player script to call the Start functions of every childs script. The problem is some childs have childs aswell, and this child have other childs and so on. So why do the Start functions not work if I load a Prefab of my player which has many childs? Is there any fix? Suggestions? Am I doing something wrong? Or how to access every! child, and the childs of the childs etc of a singel entity, to then call the start function. I could make a ridiculous amount of loops, but thats pretty unefficent and stupid.

 

Thanks for the help,

 

Mad Morra

Link to comment
Share on other sites

I made this function so I can call something on all child objects at any time from any script.


function main.system:BroadcastMessage(entity, func, a,b,c)

if entity.script and type(entity.script[func])=="function" then entity.script[func](entity.script,a,b,c) end

for i=0, entity:CountChildren()-1 do

local child = entity:GetChild(i)

if child:CountChildren()~=0 then self:BroadcastMessage(child, func, a,b,c) end

if child.script and type(child.script[func])=="function" then child.script[func](child.script,a,b,c) end

end

end

  • Upvote 1
Link to comment
Share on other sites

Yes, this should be addressed. Its odd that a prefab loaded via map loading will work but loading a prefab via code will not. Seems like the Prefab::NoStartCall is hard coded no matter what value the Prefab:Load() flag is set at. You can find a specific child in the prefab, and call its Start() function.

prefab = Prefab:Load("Prefabs/childstartest.pfb")

child = prefab:FindChild("crawler_attachment")

child.script:Start()

 

Not very useful or reasonable to have to do, but is another approach to call the Start() on prefab children. Or having to set a one time call to the Start() function in the UpdateWorld() of that child's script... either way, pretty silly to have to do.

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

An oldie but a goodie! http://www.leadwerks.com/werkspace/topic/11252-prefab-children-not-getting-scriptstart-called/page__hl__prefab+children

 

Don't recall if I ever sent him an example but he did close it. Prefabs in general need more love. There are other quirks with them that seem to pop up for the artists on our team. Faith in them is low and that's not a good thing.

Link to comment
Share on other sites

Hmm, thanks for your answers, so it seems to be a bug. Thats pretty ugly actually. So I now need to find I way to work around this.

 

@Genebris your solution sound interesting, but as I am no master at coding, I don't understand that line of code you sent me. Whats that "main.system" part at your function? And what does entity.script,a,b,c? I am a little bit confused. However I think I can find my own solution for that. Maybe I write a small class for that I can import, which calls the start function of all of an entites children.

Link to comment
Share on other sites

You can remove main.system to make the function global by itself.

function CallFunction(entity, func, a,b,c)

 

This fragment simply calls needed function with optional parameters. You don't have to use any parameters, or you can use only two of them because it's Lua.


...

func = "Hurt"

a = 5 --damage

b = "Magic" --damage type

--C not used

entity.script[func](entity.script,a,b,c)

...

 

You need to pass entity.script as a first parameter because you are calling function via "." instead of ":"

I think I wasn't able to call it with ":" this way, so I passed script manually.

 

When you have global function created all you need to do is this:


local e = Prefab:Load("prefabs/myprefab.pfb")

CallFunction (e, "Start")

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...