This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
MaryuZ's profile picture

help with a global variable

Started by MaryuZ, 11 July 2012 - 10:17 PM
MaryuZ #1
Posted 12 July 2012 - 12:17 AM
I have written this code for a gambling machine but when i try and run it it won't update the global variable credits…it keeps reinialising it…why?

t = {1,2,1,3,2,1,2,3,1}
credits=15
l=0
m=0
r=0
pa=0
bet=1
q={
"|Q.--. |",
"| (\\/) |",
"| :\\/: |",
"| '--'Q|"
}
k={
"|K.--. |",
"| :/\\: |",
"| :\\/: |",
"| '--'K|"
}
a={
"|A.--. |",
"| :/\\: |",
"| (__) |",
"| '--'A|"
}
g = {q,q,k,q,a,k,q,k,a,q,q}
function sb(bet)
if rs.testBundledInput("right", color.blue) then
if bet==1 then
bet=2
elseif bet==2 then
bet=5
elseif bet==5 then
bet=10
else bet=1
end
end
end
function play(bet,credits)
if (credits-bet)>=0 then
credits=credits-bet
l = math.random(1,9)
m = math.random(1,9)
r = math.random(1,9)
if t[l]==t[m] then
if t[m]==t[r] then
if t[l]==1 then
credits=credits+(bet*10)
elseif t[l]==2 then
credits=credits+(bet*25)
else credits=credits+(bet*75)
end
end
end
end
drawplay(l,m,r,bet,credits)
end
function pay()
local i
pa=credits/10
credits=math.fmod(credits,10)
for i=1, pa, 1 do
rs.setBundledOutput("bottom", colors.green)
sleep(1)
rs.setBundledOutput("bottom", 0)
sleep(1)
end
end
	   
function logo(bet,credits)
term.clear()
print("													    ")
print("    .------..------..------..------..------..------.    ")
print("    |M.--. ||A.--. ||R.--. ||Y.--. ||U.--. ||Z.--. |    ")
print("    | (\\/) || (\\/) || :)/>/>): || (\\/) || (\\/) || B)/>/>): |    ")
print("    | :\\/: || :\\/: || ()() || :\\/: || :\\/: || ()() |    ")
print("    | '--'M|| '--'A|| '--'R|| '--'Y|| '--'U|| '--'Z|    ")
print("    '------''------''------''------''------''------'    ")
print("													    ")
print("	    .------..------..------..------..------.	    ")
print("	    |S.--. ||L.--. ||O.--. ||T.--. ||S.--. |	    ")
print("	    | :/\\: || :/\\: || :/\\: || :/\\: || :/\\: |	    ")
print("	    | :\\/: || (__) || :\\/: || (__) || :\\/: |	    ")
print("	    | '--'S|| '--'L|| '--'O|| '--'T|| '--'S|	    ")
print("	    '------''------''------''------''------'	    ")
print("													    ")
print(".------..------..------..------..------..------..------.")
print("|M.--. ||A.--. ||C.--. ||H.--. ||I.--. ||N.--. ||E.--. |")
print("| (\\/) || (\\/) || :/\\: || :/\\: || (\\/) || B)/>/>): || (\\/) |")
print("| :\\/: || :\\/: || :\\/: || (__) || :\\/: || ()() || :\\/: |")
print("| '--'M|| '--'A|| '--'C|| '--'H|| '--'I|| '--'N|| '--'E|")
print("'------''------''------''------''------''------''------'")
print("													    ")
print("########################################################")
print("########################################################")
print("###												  ###")
if bet==10 then
print("###				   BET : ",bet,"					   ###")
else
print("###				   BET : ",bet,"					    ###")
end
print("###												  ###")
if credits<10 then
print("###			   CREDITS : ",credits,"					    ###")
elseif credits<100 then
print("###			   CREDITS : ",credits,"					   ###")
elseif credits<1000 then
print("###			   CREDITS : ",credits,"					  ###")
elseif credits<10000 then
print("###			   CREDITS : ",credits,"					 ###")
else
print("###			   CREDITS : ",credits,"				    ###")
end
print("###												  ###")
print("########################################################")
write("########################################################")
sleep(1)
end
function drawplay(l,m,r,bet,credits)
l=l+1
m=m+1
r=r+1
term.clear()
print("													    ")
print("		 .------.   #   .------.   #   .------.		 ")
print("		 ",g[l-1][1],"   #   ",g[m-1][1],"   #   ",g[r-1][1],"		 ")
print("		 ",g[l-1][2],"   #   ",g[m-1][2],"   #   ",g[r-1][2],"		 ")
print("		 ",g[l-1][3],"   #   ",g[m-1][3],"   #   ",g[r-1][3],"		 ")
print("		 ",g[l-1][4],"   #   ",g[m-1][4],"   #   ",g[r-1][4],"		 ")
print("		 '------'   #   '------'   #   '------'		 ")
print("   \\\\			   #			  #			   //   ")
print("    \\\\   .------.   #   .------.   #   .------.   //    ")
print("	 \\\\  ",g[l][1],"   #   ",g[m][1],"   #   ",g[r][1],"  //	 ")
print("   \\\\\\\\\\ ",g[l][2],"   #   ",g[m][2],"   #   ",g[r][2]," /////   ")
print("   ///// ",g[l][3],"   #   ",g[m][3],"   #   ",g[r][3]," \\\\\\\\\\   ")
print("	 //  ",g[l][4],"   #   ",g[m][4],"   #   ",g[r][4],"  \\\\	 ")
print("    //   '------'   #   '------'   #   '------'   \\\\    ")
print("   //			   #			  #			   \\\\   ")
print("		 .------.   #   .------.   #   .------.		 ")
print("		 ",g[l+1][1],"   #   ",g[m+1][1],"   #   ",g[r+1][1],"		 ")
print("		 ",g[l+1][2],"   #   ",g[m+1][2],"   #   ",g[r+1][2],"		 ")
print("		 ",g[l+1][3],"   #   ",g[m+1][3],"   #   ",g[r+1][3],"		 ")
print("		 ",g[l+1][4],"   #   ",g[m+1][4],"   #   ",g[r+1][4],"		 ")
print("		 '------'   #   '------'   #   '------'		 ")
print("													    ")
print("########################################################")
print("########################################################")
print("###												  ###")
if bet==10 then
print("###				   BET : ",bet,"					   ###")
else
print("###				   BET : ",bet,"					    ###")
end
print("###												  ###")
if credits<10 then
print("###			   CREDITS : ",credits,"					    ###")
elseif credits<100 then
print("###			   CREDITS : ",credits,"					   ###")
elseif credits<1000 then
print("###			   CREDITS : ",credits,"					  ###")
elseif credits<10000 then
print("###			   CREDITS : ",credits,"					 ###")
else
print("###			   CREDITS : ",credits,"				    ###")
end
print("###												  ###")
print("########################################################")
write("########################################################")
sleep(2)
end
logo(bet,credits)
while credits>0 do
play(bet,credits)
end
MaryuZ #2
Posted 12 July 2012 - 12:20 AM
just run the code on a 6x5 monitor and see what it does if you dont get what i mean
the global var credits is initialised with 15 but then it's modified in the function play and it stays modified for the function drawplay but then when play is called again it uses 15 instead of what it was set to the first time it played
Graypup #3
Posted 12 July 2012 - 12:23 AM
AFAIK, the function to get bundled output is rs.getBundledInput(). If it's in 1 program, then it's fine for the "global" variable. The scope for "global" variables isn't very global because they're only accessible from the program that they're defined in. Othrwise, I'll read the code and look for anomalies.
MysticT #4
Posted 12 July 2012 - 12:25 AM
You mean each time you run the program? That's because you initialize it at the top. Try changing it to:

if credits == nil then
  credits = 15
end
So it just initializes it when it's nil / not initialized.

EDIT:
Sorry, didn't read the second post.
The problem is that you don't change the global variable, just the parameter, wich is local to the function. I don't think you need it as a parameter, so just remove it.
Graypup #5
Posted 12 July 2012 - 12:29 AM
I wish that lua required us to put var before variable definitions, so that scope wasn't as annoying as it is.
MaryuZ #6
Posted 12 July 2012 - 12:40 AM
I removed it as a parameter and it now works…..but this is like super weird….i use credits inside the function i don't get it how i can call the function without credits as a parameter.
can i remove bet also?
why do functions use parameters if they just act on global variables without having them as parameters?
also Graypup some questions for you:
what does afaik mean?
what's wrong with my redstone function usage?
Graypup #7
Posted 12 July 2012 - 12:50 AM
AFAIK, that means "as far as I know". The function is rs.getBundledInput, just as I thought (got that info from wiki).
Graypup #8
Posted 12 July 2012 - 12:51 AM
I don't think that bet can be removed, because it's a bit integral.
MaryuZ #9
Posted 12 July 2012 - 12:56 AM
the function to get bundled output is rs.getBundledInput().
ok…what?
MysticT #10
Posted 12 July 2012 - 01:46 AM
I removed it as a parameter and it now works…..but this is like super weird….i use credits inside the function i don't get it how i can call the function without credits as a parameter.
can i remove bet also?
why do functions use parameters if they just act on global variables without having them as parameters?
also Graypup some questions for you:
what does afaik mean?
what's wrong with my redstone function usage?
First, you shouldn't use global variables, make them local, but have them on the "global" scope of the program.
Simple explanation about scopes:
Imagine you have a box, and you put some stuff inside. Now you put another box inside of the first one, with other stuff. The second box can see the objects in the first one, but not the other way around.
Each time you make a function, loop, if, etc. you have a new scope (you put a box inside), and create some variables inside (put some objects on the box). When that function, loop, whatever ends, the everything in the scope will be lost. Also, if you have a variable in an "inner" scope with the same name as other you can't "see" the other one.
Parameters are used to make the functions more "general", so you can use it with different inputs. If there where no parameters, how would you use rs.getInput or any other function in the apis?
For your program, you don't need to have parameters, since you need the variables to be "global" on the program.
MaryuZ #11
Posted 12 July 2012 - 02:06 AM
i get it…thx….
i'm like 90% done with this gambling machine( i just need to make 1 more function and the main body wich just calls the functions in a loop when you press buttons) and then i'll post it….but i have to go to sleep now it's 4 am and at 10 am i have driving lessons