- Home /
Spaceship game, Pick weapon
Hello! I am making a 2D spaceship shooter game, seen from above, kinda like the old meteor game. I am trying to make a menu where you can equip your ship with weapons of your own choice. I am having problems with trying to move the picked weapon (a projectile gameobject) from my menu script to my fire script. I am using arrays in my both scripts but I get errors here and there. Any help would be much appreciated! Here are the scripts:
private var playerShip : GameObject;
private var weaponTypes : int[];
public var plasma : GameObject;
public var bluePlasma : GameObject;
private var pekare : int = 0;
private var widthe : int = 200;
private var heighte : int = 80;
private var placeEquip : Rect = new Rect(widthe/4, heighte/2, widthe, heighte * 8);
private var spot1 : Rect;
private var spot2 : Rect;
private var spot3 : Rect;
private var spot4 : Rect;
private var spot5 : Rect;
private var spot6 : Rect;
private var spot7 : Rect;
private var spot8 : Rect;
private var spot9 : Rect;
private var placeUnequip : Rect = new Rect(widthe/4 + widthe + 5, heighte/2, widthe, heighte * 8);
private var plasmaEquipped : boolean = false;
private var bluePlasmaEquipped : boolean = false;
private var redPlasmaEquipped : boolean = false;
private var greenPlasmaEquipped : boolean = false;
function Start () {
playerShip = GameObject.FindGameObjectWithTag("ThePlayer").gameObject;
spot1 = new Rect(10, 20, 100, 20);
spot2 = new Rect(10, 45, 100, 20);
spot3 = new Rect(10, 70, 100, 20);
spot4 = new Rect(10, 95, 100, 20);
spot5 = new Rect(10, 115, 100, 20);
spot6 = new Rect(10, 135, 100, 20);
spot7 = new Rect(10, 150, 100, 20);
spot8 = new Rect(10, 175, 100, 20);
spot9 = new Rect(10, 120, 100, 20);
}
function OnGUI(){
if(GUI.Button(Rect(Screen.width/2, Screen.height/2, 100, 100), "Send")){
for(var i = 0; weaponTypes[i] != null; i++){
playerShip.transform.FindChild("Weapons").gameObject.GetComponent(FireScript).receiveValue(weaponTypes[i], i, 0.4, 900);
}
}
placeEquip = GUI.Window (0, placeEquip, EquipFunction, "Weapons Avaible");
placeUnequip = GUI.Window(1, placeUnequip, UnEquipFunction, "Weapons Equipped");
}
function EquipFunction(windowID : int){
if(plasmaEquipped == false){
if (GUI.Button (spot1, "Plasma")){
weaponTypes[pekare] = 1;
pekare++;
plasmaEquipped = true;
}
}
if(bluePlasmaEquipped == false){
if (GUI.Button (spot2, "Blue Plasma")){
weaponTypes[pekare] = 2;
pekare++;
bluePlasmaEquipped = true;
}
}
}
function UnEquipFunction(windowID : int){
if(plasmaEquipped == true){
if (GUI.Button (spot1, "Plasma")){
plasmaEquipped = false;
}
}
if(bluePlasmaEquipped == true){
if (GUI.Button (spot2, "Blue Plasma")){
bluePlasmaEquipped = false;
}
}
}
Shoot script:
#pragma strict
public var slots : GameObject[];
private var weapon : GameObject[];
private var forceProj : float[];
private var reloadTime : float[];
private var timerRel : float[];
/**
public var plasmaProj : GameObject;
public var bluePlasmaProj : GameObject;
**/
public var plasma : GameObject;
public var bluePlasma : GameObject;
//private var slot1Wep : GameObject;
//public var slot1Reload : float = 1;
private var timer : float;
/**
public var force1 : float;
public var force2 : float;
public var force3 : float;
public var rel1 : float;
public var rel2 : float;
public var rel3 : float;
private var timerRel1 : float;
private var timerRel2 : float;
private var timerRel3 : float;
**/
function Start () {
//timerRel = slotsReloadTime;
timerRel[0] = 0;
}
function Update () {
//shoot part
//slot1 weapon
if(Input.GetKey(KeyCode.Space)){
var arr = slots;
for(var i = 0; i < arr.Length; i++){
if(timerRel[i] < Time.time){
timerRel[i] = 0;
var copyPar = Instantiate(weapon[i], slots[i].transform.position, this.transform.rotation);
copyPar.rigidbody.velocity = this.transform.forward * forceProj[i];
Destroy(copyPar, 5);
timer = Time.time + 0.2;
timerRel[i] = Time.time + reloadTime[i];
}
}
}
}
function receiveValue(projectileType : int, spotPut : int, rel : float, force : float){
if(projectileType == 1){
weapon[spotPut] = plasma;
}
else if(projectileType == 2){
weapon[spotPut] = bluePlasma;
}
reloadTime[spotPut] = rel;
forceProj[spotPut] = force;
}
Sorry for unused variables and such which might make it harder to read.
TL;DR.
You should be a lot more specific about what errors you're getting and just post the relevant part of the script that is generating those errors.
Answer by ThatsAMorais · Dec 17, 2013 at 09:20 PM
I see what you're trying to do, I think. You mentioned some problems and errors you were having. Comment to this with the types of errors you're seeing, exactly.
Here's what I gather... You have a GUI button and two windows displaying some options. I'm a little confused on the "Send" button because it looks like you call receiveValue for each of the weapon choices when it is pressed.
Nonetheless, my answer to your issue relates to the two "weapons" arrays. The weapons array in "Shoot" only serves the function of storage of a set of GameObjects, but a better alternative would be a data-structure that allows you to index that set of object instances easily. So, instead of weapons : GameObject[], I'd use a Dictionary. For example
// declaration:
var myDictionary = new Dictionary.<KeyType,ValueType>();
// and a real-world declaration example (where 'Person' is a custom class):
var myContacts = new Dictionary.<string,Person>();
// insert or change the value for the given key
myDictionary[anyKey] = newValue;
// retrieve a value for the given key
var thisValue = myDictionary[theKey];
// get the number of items in the Hashtable
var howBig = myDictionary.Count;
// remove the key & value pair from the Hashtable, for the given key.
myDictionary.Remove(theKey);
With this data-structure, you still have a set of GameObjects, but you can reference them according to their name. For example, in "Shoot" you would initialize the Dictionary like this.
function Start()
{
myDictionary["Plasma"] = plasma;
myDictionary["Blue Plasma"] = bluePlasma
}
So, now that you can access the weapon objects using a string, you should use these strings in your other script with OnGUI() to make selections. For example...
var weapons : List.<string>();
...
function Start()
{
weapons.Add("Plasma");
weapons.Add("Blue Plasma");
}
...
for(var weaponName:String in weapons){
if(GUI.Button(Rect(Screen.width/2, Screen.height/2, 100, 100), weaponName)){
playerShip.transform.FindChild("Weapons").gameObject.GetComponent(FireScript).receiveValue(weaponName, 0.4, 900);
}
}
You've got to work out one other issue with which one of these two scripts will "contain" the weapons data. In other words, You don't want weapon-name strings defined in one script that may or may not correspond to the keys of a dictionary of another script.
Get back to me if you need other help, but I think you probably just have to get some of this basic stuff figured out. Figure out how to use this language effectively. Learn about encapsulation a little because you're probably going to want to have the weapon types defined in one place, and just pass the float value to the "Shoot" script on whatever weapon object is firing it.
That seems like a really great way to solve this! Never worked with dictionary before but will definetly try my best using it, thanks alot! :) The send was just like a test bottom so that I could send over my weapons choice over to the firingscript and see if it received it.
Dictionary is a great way to store enumerated data that is not in a specific order. It grows dynamically (although how it is implemented will ultimately deter$$anonymous$$e the cost of this). Grants constant time access to elements. The "weapons["weaponName"].value" syntax is very readable, better than "weaponArray[0]". An alternative to String type keys are enumerations.
I am interested in "answering" this question, so what are the errors you're seeing? Does this question have an answer?
Sorry been very busy, but I think I afterall won't use this in my game. I do however have a script that fires between an array off weapons but it just won't be choose-able in the game. Here is the firing script if anyone is interrested, pretty simple but it satisfy my need atm :)
public var slots : GameObject[];
public var weapon : GameObject[];
/**
public var plasma : GameObject;
public var bluePlasma : GameObject;
public var redPlasma : GameObject;
public var greenPlasma : GameObject;
**/
//private var slot1Wep : GameObject;
//public var slot1Reload : float = 1;
private var timer : float;
/**
public var force1 : float;
public var force2 : float;
public var force3 : float;
public var rel1 : float;
public var rel2 : float;
public var rel3 : float;
private var timerRel1 : float;
private var timerRel2 : float;
private var timerRel3 : float;
**/
public var force : float[];
public var rel : float[];
public var timerRel : float[];
private var wepShot : int = 0;
private var arrLength : int;
function Start () {
//timerRel = slotsReloadTime;
//timerRel[0] = 0;
//var arr = weapon;
arrLength = weapon.Length;
Debug.Log(arrLength.ToString());
for(var x = 0; x < arrLength; x++){
timerRel[x] = 0.0;
}
}
function Update () {
//shoot part
//slot1 weapon
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)){
//var arr = slots;
for(var i = 0; i < arrLength; i++){
if(timerRel[i] < Time.time){
//timerRel[i] = 0;
var copyPar = Instantiate(weapon[i], slots[i].transform.position, this.transform.rotation);
copyPar.rigidbody.velocity = this.transform.forward * force[i];
Destroy(copyPar, 5);
timerRel[i] = Time.time + 0.05;
timerRel[i] = Time.time + rel[i];
}
//}
}
}
}
Thanks anyways for trying to help :)
Your answer
Follow this Question
Related Questions
How to destroy all the items with one function? 2 Answers
Passing an array to a function by reference? 1 Answer
Passing an Array 1 Answer