ID:1795026
 
(See the best response by Tarundbz.)
Code:
obj/HUD
Attack_Button
name = "Attack"
icon = 'buttons.dmi'
icon_state = "button"
layer = MOB_LAYER+10
New(client/c)
world << "Button created!"
screen_loc = "1,1"
c.screen += src
Click()
view() << "It works!"


Problem description:

I am trying to place a button in my game, but I simply can't get any hud to work. I've tried a lot of stuff, and even using a HUD-managing library didn't work for me. I litterally tried copy-pasting code from other people to no avail.

The line "Button created" never pops up, so I'm guessing that's the biggest part of the problem.
Well, for that you'll need to make sure you're actually creating an instance of that object somewhere else in the code, since all the bit within the New() scope tells you is what to do whenever a new instance is created. i.e. on Login you could do:

mob
Login()
var/obj/testObj = new obj/HUD/Attack_Button(client)
//client same as src.client which would in this case act
//as usr.client so it is valid to satisfy your client/c arg for#
//obj/HUD/New/Attack_Button()


and Voila, bearing in mind that you should have an output box somewhere -which i'm gonna guess you will.. :P- the appropriate messages and things should occur as necessary.

Just in case though, since you mentioned you've been stuck on this for some time -unless you didn't take long trying 'a lot of stuff'- and assuming that you aren't a complete beginner to programming with the DM, maybe your past issues were tied to not realising that passing an argument upon initialisation of an obj mattered since you defined it to work as such, i.e.:

mob
Login()
var/obj/testObj = new obj/HUD/Attack_Button()

Would not work i don't think, because you request it to be as such in New(client/c) without giving the argument/parameter client/c a default value.
obj/HUD
Attack_Button
name = "Attack"
icon = 'buttons.dmi'
icon_state = "button"
layer = MOB_LAYER+10
New(client/c)
world << "Button created!"
screen_loc = "1,1"
c.screen += src
Click()
view() << "It works!"

mob/pc
Login()
..()
var/obj/testObj = new obj/HUD/Attack_Button


I really appreciate the help. It's really reassuring to know that someone can help me out, and is willing to do so. However... I still can't really get it to work.

As you said, I really am a beginner at DM programming (but not at programming in general) And I've started this project about a week ago, learning on the go.

I've tried placing that code, but it tells me obj, HUD and Attack_Button are undefined vars, with a warning saying "testObj defined but not used"

Thanks again
In response to Polkjm
Best response
obj/HUD
Attack_Button
name = "Attack"
icon = 'buttons.dmi'
icon_state = "button"
layer = MOB_LAYER+11
New(client/c)
//world << "Button created!"
screen_loc = "3,1"
c.screen += src
Click()
usr << "It works!"
alert("Hello")

mob
Login()
usr.client.HUD()
client/proc/HUD()//easy way to add HUDs
new/obj/HUD/Attack_Button(src)
Why have people given the guy who answered after me best response votes for basically echoing what i already suggested to Polkjm - simply in addition doing things through a client/proc to make the write-up appear easier on the eyes within mob/Login().

Polkjm your real issue, which i kinda suspected initially, is that you claim to have tried and supposedly slotted in different solutions into your project to try to get this to work, yet it appears as i can see here, that rather than following the explanations and doing things as shown -the correct way- you change certain things slightly, for instance you had:

mob/pc
Login()

Instead of:
mob
Login


You may assume that shouldn't matter, but actually small differences like that can affect the correct solutions in each case slightly you know (so
new obj/HUD/Attack_Object(client)
//might instead become
new obj/HUD/Attack_Object(usr.client)
).
To re-iterate also, correct syntax requires you have parenthesis denoted after the path you list to create a new instance of the Attack_Button, i.e.
new/obj/HUD/Attack_Button(src.client) // == (client) like i said initially

or:
client
proc/HUD()
new/obj/HUD/Attack_Button(src)

which is what Tarun suggested, which when reasoned out, is interpreted out to meaning the same thing as what i already gave and explained to you a day earlier... alas, here we are.

Ultimately, follow any of these suggestions correctly, and you should have no issues getting what you need accomplished. Next time, though, i suggest you take the time to read through any accompanying explanations that people who try to help you might give, in addition to any code they might supply. That way you won't go wrongly applying the solutions they give you due to a lack of understanding, which then just makes you have to wait longer than necessary to get finally arrive at an answer someone might have already given to you. Thanks.

Let me be clear first of all, I did place the parentheses at first. I tried without them at the end, and, running out of options, decided to ask again. I tried all the pieces of code you sent me. I literally created a new project and copy-pasted Tarun's code. I got the line "Button created!" to pop up, but nothing was shown. Here's the entirety of this mini-project.

/*
These are simple defaults for your project.
*/


world
fps = 25 // 25 frames per second
icon_size = 32 // 32x32 icon size by default

view = 6 // show up to 6 tiles outward from center (13x13 view)


// Make objects move 8 pixels per tick when walking

mob
step_size = 32

obj
step_size = 32


turf
grasse
icon = 'floor.dmi'

obj/HUD
Attack_Button
name = "Attack"
icon = 'buttons.dmi'
icon_state = "button"
layer = MOB_LAYER+11
New(client/c)
//world << "Button created!"
screen_loc = "3,1"
c.screen += src
Click()
usr << "It works!"
alert("Hello")

mob
icon = 'rutabaguse.dmi'
Login()
usr.client.HUD()
client/proc/HUD()//easy way to add HUDs
new/obj/HUD/Attack_Button(src)


Along with three little icons... The line "Button created!" pops up, but I can't see any button on the screen...

Here's hoping I don't look like an idiot again.
    New(client/c)
//world << "Button created!"
screen_loc = "3,1"
c.screen += src


This is problematic.

For any atom, the first argument for New() should always be loc.

New(loc,client/c)


DM implicitly interprets the first argument of New() as a location. Not paying attention to this can lead to all kinds of problems. For instance, I helped someone debug their Naruto game a few months back where the savefiles were just constantly increasing in size. It turned out that he was initializing his hud objects with the first argument being the player they were going to be added to. This resulted in the hud objects going into the player's inventory, which was saved.

In your particular case, this won't be a problem as client isn't a valid location, but it's always best to avoid making the first argument of New() of any atom subtype ambiguous like that. It's brittle.

mob
Login()
usr.client.HUD()
..()


If you aren't setting the location of the player in Login(), you need to call .. (the supercall, or overridden function), which will place them as close to 1,1,1 as possible.

You also shouldn't be using usr in Login(). It's incorrect use of syntax. Tarun, I've mentioned usr abuse to you before. It's best not to propagate bad habits like that --particularly one that's so widely known about.

mob
Login()
client.Hud()
..()


Show us a screenshot of the buttons.dmi file. That's the only place something could be going wrong, looking at your code.
Oh my god! It works!!

I've made the changes you suggested, and changed the syntax of New() inside Login(), so it now sets the location correctly... Now I can finally see a button on my screen!

It actually works perfectly now, so I really want to thank you three who helped me. I've been struggling for a week, and now I'm really happy I called for help.

Thank you so much!

I can finally advance in the making of my game now :D