Adam's Lair Forum

game development and casual madness
It is currently 2020/01/28, 19:24

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Bullet shooting
PostPosted: 2015/11/09, 11:41 
Newbie
Newbie

Joined: 2015/11/09, 11:38
Posts: 2
Role: Hobbyist
Hi!
I'm all new to Duality, and I'm stuck trying to learn the engine.
I am not a professional in C#, but I do know all of the basics.
My question goes as follows: How can I implement bullets/projectiles in the
introductionary tutorial on this site? :-) I get my ship to move perfectly as I want, but I would
like to see it fire!

Thanks


Top
 Profile  
 
 Post subject: Re: Bullet shooting
PostPosted: 2015/11/09, 12:02 
Forum Addict
Forum Addict
User avatar

Joined: 2013/09/19, 14:31
Posts: 883
Location: Italy
Role: Hobbyist
Hi, welcome to the forums :mrgreen:

it might not be exactly what you need, and it might have some issues compiling, since it was made with Duality 1, but you can try to have a look here.

If there is something not clear, feel free to ask ^^ Good luck!

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


Top
 Profile  
 
 Post subject: Re: Bullet shooting
PostPosted: 2015/11/09, 15:56 
Newbie
Newbie

Joined: 2015/11/09, 11:38
Posts: 2
Role: Hobbyist
Thank you! :D


Top
 Profile  
 
 Post subject: Re: Bullet shooting
PostPosted: 2015/11/10, 13:54 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
Welcome! :D

Nothing to add content-wise, just moving this topic to the Programming section, as you're asking a question rather than providing a Tutorial or HowTo on your own.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
 Post subject: Re: Bullet shooting
PostPosted: 2015/12/11, 17:23 
Newbie
Newbie

Joined: 2015/12/11, 17:07
Posts: 2
Location: Belgium
Role: Hobbyist
Hi there ^^ ! Also a newbie here and before I ask my question I just want to say that this is a fantastic community and a very nice game engine, can't wait to play around more with it!

Now, my question :D : I can't seem to get the code of SirePi to work.
The initialization of bullet is giving me a hard time:

Code:
GameObject bullet = GameRes.Data.Bullet_Prefab.Res.Instantiate();


the GameRes doesn't seem to exist and I know that the .Instantiate is now .Instanciate but that is not the thing that's giving me trouble. It's just that entire line of code :P.

I checked the rest of the code to see if GameRes was a reference but it isn't and in the DDoc there is no GameRes either. I'm probably just missing something or made a stupid mistake but yeah...

I'm a bit new in C# but I'm learning and I just couldn't find the solution so I thought I'd just post it here :). Thanks!
(Sorry if I posted this in the wrong place but I thought this would be the most logical place to post it.)


Top
 Profile  
 
 Post subject: Re: Bullet shooting
PostPosted: 2015/12/11, 18:14 
Site Admin
Site Admin
User avatar

Joined: 2013/05/11, 22:30
Posts: 2073
Location: Germany
Role: Professional
Cryofear wrote:
Hi there ^^ ! Also a newbie here and before I ask my question I just want to say that this is a fantastic community and a very nice game engine, can't wait to play around more with it!

Hey there ^^ Glad to hear! :)

Cryofear wrote:
Now, my question :D : I can't seem to get the code of SirePi to work.
The initialization of bullet is giving me a hard time:

Code:
GameObject bullet = GameRes.Data.Bullet_Prefab.Res.Instantiate();

Ahh.. say no more. That's completely not your fault, it's simply an outdated approach since Duality v2.0 was released. It was viable back then, but the feature got removed since due to maintainability considerations.

You can achieve roughly the same using this:
Code:
// Load the Prefab Resource and get a reference to it
ContentRef<Prefab> prefab = ContentProvider.RequestContent<Prefab>(@"Data\Bullet.Prefab.res");
// Instantiate the Prefab to get a new GameObject
GameObject bullet = prefab.Res.Instantiate();

Or, which is a near equivalent way of doing the same thing:
Code:
// Reference the Prefab directly, telling the system "Well, I don't have the Prefab yet, but here's where you'll find it".
ContentRef<Prefab> prefab = new ContentRef<Prefab>(null, @"Data\Bullet.Prefab.res");
// Instantiate the Prefab to get a new GameObject
GameObject bullet = prefab.Res.Instantiate();

However, while fully functional, this is not the recommended approach. If you're new to Duality and just want to see some wheels turning, I'd say do it - but keep in mind that this is not ideal and know why:

  • As soon as you move or rename that Prefab, your code breaks.
  • Your Component won't be reusable with a different Prefab - not unless you change the actual source code.
  • It's also inconvenient. You'll have to find out and manually type the paths of your Resources.

A better approach would be to simply define a field / property pair in your Component which you will be able to assign in the editor by doing a dragdrop operation:
Code:
public class YourComponent : Component
{
   // This is where we'll store the reference to the Prefab
   private ContentRef<Prefab> bulletPrefab;
   
   // This is how it is exposed to other objects and, more importantly, the editor as well.
   public ContentRef<Prefab> BulletPrefab
   {
      get { return this.bulletPrefab; }
      set { this.bulletPrefab = value; }
   }
}


That way, you can specify what Prefab to use in the editor - for each object individually, if need be. Another approach would be blueprinting, but I think that's a topic for another day. ^^ For now, this should get you started.

_________________
Blog | GitHub | Twitter (@Adams_Lair)


Top
 Profile  
 
 Post subject: Re: Bullet shooting
PostPosted: 2015/12/11, 20:45 
Newbie
Newbie

Joined: 2015/12/11, 17:07
Posts: 2
Location: Belgium
Role: Hobbyist
Oooh okay, I thought that I was making a huge mistake somewhere :P Thanks, this does indeed work! And yes indeed, I think I'll stay away from the more advanced stuff until I can properly make that tutorial ship do what I want ^^


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

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


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:  
Powered by phpBB® Forum Software © phpBB Group