Jump to content

GetTarget(0) not working?


Canardia
 Share

Recommended Posts

When I add this to my Spawn function, it doesn't find the target:

function Spawn(model)
local entity=base_Spawn(model)

local target = model:GetTarget(0)
if target~=nil then
	AppLog("TARGET 0 FOUND")
end
end

 

It's correctly targetted in the sbx file:

Model {
path="fuselage.gmf"
position=-2.82523990,10.4962835,-4.36183310
rotation=-1.15659511,0.360873282,-0.258110493
scale=1.00000000,1.00000000,1.00000000
id=503696184
target0=503749016
"class"="Model"
"collisiontype"="1"
"friction"="0.00999999978,0.00999999978"
"intensity"="1"
"mass"="1.0000000000000000"
"name"="fuselage_1"
"reloadafterscript"="1"
}

Model {
path="propeller.gmf"
position=-2.84715009,9.77161789,-6.75078249
rotation=0.000000000,0.000000000,-0.585468173
scale=1.00000000,1.00000000,1.00000000
id=503749016
"class"="Model"
"collisiontype"="1"
"friction"="0.500000000,0.500000000"
"gravity"="0"
"intensity"="1"
"mass"="1.0000000000000000"
"name"="propeller_2"
"reloadafterscript"="1"
}

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Would that return the actual entity/model or just the name of it? If it returns the entity/model then it's possible that it hasn't been spawned yet. This is something I notice we have to be careful about in the Spawn() function. We really can't be guaranteed any other entities are loaded at that point.

 

It would be nice if our entities had a method that is called once after everything has been loaded. Because we may wish to store references to other entities in an entity. Currently to do that we'd have to create some sort of loaded flag and check it/set it in the entities Update() method.

Link to comment
Share on other sites

Ah, that was it!

It doesn't really matter in which order the 2 models are spawned, since both need to be loaded first.

I need to make a delayed initialize, so around 5 seconds after the scene is loaded, I need to call my custom Initialize() function for all models:

function Initialize()
for model,entity in pairs(entitytable) do
	local target = model:GetTarget(0)
	if target~=nil then
		AppLog("TARGET 0 FOUND")
	end
end
end

I don't know if there is somekind of function which would inform me that all models are loaded, or do I really have to wait for a random amount of time before I can call this Initialize for all models.

 

Actually I have one idea how to make this automatic: I could create a SceneReady model, which I manually place at the end of the sbx file, and it would have an script which sets a global flag that now the whole scene is loaded. Then the UpdatePhysics() function of each Model checks for that flag, and calls the Initialize() function once.

 

Or I could have a frame counter, which is raised by 1 each time the UpdateWorld() is called. The FrameCounter would be a model too, and only if it has the value 2, then the Initialize function would be called from each Model's UpdatePhysics() function.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

Ah, that was it!

It doesn't really matter in which order the 2 models are spawned, since both need to be loaded first.

I need to make a delayed initialize, so around 5 seconds after the scene is loaded, I need to call my custom Initialize() function for all models:

function Initialize()
for model,entity in pairs(entitytable) do
	local target = model:GetTarget(0)
	if target~=nil then
		AppLog("TARGET 0 FOUND")
	end
end
end

I don't know if there is somekind of function which would inform me that all models are loaded, or do I really have to wait for a random amount of time before I can call this Initialize for all models.

 

Actually I have one idea how to make this automatic: I could create a SceneReady model, which I manually place at the end of the sbx file, and it would have an script which sets a global flag that now the whole scene is loaded. Then the UpdatePhysics() function of each Model checks for that flag, and calls the Initialize() function once.

 

Or I could have a frame counter, which is raised by 1 each time the UpdateWorld() is called. The FrameCounter would be a model too, and only if it has the value 2, then the Initialize function would be called from each Model's UpdatePhysics() function.

 

 

Couldn't you make a boolean flag in the spawn and set it to false. Then in your Update() check if the flag is false and if so run an init function and then set it to true. It'll make this check each call to Update() but that wouldn't cause any significant performance hit. The though being that when Update() is called everything should be loaded by that point.

Link to comment
Share on other sites

Whatever behavior you are enabling, do it in the SetTarget() function. Then it will just be performed when the target is set. When the Spawn() function is called, the targets haven't even been set yet.

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