Jump to content

C# help Me with my new mod please.


Recommended Posts

using UnityEngine;

namespace Constructive_Criticism
{
public class ConstructiveCritcism : PartModule
{
public void EventData<Vessel>onFlagPlant();
{
public static void main();
{
string[] msgsflagplace = new string[3];
msgsflagplace[0] = "Congratulations, you have placed a flag!";
msgsflagplace[1] = "Now give yourself a pat on the back. you did good.";
msgsflagplace[2] = "A new planet with a flag on it,jolly good job.";
msgsflagplace[3] = "A new planet conquered!";
Random rnd = new Random();
ScreenMessages.PostScreenMessage(msgsflagplace[rnd.Next(0,2)], 3f, ScreenMessageStyle.UPPER_CENTER);
}
}
}
}

Im am trying to make it so that when a flag is planted or any other event, a witty comment is randomly selected from a list and shown as a screen message. I have little to no knowledge of c# so don't throw out any c# terms when explaining things. There were 28 errors in vs. I just need someone to edit the code so I can duplicate it for the other game events. credit will be given upon release don't worry.

Thanks

Ryan

Link to comment
Share on other sites

1) You should be using a KSPAddon type plugin for this. A partModule plugin would have to be attached to either the kerbal or flag which would require another script anyway. See here for a very simple addon type plugin

2) You have a function (main) inside another function, that's never going to work

3) You don't put semicolons on function declaration lines for actual functions (there are times when you do, but thats not anywhere near here)

4) You need to register your flagplant function inside either Start() or Awake() and deregister it in OnDestroy() so that it actually gets called

GameEvents.onFlagPlant.Add(main); // called in Start or Awake
GameEvents.onFlagPlant.Remove(main); // called in OnDestroy

and main will need to take a parameter of type vessel to be eligible for the event registration

public void main(Vessel v)

There's a few more problems but that should cover most of it. I would strongly recommend that you read up a bit on C# syntax, particularly functions, if you're going to be continuing with this.

Edited by Crzyrndm
Link to comment
Share on other sites

I thought I'd at least try so here is what I came up with, but what would I do for the other events like "OnCollision".

[/FONT]using UnityEngine;

namespace Constructive_Criticism
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class ConstructiveCritcism : MonoBehavior
{
public void Start()
{
GameEvents.onFlagPlant.Add(main);
}
public void OnDestroy()
{
GameEvents.onFlagPlant.Remove(main);
}
public void main(Vessel v);
{
string[] msgsflagplace = new string[3];
msgsflagplace[0] = "Congratulations, you have placed a flag!";
msgsflagplace[1] = "Now give yourself a pat on the back. you did good.";
msgsflagplace[2] = "A new planet with a flag on it,jolly good job.";
msgsflagplace[3] = "A new planet conquered!";
Random rnd = new Random();
ScreenMessages.PostScreenMessage(msgsflagplace[rnd.Next(0,2)], 3f, ScreenMessageStyle.UPPER_CENTER);
}
}
}

thanks again

Link to comment
Share on other sites

On collision is essentially the same, except the argument for the function is of type EventReport instead of type Vessel.

GameEvents.onCollision.Add(collide);

void collide (EventReport r)
{ }

You can tell which arguments are required by looking at the type of onCollision and seeing that it's "EventData<EventReport>" (compared to flagPlant which was EventData<Vessel>)

Link to comment
Share on other sites

So did I put it together correctly

- - - Updated - - -

What I am worried about is the screen message get random string from list code, did I do that correctly, I mean you just never mentioned that part.

- - - Updated - - -

public void Start()        {            
GameEvents.onFlagPlant.Add(main);
GameEvents.onCollision.Add(collide);
}
public void OnDestroy()
{
GameEvents.onFlagPlant.Remove(main);
GameEvents.onCollision.Remove(collide);
}

here is was I did to add the new event.

Link to comment
Share on other sites

How do I add more then

You have declared an array of length 3 (new string[3];) so the fourth entry (with index 3) is going to generate an index out of range exception. If you make the array length 4, index 3 is a valid position

and will my event add code work by adding that line to start()

Yup, just list them all out in Start and OnDestroy

Are you actually testing this yet? It would be faster to check this yourself instead of asking every time ;)

Link to comment
Share on other sites

Hey crzyrndm, could you test my plugin for me before I get too far into it, I just don't want to have to change what could possible be 1000 lines of code.

using UnityEngine;

namespace Constructive_Criticism
{
[KSPAddon(KSPAddon.Startup.Flight, false)]
public class ConstructiveCritcism : MonoBehavior
{
public void Start()//initialize event happened checker
{
GameEvents.onFlagPlant.Add(main);
GameEvents.onCollision.Add(collide);
GameEvents.OnLaunch.Add(launch);
GameEvents.OnOverheat.Add(overheat);
GameEvents.OnGUIAstronautComplexSpawn.Add(acomplex);
}
public void OnDestroy()
{
GameEvents.onFlagPlant.Remove(main);
GameEvents.onCollision.Remove(collide);
GameEvents.OnLaunch.Remove(launch);
GameEvents.OnOverheat.Remove(overheat);
GameEvents.OnGUIAstronautComplexSpawn.Remove(acomplex);
}
public void main(Vessel v);
{
string[] msgsflagplace = new string[4];// messages on flag plant event
msgsflagplace[0] = "Congratulations, you have placed a flag!";
msgsflagplace[1] = "Now give yourself a pat on the back. you did good.";
msgsflagplace[2] = "A new planet with a flag on it,jolly good job.";
msgsflagplace[3] = "A new planet conquered!";
Random rnd = new Random();
ScreenMessages.PostScreenMessage(msgsflagplace[rnd.Next(0,2)], 3f, ScreenMessageStyle.UPPER_CENTER);
}
public void collide (EventReport r)
{
string[] msgscol = new string[5];// messages to display if you crash
msgscol[0] = "Nice, you scratched the paint";
msgscol[1] = "Be carefull, there is no insurance on this craft.";
msgscol[2] = "Do you have any idea how expensive rockets are?!?!?!";
msgscol[3] = "That'll leave a mark.";
msgscol[4] = "you have crashed. Jeb will be proud.";
Random rnd = new Random();
ScreenMessages.PostScreenMessage(msgscol[rnd.Next(0,4)], 3f, ScreenMessageStyle.UPPER_CENTER);
}
public void launch (EventReport l)
{
ScreenMessages.PostScreenMessage("Liftoff!", 2f, ScreenMessageStyle.UPPER_CENTER);
}
public void overheat (EventReport oh)
{
string[] msgsoh = new string[5];// messages to display if a part overheats
msgsoh[0] = "you just voided the warranty.";
msgsoh[1] = "You just lost a few parts.... no biggie.";
msgsoh[2] = "Feel free to panic.";
msgsoh[3] = "There is a little red bar next to the fuel bar, use it!";
msgsoh[4] = "Tip: If your engine nozzels start turning into cherries. throttle down.";
Random rnd = new Random();
ScreenMessages.PostScreenMessage(msgscol[rnd.Next(0,4)], 3f, ScreenMessageStyle.UPPER_CENTER);
}
public void acomplex (EventVoid ac)
{
ScreenMessages.PostScreenMessage("Liftoff!", 2f, ScreenMessageStyle.UPPER_CENTER);
}
}
}

I hope this isn't too much to ask.

thanks so much.

- - - Updated - - -

I'm not gonna be back from vacation til next week

- - - Updated - - -

Hey crzyrndm, could you test my plugin for me before I get too far into it, I just don't want to have to change what could possible be 1000 lines of code.

using UnityEngine;[/FONT][FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]
[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]namespace Constructive_Criticism[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]{[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] [KSPAddon(KSPAddon.Startup.Flight, false)][/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] public class ConstructiveCritcism : MonoBehavior[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] {[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] public void Start()//initialize event happened checker[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] { [/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.onFlagPlant.Add(main);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.onCollision.Add(collide);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.OnLaunch.Add(launch);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.OnOverheat.Add(overheat);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.OnGUIAstronautComplexSpawn.Add(acomplex);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] }[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] public void OnDestroy()[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] {[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.onFlagPlant.Remove(main);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.onCollision.Remove(collide);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.OnLaunch.Remove(launch);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.OnOverheat.Remove(overheat);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] GameEvents.OnGUIAstronautComplexSpawn.Remove(acomplex);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] } [/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] public void main(Vessel v);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] {[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] string[] msgsflagplace = new string[4];// messages on flag plant event[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgsflagplace[0] = "Congratulations, you have placed a flag!";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgsflagplace[1] = "Now give yourself a pat on the back. you did good.";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgsflagplace[2] = "A new planet with a flag on it,jolly good job.";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgsflagplace[3] = "A new planet conquered!";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] Random rnd = new Random();[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] ScreenMessages.PostScreenMessage(msgsflagplace[rnd.Next(0,2)], 3f, ScreenMessageStyle.UPPER_CENTER);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] }[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] public void collide (EventReport r)[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] {[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] string[] msgscol = new string[5];// messages to display if you crash[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgscol[0] = "Nice, you scratched the paint";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgscol[1] = "Be carefull, there is no insurance on this craft.";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgscol[2] = "Do you have any idea how expensive rockets are?!?!?!";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgscol[3] = "That'll leave a mark.";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgscol[4] = "you have crashed. Jeb will be proud.";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] Random rnd = new Random();[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] ScreenMessages.PostScreenMessage(msgscol[rnd.Next(0,4)], 3f, ScreenMessageStyle.UPPER_CENTER);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] }[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] public void launch (EventReport l)[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] {[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] ScreenMessages.PostScreenMessage("Liftoff!", 2f, ScreenMessageStyle.UPPER_CENTER);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] }[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] public void overheat (EventReport oh)[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] {[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] string[] msgsoh = new string[5];// messages to display if a part overheats[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgsoh[0] = "you just voided the warranty.";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgsoh[1] = "You just lost a few parts.... no biggie.";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgsoh[2] = "Feel free to panic.";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgsoh[3] = "There is a little red bar next to the fuel bar, use it!";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] msgsoh[4] = "Tip: If your engine nozzels start turning into cherries. throttle down.";[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] Random rnd = new Random();[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] ScreenMessages.PostScreenMessage(msgscol[rnd.Next(0,4)], 3f, ScreenMessageStyle.UPPER_CENTER);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] }[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] public void acomplex (EventVoid ac)[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] {[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] ScreenMessages.PostScreenMessage("Liftoff!", 2f, ScreenMessageStyle.UPPER_CENTER);[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] } [/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif] }[/FONT]
[FONT=Verdana, Arial, Tahoma, Calibri, Geneva, sans-serif]}

I hope this isn't too much to ask.

thanks so much.

- - - Updated - - -

I just posted that twice, stupid me, thought it didn't go through.

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