Jump to content

Problem creating a Stuka siren plugin


Recommended Posts

Hi all !

After playing KSP for a while and using a couple of mods, i decided to try and create my own. I would like to create a Stuka-like-siren part, which make an increasingly higher sound depending of the speed, when activated. I don't want the sound to be played when my custom part is not attached to the vessel, and i don't want all parts to play the sound. I have a basic undersanding in object oriented programming, and i'm fairly new to c#.

My problem is, i didn't find the way to get the vessel info "srfSpeed" (see: http://wiki.kerbalspaceprogram.com/wiki/API:Vessel#Fields).

public class ModuleStukaSiren : PartModule
{
	double vspeed = vessel.srfspeed;
}

I get this error : A field initializer cannot reference the nonstatic field, method, or property...

As far as i know/understood, i can't set that class as static, and at this point i don't even know if i can access the class "vessel" through PartModule (whereas Documentation for the KSP API let me think i should), or even if the class "vessel" needs to be used. I spent the afternoon on c#, KSP API, KSP plugins related forums, without conclusive result.

So, is it possible to access the vessel info through a plugin for a module i will attach on a specific part, and how ? Does someone have in mind some simple tutorial or mod source which can help or inspire me ?

My apologies if that question is dumb and only c# related, and sorry for my bad english.

Thanks :)

Roiarg

 

Link to comment
Share on other sites

The problem is that you're trying to initialise a value with something that isn't a constant. What you really want to do is constantly set the value of vSpeed to match the vessel speed, so you need to do that inside one of the functions that are called regularly by Unity (in this case, Update)

public class ModuleStukaSiren : PartModule
{
	double vspeed;
	
	public void Update()
	{
		vSpeed = vessel.srfSpeed
	}
}

 

Edited by Crzyrndm
Link to comment
Share on other sites

Thank you Crzyrndm !

I managed to play a sound, i'm now looking for how to make the part generates the sound (like engines), as i'm currently using SoundManager from Nifty255's youtube tutorials (which seems better for alarm-like sounds). I'm reading sources from Sound Overhaul Project.

Link to comment
Share on other sites

  • 1 month later...

You can do it much easier by creating custom parts and using ModuleEngineFX, then you don't need to build a plugin for it or use mods. ModuleEngineFX is incredibly versatile. I've made a mod called "Sirens!" myself, it's not on SpaceDock yet after the demise of KerbalStuff. Here's a video.

 

 

 

The sirens are essentially rocket engines which produce a minscule amount of thrust. The sirens change pitch based on throttle setting.

2 hours ago, DerpyFirework said:

ModuleEnginesFX allows both a powerEffectName (Linked to actual thrust) and runningEffectName (Linked to throttle).

Use powerEffectName and let the siren/engine run on a new resource, adapt an air intake to take that resource and adjust intake area, base speed and engine consumption, and set min/max thrust to the same value. This should work and is probably easier than using C#.

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

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.

×
×
  • Create New...