Flexible spell GUI

I have dared to step into the world of GUI!
This is an attempt of making a flexible GUI system, in which you can drag spells from one spot to another. Then from that spot, you can click the button and you would cast the spell that you dragged into that spot.

Here’s the script called “testScript”

enum SpellSlots {
	inActive,
	slot1,
	slot2,
	slot3
}

//static var doScript : boolean = false;

var _state1 = SpellSlots.inActive;
var _state2 = SpellSlots.inActive;
var _state3 = SpellSlots.inActive;

var bool1 = true;
var bool2 = true;
var bool3 = true;

var clickedButton = false;
static var targetSpell : String;
static var spellName : String;
private var spellSlot : SpellSlots;



function OnGUI () {
	// For combat use
	if(bool1){
		if(GUI.Button(Rect (100, 10, 50, 50), "")){
			if (clickedButton){
				_state1 = SpellSlots.slot1;
				bool1 = false;
				clickedButton = false;
			}
		}
	}
	
	if(bool2){
		if(GUI.Button(Rect (100, 70, 50, 50), "")){
			if (clickedButton){
				_state2 = SpellSlots.slot2;
				bool2 = false;
				clickedButton = false;
			}
		}
	}
	
	if (bool3){
		if(GUI.Button(Rect (100, 130, 50, 50), "")){
			if (clickedButton){
				_state3 = SpellSlots.slot3;
				bool3 = false;
				clickedButton = false;
			}
		}
	}

	
	// For test use
	
	if(GUI.Button(Rect (20, 20, 50, 50), spellName)){
		clickedButton = true;
	}
	
	
	if(bool1 == false){
		if(GUI.Button(Rect (100, 10, 50, 50), spellName)){
			Debug.Log("Clicked button 1");
		}
	}
	
	if(bool2 == false){
		if(GUI.Button(Rect (100, 70, 50, 50), spellName)){
			Debug.Log("Clicked button 2");
		}
	}
	
	if(bool3 == false){
		if(GUI.Button(Rect (100, 130, 50, 50), spellName)){
			Debug.Log("Clicked button 3");
		}
	}
}

// When a state is activated
function Activated () {
	spellScript.doScript = true;
}	


function Update() {
	if(clickedButton){
		clickedButton = true;
	}
	
	if (_state1 == SpellSlots.slot1){
		Activated();
	}
	
	else if(_state2 == SpellSlots.slot2){
		Activated();
	}
	
	else if(_state3 == SpellSlots.slot3){
		Activated();
	}
}

And here’s the script called “spellScript”

var spellName = "";
private var thisObject;
//private var thisScript;

static var doScript : boolean = false;
var callScript = "";

function Update() {
	testScript.spellName = spellName;
	//testScript.targetSpell = thisScript;
	
	thisObject = this.gameObject;
	//thisScript = this;
	
	if(doScript == true){
		GetComponent(callScript).enabled = true;
	}
}

I am looking for some kind of way to make testScript identify how many versions of spellScripts there are. So that there can’t only be one spell available. And I also want the option to have different spells (scripts and models) assigned to one game object. Besides that, I don’t know how to make rows of buttons without having to repeat myself, or make a new button next to another when f.ex. you get a new spell.

I hope I’ve been specific enough and that you guys wanna help me :slight_smile:

Edit: It would be nice if you could give me some tips on this or maybe show me where I can learn to do this - I am very new on this subject so I can’t read and understand other GUI scripts if they’re advanced.

Ok so far I’ve managed to find a solution to the “have-more-spells-in-one-object” problem :stuck_out_tongue: using “thisScript = this;” and “testScript.targetScript = thisScript;”. I am still having some problems, but in case anyone is reading this I will be posting my code later or tomorrow.

Why does no one wanna help me with this?

Hmm, for automatic layout, you should try using GUILayout instead of GUI.

To make column and / or rows of button, try using GUILayout.BeginHorizontal and BeginVertical. Have a quick look there : Unity - Manual: IMGUI Layout Modes

Thanks, AkilaeTribe, I’ll take a look when I get home :smile:

Right now I’m looking for a way to define a script from a number f.ex.

I have edited the “spellScript” so that it uses the “this” syntax, and sends that to the “testScript”. So now it is irrelevant what the script is called, basically. I can not access the script right now because I’m not home.

My “spellScript”, for example, could have the number 1, and then I could make another script called “fireBall” which had the number 2 (I am also making a dropdown menu which adds all the new spells I make). If this is possible, then if you click a name, f.ex. fireBall from the dropdown, and then click a button so it has that spell in it, then if you click it again, it hurls a fireball. And the same if you click spellScript, which would make it more flexible. Can you see what I mean?

I’d really appreciate some feedback, maybe pointing me in the right direction :slight_smile:

Ok I can post my scripts if it is any use, please take a look at it, because I really wanna get done with it :slight_smile:

Here’s the one called “spellScript”:

// Name of this spell
var spellName = "";
// The minimum damage this spell should deal (0 = nothing)
var minDamage : int;
// the maximum damage this spell should deal (0 = nothing(wouldn't recommend it))
var maxDamage : int;
// The minimum healing this spell should deal (0 = nothing)
var minHealing : int;
// The maximum healing this spell should deal (0 = nothing(still wouldn't reccomend it))
var maxHealing : int;


static var spellBallCollided : boolean = false;
private var damageDealt : int;

// 
var spellBall : Rigidbody;

private var thisObject;
private var thisScript;

private var shootSpell;
static var doScript : boolean = false;
//var callScript = "";

function Update() {
	if(spellBallCollided){
		ApplyDamage();
	}
	
	testScript.spellName = spellName;
	testScript.targetScript = thisScript;
	
	thisObject = this.gameObject;
	thisScript = this;
	
	if(doScript == true){
		shootSpell = Instantiate(spellBall, transform.position, Quaternion.identity);
		shootSpell.velocity = transform.TransformDirection(Vector3.forward * 100);
		doScript = false;
	}
}

function ApplyDamage(){
	damageDealt = Random.Range(minDamage, maxDamage);
	Debug.Log(damageDealt);
	Destroy(shootSpell.gameObject);
	spellBallCollided = false;
}

And here’s the one called “testScript”:

enum SpellSlots {
	inActive,
	slot1,
	slot2,
	slot3
}

//static var doScript : boolean = false;

var _state1 = SpellSlots.inActive;
var _state2 = SpellSlots.inActive;
var _state3 = SpellSlots.inActive;

static var dropdownString : String [] = [];
var bool1 = true;
var bool2 = true;
var bool3 = true;

var clickedButton = false;
static var targetScript;
static var spellName : String;
private var spellSlot : SpellSlots;



function OnGUI () {
	// For combat use
	if(bool1){
		if(GUI.Button(Rect (100, 10, 50, 50), "")){
			if (clickedButton){
				_state1 = SpellSlots.slot1;
				bool1 = false;
				clickedButton = false;
			}
		}
	}
	
	if(bool2){
		if(GUI.Button(Rect (100, 70, 50, 50), "")){
			if (clickedButton){
				_state2 = SpellSlots.slot2;
				bool2 = false;
				clickedButton = false;
			}
		}
	}
	
	if (bool3){
		if(GUI.Button(Rect (100, 130, 50, 50), "")){
			if (clickedButton){
				_state3 = SpellSlots.slot3;
				bool3 = false;
				clickedButton = false;
			}
		}
	}

	
	// For test use
	
	if(GUI.Button(Rect (20, 20, 50, 50), spellName)){
		clickedButton = true;
	}
	
	
	if(bool1 == false){
		if(GUI.Button(Rect (100, 10, 50, 50), spellName)){
			Activated();
		}
	}
	
	if(bool2 == false){
		if(GUI.Button(Rect (100, 70, 50, 50), spellName)){
			Activated();
		}
	}
	
	if(bool3 == false){
		if(GUI.Button(Rect (100, 130, 50, 50), spellName)){
			Activated();
		}
	}
}

// When a state is activated
function Activated () {
	targetScript.doScript = true;
}	


function Update() {
	if(clickedButton){
		clickedButton = true;
	}
	
	if (_state1 == SpellSlots.slot1){
		//Activated();
	}
	
	else if(_state2 == SpellSlots.slot2){
		//Activated();
	}
	
	else if(_state3 == SpellSlots.slot3){
		//Activated();
	}
}

When I put testScript on a camera and launch, Unity crashes.

What? That might be why my program crashes all the time… But it shouldn’t cause this?

The end of the FPS make my Unity crashes too, so, it is not impossible. :slight_smile:

That’s kinda weird, do you know how to fix this?

That’s kinda weird, do you know how to fix this?

Ok so far I’ve managed to find a solution to the “have-more-spells-in-one-object” problem using “thisScript = this;” and “testScript.targetScript = thisScript;”. I am still having some problems, but in case anyone is reading this I will be posting my code later or tomorrow.

Lol thats my old post :S Does no one know how I can fix this crash problem?