ID:1754769
 
(See the best response by LordAndrew.)
If you declare a var in a proc, how do you change that var within another proc?

mob/verb
Change()
var/var1 = 0
changeVar(var1)
world << "[var1]"


proc
changeVar(var/variable)
variable = 2


The output always gives me 0, meaning it didn't change at all.
Best response
Unfortunately this is not possible in DM. Variables scoped to a function cannot be modified within another function unless the second function returns a value back to the first function.

mob/verb/Change()
var var1 = 0
var1 = changeVar(var1)
world << var1

proc/changeVar(variable)
return variable++
Ahhhh I thought so. I really didn't want to use return, so I guess I'll have to simply used a predeclared var. Thanks for the help.
You'd want to use return values
mob/verb
Change()
var/var1 = 0
var1 = changeVar(var1)
world << "[var1]"

proc/changeVar(var/variable)
variable = 2
return variable


EDIT: I realize LA provided the same answer that I did, but I still feel the need to ask this regardless. What are you doing that you need to change a variable in a different function? Why can't you cover it in the same function or use a return?
mob
proc
Mix(obj/Profession/Witchcraft/Cauldron/C)
C.mixed = 0
switch(C.contents.len)
if(0)
src << "<font color=#95A3B5>There is nothing in here to mix."
return
if(1)
MAT1(C, /obj/Inventory/Augury/Potions/Sanguine_Elixer, 1, C.mixed)

MAT1(C, /obj/Inventory/Augury/Potions/Noruz_Tonic, 1, C.mixed)

MAT1(C, /obj/Inventory/Augury/Misc/Blank_Sutra, 5, C.mixed)

MAT1(C, /obj/Inventory/Augury/Misc/Demon_Shadow, 12, C.mixed)

MAT1(C, /obj/Inventory/Augury/Poison/Vigor_Venom, 5, C.mixed)

if(2)
MAT2(C, /obj/Inventory/Augury/Potions/Corpuscles, 1, C.mixed)

MAT2(C, /obj/Inventory/Augury/Misc/Wraith_Pate, 1, C.mixed)

MAT2(C, /obj/Inventory/Augury/Potions/Skoulet, 4, C.mixed)

MAT2(C, /obj/Inventory/Augury/Potions/Poison_Constituent, 7, C.mixed)

MAT2(C, /obj/Inventory/Augury/Misc/Demon_Will, 13, C.mixed)

MAT2(C, /obj/Inventory/Augury/Poison/Vigor_Toxin, 7, C.mixed)

MAT2(C, /obj/Inventory/Augury/Poison/Vigor_Contagion, 10, C.mixed)

MAT2(C, /obj/Inventory/Augury/Antibody/Weak_Antibody, 5, C.mixed)

MAT2(C, /obj/Inventory/Augury/Antibody/Strong_Antibody, 7, C.mixed)

MAT2(C, /obj/Inventory/Augury/Elixers/Elixer_of_Life, 14, C.mixed)

if(3)
MAT3(C, /obj/Inventory/Augury/Potions/Potent_Poison, 11, C.mixed)

MAT3(C, /obj/Inventory/Augury/Misc/Hulluk, 7, C.mixed)

MAT3(C, /obj/Inventory/Augury/Potions/Mow_Potion, 6, C.mixed)

MAT3(C, /obj/Inventory/Augury/Potions/Lextric_Mix, 3, C.mixed)

MAT3(C, /obj/Inventory/Augury/Potions/Testron_Tonic, 8, C.mixed)

MAT3(C, /obj/Inventory/Augury/Potions/Respine_Elixer, 2, C.mixed)

MAT3(C, /obj/Inventory/Augury/Elixers/Elixer_of_Death, 16, C.mixed)

if(C.mixed == 1)
return
else
for(var/obj/T in C.contents)
src << "<font color=#95A3B5>You didn't create anything with the ingredients!"
del(T)

MAT1(obj/Profession/Witchcraft/Cauldron/C, var/obj/Inventory/Augury/Result, var/REQ, var/mixed)
C.mixed = 0
var/obj/Inventory/Augury/A = new Result
var/obj/Inventory/In = A.components[1]
In = new In
var/Amount = A.components[A.components[1]]
var/obj/Inventory/Ingredient = locate(In) in C.contents

if(!Ingredient) return

if(Ingredient.stack_count >= Amount)
if(REQ > WCL)
src << "<font color=#95A3B5>Your witchcraft level needs to be at least [REQ]."
del(A)
del(In)
return
var/amount = round(Ingredient.stack_count / Amount)
var/name
for(var/n = amount, n>0, n--)
var/obj/Inventory/P = new Result
name = "[P.name]"
CitemAdd(P)
WCE(10*REQ)
C.mixed = 1
src << "<font color=#95A3B5>You made [amount] [name](s)!"
Ingredient.itemDelete(amount*Amount)
del(A)
del(In)

MAT2(obj/Profession/Witchcraft/Cauldron/C, var/obj/Inventory/Augury/Result, var/REQ, var/mixed)
C.mixed = 0
var/obj/Inventory/Augury/A = new Result

var/obj/Inventory/In1 = A.components[1]
In1 = new In1
var/Amount1 = A.components[A.components[1]]

var/obj/Inventory/In2 = A.components[2]
In2 = new In2
var/Amount2 = A.components[A.components[2]]

var/obj/Inventory/Ingredient1 = locate(In1) in C.contents
var/obj/Inventory/Ingredient2 = locate(In2) in C.contents

if(!Ingredient1 || !Ingredient2) return

if((Ingredient1.stack_count >= Amount1) && (Ingredient2.stack_count >= Amount2))
if(REQ > WCL)
src << "<font color=#95A3B5>Your witchcraft level needs to be at least [REQ]."
del(A)
del(In1)
del(In2)
return

var/amount = min(round(Ingredient1.stack_count / Amount1), round(Ingredient2.stack_count / Amount2))
var/name
for(var/n = amount, n>0, n--)
var/obj/Inventory/P = new Result
name = "[P.name]"
CitemAdd(P)
WCE(10*REQ)
C.mixed = 1
src << "<font color=#95A3B5>You made [amount] [name](s)!"
Ingredient1.itemDelete(amount*Amount1)
Ingredient2.itemDelete(amount*Amount2)
del(A)
del(In1)
del(In2)

MAT3(obj/Profession/Witchcraft/Cauldron/C, var/obj/Inventory/Augury/Result, var/REQ, var/mixed)
C.mixed = 0
var/obj/Inventory/Augury/A = new Result

var/obj/Inventory/In1 = A.components[1]
In1 = new In1
var/Amount1 = A.components[A.components[1]]

var/obj/Inventory/In2 = A.components[2]
In2 = new In2
var/Amount2 = A.components[A.components[2]]

var/obj/Inventory/In3 = A.components[3]
In3 = new In3
var/Amount3 = A.components[A.components[3]]

var/obj/Inventory/Ingredient1 = locate(In1) in C.contents
var/obj/Inventory/Ingredient2 = locate(In2) in C.contents
var/obj/Inventory/Ingredient3 = locate(In3) in C.contents

if(!Ingredient1 || !Ingredient2 || !Ingredient3) return

if((Ingredient1.stack_count >= Amount1) && (Ingredient2.stack_count >= Amount2) && (Ingredient3.stack_count >= Amount3))
if(REQ > WCL)
src << "<font color=#95A3B5>Your witchcraft level needs to be at least [REQ]."
del(A)
del(In1)
del(In2)
del(In3)
return

var/amount = min(round(Ingredient1.stack_count / Amount1), round(Ingredient2.stack_count / Amount2), round(Ingredient3.stack_count / Amount3))
var/name
for(var/n = amount, n>0, n--)
var/obj/Inventory/P = new Result
name = "[P.name]"
CitemAdd(P)
WCE(10*REQ)
C.mixed = 1
src << "<font color=#95A3B5>You made [amount] [name](s)!"
Ingredient1.itemDelete(amount*Amount1)
Ingredient2.itemDelete(amount*Amount2)
Ingredient3.itemDelete(amount*Amount3)
del(A)
del(In1)
del(In2)
del(In3)


I declared a var called mixed before the switch statement. ATM it says C.mixed but its because I just made cauldrons have a mixed var.