14 Replies - 3866 Views - Last Post: 13 August 2013 - 05:35 PM

#1 pillarleg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-August 13

Using a value as a variable (really need help)

Posted 02 August 2013 - 10:34 AM

I've got a quick question that I've been trying to figure out for days now.

I'm running flash professional MX 2004.

How do you use the value of a textbox as a variable on the left side of an operand/operator/operation?

Some examples of what I need it for, keep in mind these codes aren't even close to correct and I know it, I simply don't know how to go about coding this.

For instance if I had two texboxes(input and startingOutput), and 2 buttons (changeBtn and addBtn), how could I make this code work:

stop();
input = "";
startingOutput = "";

changeBtn.onRelease = function(){
	 startingoutput = input; //this is what I can't figure out, I want to CHANGE the variable startingoutput intto whatever input's textbox is
}
// in this example I'm hoping to type "desiredOutput" into "input", then click the changeBtn, changing "startingOutput" into "desiredOutput", then click addBtn, causing desiredOutput to become 2.
addBtn.onRelease = function(){
	desiredOutput = 2;
}



I'm programming something and it would be infinitely useful to know how to do.

if I had a dynamic textbox named "bob", a dynamic textbox named "player", and a button named 'deusExMachinaBtn' on my screen how would I get this code to work?

stop();
bob = 0;
player = "bob";

deusExMachinaBtn.onRelease = function(){
	player += 1; //this is what I can't figure out, I want to use the value of the variable player (which is currently bob) as the variable in this line, adding 1 to bob.
}



If you could tell me how to do either of these things, it would save me so much time and make my program both smaller and more efficient.

I've been trying things and researching for days on this, I've asked on 4 different sites over the last few days and none of them have even answered me. I'm really stuck, any input would be nice.

Is This A Good Question/Topic? 0
  • +

Replies To: Using a value as a variable (really need help)

#2 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6233
  • View blog
  • Posts: 10,782
  • Joined: 28-September 07

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 11:17 AM

player += 1



This piece of code is saying

Quote

player = player + 1


But 1 is a numeric value while player is set to a string value.

So what you're REALLY saying is

Quote

player = "bob"+1


Your variable logic is flawed.

You can't perform a mathematical operation on character string.
Was This Post Helpful? 1
  • +
  • -

#3 pillarleg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-August 13

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 11:34 AM

View PostBenignDesign, on 02 August 2013 - 11:17 AM, said:

player += 1



This piece of code is saying

Quote

player = player + 1


But 1 is a numeric value while player is set to a string value.

So what you're REALLY saying is

Quote

player = "bob"+1


Your variable logic is flawed.

You can't perform a mathematical operation on character string.



Yes, that's true... perhaps I'm not being descriptive enough.
maybe this is describing it better)

with the "=" operand, the left side is always the variable, and the right is always the value it's getting.
What I want to do is:

Value of player (which is bob) += (gets plus) 1;.

In other words I want to use the value of one variable, AS a variable in an operation.
Was This Post Helpful? 0
  • +
  • -

#4 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6233
  • View blog
  • Posts: 10,782
  • Joined: 28-September 07

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 12:07 PM

Exactly. Your logic is flawed.

Instead of adding 1 to the VARIABLE bob, you're adding one to player which is set to the STRING value "bob".

I'm making the assumption that you're attempting to designate some dude named Bob as the active player... and then increment his score. If I'm incorrect, please set me straight on this one.

You need one variable for setting the active player...
player="bob"


And another variable for setting the active player's score...
bob=0


So when incrementing...
bob+=1


This should add 1 to Bob's score without attempting to perform addition on a string variable.

I understand what you're attempting. You want to use the string "bob" to designate the variable to be incremented.

In pseudo-code to get my logic right:

Player name = 0
Player = "Player name"
Value of Player = Value of Player + 1

I think I'd do something a little more defining with the variables...

score_bob = 0
player = "bob"
score_ +player += 1
Was This Post Helpful? 1
  • +
  • -

#5 pillarleg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-August 13

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 12:22 PM

View PostBenignDesign, on 02 August 2013 - 12:07 PM, said:

I think I'd do something a little more defining with the variables...

score_bob = 0
player = "bob"
score_ +player += 1


This looks like it'd do exactly what I needed it to, but when I tried to run it, I got this error:

**Error** Scene=Scene 1, layer=Layer 1, frame=1:Line 5: Left side of assignment operator must be variable or property.
score_ + player += 1;

Total ActionScript Errors: 1 Reported Errors: 1
Was This Post Helpful? 0
  • +
  • -

#6 pillarleg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-August 13

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 12:28 PM

ps. I am really glad I'm getting a response, been stuck on this for ages.
Was This Post Helpful? 0
  • +
  • -

#7 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6233
  • View blog
  • Posts: 10,782
  • Joined: 28-September 07

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 12:59 PM

I've done this in PHP, never in Flash... so I'm not entirely sure of the concatenation syntax. Look up variable concatenation and see if I missed something in my attempt to append score_ and the variable "player".

I'm at work at the moment and am short on time to dig into this further right now. I'll check back this evening and see what you've come up with.
Was This Post Helpful? 0
  • +
  • -

#8 pillarleg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-August 13

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 06:45 PM

been searching since noon... nothing related to that brings up anything useful. or maybe it is and I'm just not understanding it, most of what pops up on the searches look irrelevant, what is relevant usually turns out to be something completely different. I really hope you can clear things up for me. if it helps, I can specify why it's so important I need to know how to do this.

There are many applicable reasons for wanting to know how to do this, the example I just showed you was one. I know that on the code I sent you, player always got the value bob, because:
 
bob = 0;
player = "bob"; 



but what if instead of that, I had 3 additional buttons, coded like this:

bob = 0;
susan = 0;
mary = 0;

bobBtn.onRelease = function(){
	player = "bob";
}

maryBtn.onRelease = function(){
	player = "mary";
}

susanBtn.onRelease = function(){
	player = "susan";
}



Then, I could click a button and player's value would become whatever I clicked.
Then, when I clicked deusExMachinaBtn, whatever player's button you clicked would add += 2 to their value.

that is if my code worked, which it doesn't unfortunately.

Knowing how to use the value of a variable on the left side of "=" (or inserting the value of a variable into a string, or variable concatenation, or dynamic variables or whatever you want to call it) would open a lot of doors for me, and there's many things in the game that I'm programming that would benefit from it, and a couple features I don't think I can add without knowing how to first do this.

Knowing how to do what I describe my goal to be in this example would be just as useful.

stop();  
input = "";  
startingOutput = "";      
changeBtn.onRelease = function(){  
      startingoutput = input; //this is what I can't figure out, I want to CHANGE the variable startingoutput intto whatever input's textbox is  
}  
// in this example I'm hoping to type "desiredOutput" into "input", then click the changeBtn, changing "startingOutput" into "desiredOutput", then click addBtn, causing desiredOutput to become 2.  
addBtn.onRelease = function(){  
desiredOutput = 2;  
} 



I've been searching 3 nearly non-stop (I'm an insomniac) days for the correct way to do either of these things, all leading to dead ends or things that just plain don't work. I've posted on multiple flash and actionscript help sites and you guys are the first to even answer me. The help documentation on my flash and on the adobe website were downright useless. Couldn't find a topic even close to using the value of a variable as a variable in the "=" operand.

I've literally reached the end of google results pages, and scoured the help documentation. I'm either too inexperienced to see the solution in front of me, I've been searching the wrong words, or it's just not posted on the internet (highly unlikely). I started programming a month ago, self taught everything, and this is the first problem I've had to ask help for. If you can help me achieve either of the chunks of code I've posted, I can take it from there, and apply it to my real program.

getting off topic, stuck on this topic for 3 days, pulling my hair out from lack of solution, really need help.
Was This Post Helpful? 0
  • +
  • -

#9 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6233
  • View blog
  • Posts: 10,782
  • Joined: 28-September 07

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 07:19 PM

My apologies for not having a chance to look at this sooner.

I have a couple questions:

1. Do you intend to leave Bob, Mary, and Susan hard coded into your game?

2. Do you intend to make this game have a maximum number of players?

The reason I ask:

If the answer to either of the above questions is "yes", you're making this whole process harder than it has to be.
Was This Post Helpful? 0
  • +
  • -

#10 pillarleg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-August 13

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 09:05 PM

View PostBenignDesign, on 02 August 2013 - 07:19 PM, said:

My apologies for not having a chance to look at this sooner.

I have a couple questions:

1. Do you intend to leave Bob, Mary, and Susan hard coded into your game?

2. Do you intend to make this game have a maximum number of players?

The reason I ask:

If the answer to either of the above questions is "yes", you're making this whole process harder than it has to be.


It's fine, any help at all is much appreciated. like I said I've been running into a wall for days.

I was just using bob mary and susan as an example. I made a watered down example because I figured it'd be less complicated than explaining the full reason I need these :S.

The first use for this information was going to be for the equipment system in my game.

See once weapons are generated, they're a large set of variables. Anytime that weapon is moved, the variables have to be switched. This is the code for equipping an item from your first equipment slot:

equipOneBtn.onRelease = function(){
	switch(slotOneSlot){
		case "Weapon":
		if (equippedWeapon == "None"){
			_root.currentInventory -= 1;
		}
		tempSlot = slotOne;
		slotOne = equippedWeapon;
		equippedWeapon = tempSlot;
		tempSlotHp = slotOneHp;
		slotOneHp = equippedWeaponHp;
		equippedWeaponHp = tempSlotHp;
		tempSlotAtk = slotOneAtk;
		slotOneAtk = equippedWeaponAtk;
		equippedWeaponAtk = tempSlotAtk;
		tempSlotDef = slotOneDef;
		slotOneDef = equippedWeaponDef;
		equippedWeaponDef = tempSlotDef;
		tempSlotSpd = slotOneSpd;
		slotOneSpd = equippedWeaponSpd;
		equippedWeaponSpd = tempSlotSpd;
		tempSlotType = slotOneType;
		slotOneType = equippedWeaponType;
		equippedWeaponType = tempSlotType;
		tempSlotNotes = slotOneNotes;
		slotOneNotes = equippedWeaponNotes;
		equippedWeaponNotes = tempSlotNotes;
		tempSlotSlot = slotOneSlot;
		slotOneSlot = equippedWeaponSlot;
		equippedWeaponSlot = tempSlotSlot;
		tempSlotPrice = slotOnePrice;
		slotOnePrice = equippedWeaponPrice;
		equippedWeaponPrice = tempSlotPrice;
		break;
		case "Armor":
		if (equippedArmor == "None"){
			_root.currentInventory -= 1;
		}
		tempSlot = slotOne;
		slotOne = equippedArmor;
		equippedArmor = tempSlot;
		tempSlotHp = slotOneHp;
		slotOneHp = equippedArmorHp;
		equippedArmorHp = tempSlotHp;
		tempSlotAtk = slotOneAtk;
		slotOneAtk = equippedArmorAtk;
		equippedArmorAtk = tempSlotAtk;
		tempSlotDef = slotOneDef;
		slotOneDef = equippedArmorDef;
		equippedArmorDef = tempSlotDef;
		tempSlotSpd = slotOneSpd;
		slotOneSpd = equippedArmorSpd;
		equippedArmorSpd = tempSlotSpd;
		tempSlotType = slotOneType;
		slotOneType = equippedArmorType;
		equippedArmorType = tempSlotType;
		tempSlotNotes = slotOneNotes;
		slotOneNotes = equippedArmorNotes;
		equippedArmorNotes = tempSlotNotes;
		tempSlotSlot = slotOneSlot;
		slotOneSlot = equippedArmorSlot;
		equippedArmorSlot = tempSlotSlot;
		tempSlotPrice = slotOnePrice;
		slotOnePrice = equippedArmorPrice;
		equippedArmorPrice = tempSlotPrice;
		break;
		case "Potion":
		cantEquipWindow = "You can't equip that!";
		break;
	}//ends switch
}//button



The fact I have to move a lot of variables isn't a problem, the problem is that I have to put in a huge chunk of that code that says nearly the same thing with slightly edited words every time an item gets moved.

it feels like a ton of unnecessary code, that I fear my cause computers to lag. Especially considering there are only 4 attributes there, Health, attack, defense and speed, and in the final game there's going to be around 12. 3 lines of code for 9 more attributes is about 27 more lines of code anytime anything has to be moved that I have to edit and change around and stuff. Stuff gets moved around a lot too, so if I don't find a more efficient way of moving an item I'll soon be looking at longer load times and massive walls of text.

That's when I got an idea.

if I could change variables midruntime, I could create a single block of code that does all the work, then program buttons to simply change the variables within that block temporarily, and run everything through it, allowing code to be reused over and over efficiently as opposed to having to be copied, pasted, and each and every time an item is moved.

here's what I had in mind:

if (triggerCounter > 0){ //I need a way to trigger the event when I need to, I'm not sure what I'd actually use when the time comes, so for now I'll use a "triggerCounter" variable.
	tempSlot = this["changeableVariableTarget"];
	tempSlotHp = this["changeableVariableTarget" +Hp];
	tempSlotAtk = this["changeableVariableTarget" +Atk];
	tempSlotDef = this["changeableVariableTarget" +Def];
	tempSlotSpd = this["changeableVariableTarget" +Spd];
	tempSlotType = this["changeableVariableTarget" +Type];
	tempSlotNotes = this["changeableVariableTarget" +Notes];
	tempSlotSlot = this["changeableVariableTarget" +Slot];
	tempSlotPrice = this["changeableVariableTarget" +Price];
//The way this works is first, the tempslot gains the values of whatever variable you set as your target. For instance, if you were equipping a weapon, the weapon you were previously wearing is stored in the tempslots.
	this["changeableVariableTarget"] = this["changeableVariableLocation"];
	this["changeableVariableTarget" +Hp] = this["changeableVariableLocation" +Hp];
	this["changeableVariableTarget" +Atk] = this["changeableVariableLocation" +Atk];
	this["changeableVariableTarget" +Def] = this["changeableVariableLocation" +Def];
	this["changeableVariableTarget" +Spd] = this["changeableVariableLocation" +Spd];
	this["changeableVariableTarget" +Type] = this["changeableVariableLocation" +Type];
	this["changeableVariableTarget" +Notes] = this["changeableVariableLocation" +Notes];
	this["changeableVariableTarget" +Slot] = this["changeableVariableLocation" +Slot];
	this["changeableVariableTarget" +Price] = this["changeableVariableLocation" +Price];
//Next the Target gains the values of whatever the variable you set as your location is. in the equipping a weapon metaphor, this is the phase where the new weapon is actually put onto your character from wherever It previously was, for simplicities sake, let's say the backpack slot one.
	this["changeableVariableLocation" +Type] = tempSlotType;
	this["changeableVariableLocation" +Atk] = tempSlotAtk;
	this["changeableVariableLocation"] = tempSlot;
	this["changeableVariableLocation" +Hp] = tempSlotHp;
	this["changeableVariableLocation" +Def] = tempSlotDef;
	this["changeableVariableLocation" +Spd] = tempSlotSpd;
	this["changeableVariableLocation" +Notes] = tempSlotNotes;
	this["changeableVariableLocation" +Slot] = tempSlotSlot;
	this["changeableVariableLocation" +Price] = tempSlotPrice;
//Next, whatever you set as your original variable receives the values your target had before receiving the new ones. Using the same weapon metaphor, when you equip a new weapon, your old one doesn't vanish, it goes into your backpack, where the old one was. This is the phase where that happens.
	this["changeableVariableLocation"] = "changeableVariableLocation";
	this["changeableVariableTarget"] = "changeableVariableTarget";
//Lastly, this is where whatever you set as your changeableVariableLocation and whatever you set as your changeableVariableTarget cease to be whatever you set them as and return to their original names.
	triggerCounter = 0;
//Then trigger counter returns to 0, ending this whole block.
}



^ obviously this code failed as I don't know how to use a value of a variable on the left side of the "=" operand, nor do I know how to change a variable, which is what I've been searching for for days, a way to do either or both of those.

if I knew, that SAME block of code from before (equipping an item from your first equipment slot) would instead look like this:

equipOneBtn.onRelease = function(){
	switch(slotOneSlot){
		case "Weapon":
		if (equippedWeapon == "None"){
			_root.currentInventory -= 1;
		}
		this[changeableVariableTarget] = "slotOne";
		this[changeableVariableLocation] = "equippedWeapon";
		triggerCounter = 1;
		break;
		case "Armor":
		if (equippedArmor == "None"){
			_root.currentInventory -= 1;
		}
		this[changeableVariableTarget] = "slotOne";
		this[changeableVariableLocation] = "equippedArmor";
		triggerCounter = 1;
		break;
		case "Potion":
		cantEquipWindow = "You can't equip that!";
		break;
	}//ends switch
}//button



That's why I want to know how to use a variable's value before "=" in operand, and how to change a variable during runtime.

No matter how many variables a weapon or armor has, it'll always be that small of a code, because all it's doing is changing those 2 variables, then running that codeblock. At the end of the clodeblock the variables turn back to normal, ready for reuse. The versatility would allow it to be used for any weapon movement purpose, and would significantly help minimize the amount of code I'm using.
Was This Post Helpful? 0
  • +
  • -

#11 pillarleg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-August 13

Re: Using a value as a variable (really need help)

Posted 02 August 2013 - 09:22 PM

That box of code is 71 lines long that I'm currently using. that's with only 4 attributes + things like price and stuff. if I implemented the other 9 attributes, that'd increase that block of code by 54 lines! that's... 125 total lines of code. not to mention if I implemented any other attributes.

I use that or a similarly named block of code at least 18 times in my current program.

That's 2250 lines of code.

if I could get this system up and running, on my new game no matter how many attributes my weapons have, or variables, it will always be a flat 23 whenever a weapon is moved because all the work is done by the separate block of code containing the changeable variables.

if you calculate the 18 times I use the blocks now * the 23 lines instead of 125, you get 414.

I could go from 2250 lines, to 414 lines.

that's 1836 lines of code I could obsolete.

And I could add more inventory slots, that's with only 3.
Was This Post Helpful? 0
  • +
  • -

#12 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6233
  • View blog
  • Posts: 10,782
  • Joined: 28-September 07

Re: Using a value as a variable (really need help)

Posted 03 August 2013 - 05:11 AM

Your explanation of what you're trying to accomplish does help explain a lot. Not sure why you're doing all of this in Flash, but that's neither here nor there.

And I know you're anxious for an answer. I wish I could toss one out there off the top of my head, but this particular question is a learning experience for me as well. I'll keep looking for a good reference explaining how to do what we're trying to do and get back to you with either a great success or to confess my miserable failure.
Was This Post Helpful? 0
  • +
  • -

#13 pillarleg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-August 13

Re: Using a value as a variable (really need help)

Posted 05 August 2013 - 08:16 PM

I've made no progress. Have you had any luck so far?
Was This Post Helpful? 0
  • +
  • -

#14 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6233
  • View blog
  • Posts: 10,782
  • Joined: 28-September 07

Re: Using a value as a variable (really need help)

Posted 05 August 2013 - 08:43 PM

Nothing so far... but I honestly haven't spent a huge amount of time searching. Real life has gotten in the way a bit (work, kids, looming deadlines and the like). I'll keep looking as I find time!!

It's terribly frustrating that I know exactly what you need to do... I just can't figure out the syntax to do it in actionscript!
Was This Post Helpful? 0
  • +
  • -

#15 pillarleg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 02-August 13

Re: Using a value as a variable (really need help)

Posted 13 August 2013 - 05:35 PM

I took a break from trying to decipher this, I started reworking my game into a new, shinier more finalized version of the game while you search, all attempts at finding the answer myself I've given up on honestly. I've never ever in my life researched something so hard I reached the end of the google search results and I have multiple times on this issue :S.

Any luck yet?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1