Jump to content

start function and loading order


Marcousik
 Share

Recommended Posts

Hello people,

I read a lot of people who encountered problems that the start functions of the children would not fire...

I encounter today the opposite problem.

All my scripts are written like that the main character start function fires at first and then the start() of the children.

Now it seems like Leadwerks let run first the children start() functions that return a many errors as my variables are in the main start() declared...

That's penible because it seems there is no order control about this ?

thx

 

 

Link to comment
Share on other sites

Yeah I have been bitten by this problem several times. What you can do to solve this is add a bool variable IsInit at the end of each start function and set it to true when the end of the start functions has been reached. When you are in a child, check if the parent has this IsInit variable set to true. If not, then perform an init function on the parent first.

  • Thanks 1
Link to comment
Share on other sites

The entities may have Start() called in any order. I have never had a problem writing a script to account for this, but if you have a concrete example of a problem please post it.

If the problem is solved in code then the script just works everywhere. If a complicated ordering system were introduced in the editor then you would have to adjust the order for every map you use the script in.

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

Yes Josh that's right...

I got one time the problem with childs using variables defined in the parent start script like this:

Child:GetParent().script.variable

This "variable" should not be the same if the parent start() function fires first. The same with Global variables defined in the parent start function and that could be used in children start()...

BUT I encounter the problem only one time, just after saving my player-character as Prefab, the same map that runs 100x without bugs didn't anymore.

This occurs only one time so it's not so heavy for me, I could restore the character from a another map, saving it as prefab without problem. I don't know. Little bit strange.

 

 

Link to comment
Share on other sites

On 1/5/2018 at 1:32 PM, Marcousik said:

Yes Josh that's right...

I got one time the problem with childs using variables defined in the parent start script like this:

Child:GetParent().script.variable

This "variable" should not be the same if the parent start() function fires first. The same with Global variables defined in the parent start function and that could be used in children start()...

BUT I encounter the problem only one time, just after saving my player-character as Prefab, the same map that runs 100x without bugs didn't anymore.

This occurs only one time so it's not so heavy for me, I could restore the character from a another map, saving it as prefab without problem. I don't know. Little bit strange.

Normally we solve issues like this with the following design:

function Script:Start()
  	local parent = self.entity:GetParent()
  	if parent.script~=nil then
	  	if type(parent.script.Initialize)=="function" then parent.script:Initialize() end
  	end
  	--do some other stuff to the parent
end

And in the parent script:

function Script:Start()
	self:Initialize()
end

function Script:Initialize()
	if self.initialized then return end
  	self.initialized = true
  	--do some other stuff
end

Hopefully that is clear. Note the need for something like this is very rare. None of the default scripts that ship with Leadwerks need anything like this.

  • Thanks 1

My job is to make tools you love, with the features you want, and performance you can't live without.

Link to comment
Share on other sites

This is even a little simpler:

function Script:Start()
  	local parent = self.entity:GetParent()
  	if parent.script~=nil then
	  	if type(parent.script.Start)=="function" then parent.script:Start() end
  	end
  	--do some other stuff to the parent
end
function Script:Start()
	if self.initialized then return end
  	self.initialized = true
  	--do some other stuff
end

 

  • Like 1

My job is to make tools you love, with the features you want, and performance you can't live without.

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...