Adam's Lair Forum

game development and casual madness
It is currently 2018/12/14, 06:01

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: 2018/09/05, 13:35 
Novice Member
Novice Member
User avatar

Joined: 2018/08/28, 17:12
Posts: 12
Role: Hobbyist
Hi.

I'm trying to create some objects in the level during runtime but instead of having a public property to set prefab manually in the editor, I would like that if my component could figure this out and find the prefab of the object on it own. But I can't find the solution.

This is what I tried:

Code:
ContentRef<Prefab> objPrefab = this.GameObj.PrefabLink.Prefab;


But when I try to run a preview I get Null Reference Exception error saying Object reference not set to an instance of an object and PrefabLink returned null. Which basically means it did not find a link to a prefab I guess, but I'm not sure why. The object does have a prefab.
Could someone explain please why it doesn't work?

I can get it to work if I create a public property and set the prefab manually in the editor, but I though it would be fun to be able to make the component figure this out on it own.

I would appreciate any help.
Thanks.


Top
 Profile  
 
PostPosted: 2018/09/05, 14:20 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 880
Location: Italy
Role: Hobbyist
AFAIK, the PrefabLink is a property that is only available in the editor, and gets discarded when running the game proper - as you can notice, while the editor is in "Editor" mode, the SceneView displays your prefab object in blue, and with a link icon, while it appears black as all the others when running in "Game" mode; so you can't rely on it to do what you want to.

This said, you can either associate a ContentRef<Prefab> property to your Component (as you already did), or simply do a manual lookup in the ContentProvider (either you know the name of the Prefab resource, or you do some logic of your own to find it).

Something like this:
Code:
ContentRef<Prefab> pf = ContentProvider.RequestContent<Prefab>(@"Data\MyPrefab.Prefab.res");
if (pf.IsAvailable)
   this.GameObj.ParentScene.AddObject(pf.Res.Instantiate());

Hope it helps :D

_________________
Come on Duality's Discord channel. We have cookies! :mrgreen:


Top
 Profile  
 
PostPosted: 2018/09/05, 16:21 
Novice Member
Novice Member
User avatar

Joined: 2018/08/28, 17:12
Posts: 12
Role: Hobbyist
Thank you for the explanation. It helped a lot :)

It explains then why I was able to totally unintentionally create an editor plugin... well sort of. It was more of a bug than a plugin xD

I managed to get it working in the preview, but then something probably crashed because even when I stopped the preview, the scene did not reset and all the objects created in the preview remained in the editor and I was keep getting the message the scene was edited externally. I though at first it cool but then realized something seriously messed up because the game did not run any more with the Launcher not even if I changed everything back as it were and after messing around a bit more I was unable to load the scene any more. Fortunately I had a backup.

I should have triggered something that Dualitor was not prepared for :twisted:


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 17 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group