ID:1254156
 
(See the best response by Kaiochao.)
Hi, im very new in BYOND coding, im trying to make a game my self,
Now im trying to make a chat that you click a button, and it dissapears, and when you click again, it appears, tried something like this but it doesn't work, what im doing wrong?
Im a newbie in code, please don't laugh ;-;


obj/chatbutton
name = "turnchat"
icon = 'base.dmi'
Click()
if(usr.openchat == 1)
usr.closechatproc()
if(usr.closechat == 1)
usr.openchatproc()

mob/var/openchat=1
mob/var/closechat=0
mob
proc
closechatproc()
set hidden=0
set category="Options"
winset(src,"chat","is-visible=false")
usr.closechat=1
usr.openchat=0
mob
proc
openchatproc()
set hidden=0
set category="Options"
winset(src,"chat","is-visible=true")
usr.openchat=1
usr.closechat=0
Rather than having two variables, openchat and closechat, use one.

Think about this one variable like a door. When the variable is equal to 1, the door is open, and when it is 0 the door is closed. You could also think of it as a toggle. When you think about it like that being able to toggle windows like this should come to you much easier.

Also, I would advise using src instead of usr in procs until you have a solid understanding of what the differences are.
Best response
This is a classic example of where "else" is necessary. Your Click() doesn't appear to be doing anything because you always open it right after closing it.

The problem:
//  x is either 0 or 1
var x = prob(50)
if(x) x = 0
// x is always 0 here
if(!x) x = 1
// x is always 1 here


The solution:
var x = prob(50)
if(x) x = 0
else x = 1
// x is the opposite of what it was initially
// (note: this is the same as x = !x)
In response to Albro1
Like this?:
obj/chatbutton
name = "turnchat"
icon = 'base.dmi'
Click()
if(usr.toggle == 1)
usr.closechatproc()
if(usr.toggle == 0)
usr.openchatproc()

mob/var/toggle=1
mob
proc
closechatproc()
set hidden=0
set category="Options"
winset(src,"chat","is-visible=false")
src.toggle=0
mob
proc
openchatproc()
set hidden=0
set category="Options"
winset(src,"chat","is-visible=true")
src.toggle=1


edit now the window open, but it doens't close
In response to Nezeha
This is the part where you read Kaiochao's post, because he explained why you can't close it. You have to think less like a human and more like a machine, and read the code not how you want it to work but how the computer will read it. Look for issues in logic.
In response to Albro1
I fell so dumb right now, thank you a lot!
In response to Kaiochao
Thanks man! It worked, here is how it ended
obj/chatbutton
name = "turnchat"
icon = 'base.dmi'
Click()
if(usr.toggle == 1)
usr.closechatproc()
else
usr.openchatproc()
mob/var/toggle=1
mob
proc
closechatproc()
set hidden=0
set category="Options"
winset(src,"chat","is-visible=false")
src.toggle=0
mob
proc
openchatproc()
set hidden=0
set category="Options"
winset(src,"chat","is-visible=true")
src.toggle=1
Alternatively,
obj/chatbutton/Click() winshow(usr, "chat", winget(usr, "chat", "is-visible") != "true")


When you click the chatbutton, the "chat" is toggled.
It might not be absolutely clear to you, but it works out if you look closely.
winshow(usr, window, 1) // shows the window
winshow(usr, window, 0) // hides the window

winget(usr, window, "is-visible") == "true" // 1 if visible, 0 if not visible
winget(usr, window, "is-visible") != "true" // 1 if not visible, 0 if visible (could also do == "false")

winshow(usr, window, 1 if not visible, 0 if visible)
-> winshow(usr, window, winget(usr, window, "is-visible") != "true"))
// show the window if not visible
// hide the window if visible

This is the kind of logic that comes in handy when computer programming.

Another tool that comes in handy is noticing when to take something out of context and turn it into a proc!
proc/wintoggle(player, window)
winshow(player, window, winget(player, window, "is-visible") == "false")

obj/chatbutton/Click() wintoggle(usr, "chat")