ID:165868
 
hi. i want to know how to make it so that when i attack someone it will add an over lay showing the damage.

i have icons for 0 - 9 in 4 diff posistions. so


0--- 1--- 2--- 3--- 4--- 5--- 6--- 7--- 8--- 9---
-0-- -1-- -2-- -3-- -4-- -5-- -6-- -7-- -8-- -9--
--0- --1- --2- --3- --4- --5- --6- --7- --8- --9-
---0 ---1 ---2 ---3 ---4 ---5 ---6 ---7 ---8 ---9

so where do i go from here? im not sure how to set it up so that it checks the damage them shows the appropriate icons
You dont need them in 4 different positions, just use 1 set of numbers going from 0-9, all at the same position.

Assuming you've got that, and the icons are in an icon file called "damage.dmi", try this out;

mob/verb/attackMe()
var
//How long the overlay stays ontop of the source
overlayLife = 5
//The gap in pixels between each digit
xOffset = 5
//calculate some random damage
damage = rand(10,20)
//start a loop going from 1 to the amount of digits in damage.
for(var/i = 1;i <= lentext("[damage]"); i++)
//add the appropriate overlay. We use the image command here to allow us to
//specify the overlay's icon_state and pixel_x all at once.
//The icon_state is found using the copytext command, which basically extracts
//a piece of text from a string inbetween the specified letters. So, we extract the
//number from the current digit, this number will be our icon state.
//After that we set the pixel_x value, this sets how far to the right or left the overlay will
//be placed. The formula we're using is (i-1)*xOffset. Since i will always equal the current
//digit ID, and we want the first digit to have a pixel offset of 0, we subtract 1.
//I didnt explain that too well but... it works.
src.overlays += image(icon = 'damage.dmi',icon_state = copytext("[damage]",i,i+1),pixel_x = (i-1)*xOffset)
//Wait a bit, and then remove the overlay.
spawn(overlayLife)
src.overlays -= image(icon = 'damage.dmi',icon_state = copytext("[damage]",i,i+1),pixel_x = (i-1)*xOffset)


Most of that is comments so dont be scared by the size. As usual, dont just paste it in and hope for it to work, learn from it.
- Conj'
In response to The Conjuror
 
spawn(overlayLife)
src.overlays -= image(icon = 'damage.dmi',icon_state = copytext("[damage]",i,i+1),pixel_x = (i-1)*xOffset)


Spawning for no reason, mr. Conj?
In response to DivineO'peanut
There is a reason, it lets the loop continue through so it can add the rest of the digits for the damage, afterwards it removes the overlay. If sleep was used it would delete each overlay before adding the next.

[EDIT]

Unless I missed an indent there, I never was sure if you needed to indent anything you want Spawn to affect, I tested it and it worked so I figured it would be ok with that indentation.
In response to The Conjuror
i tinkered with the code a lil (when i attacked the hud damage appeared on me) so i change src.overlays to M.overlays. rounded the damage and it works great :). thanks for that.

also while we are on the subject of HUD can i ask something else? how do i make an ingame hud? that stays on screen all the time?

my mate asked a while back and was given this code

client
lazy_eye = 0
obj/hud/wsay
icon = 'hud{gui}.dmi'
icon_state = "WS1"
screen_loc = "2,2"


howver whenever we tried to add another piece of the hud it never showed up.
In response to Ultimate165
Cant help ya' there, haven't delt with on-screen stuff for a few years now. Give me a couple of minutes to test the screen commands out and I might have something, though from the sounds of things the reason it didnt show up is because you either

A) Used an invalid icon state, resulting in an invisible icon
B) Gave it a screen location that the user cant see. IIRC, by default the screen size is 11x11, that gives you possible positions from 1,1 to 11,11, or maybe it was 0,0, to 12, 12, I forget.
In response to The Conjuror
client
lazy_eye = 0
obj/hud/wsay
icon = 'hud{gui}.dmi'
icon_state = "WS1"
screen_loc = "2,2"
obj/hud/wsay2
icon = 'hud{gui}.dmi'
icon_state = "WS2"
screen_loc = "3,2"


this is the code i tried but only the 1st one shows up.
In response to The Conjuror
The Conjuror wrote:
There is a reason, it lets the loop continue through so it can add the rest of the digits for the damage, afterwards it removes the overlay. If sleep was used it would delete each overlay before adding the next.

[EDIT]

Unless I missed an indent there, I never was sure if you needed to indent anything you want Spawn to affect, I tested it and it worked so I figured it would be ok with that indentation.

Yep, <code>spawn()</code> requires identation. =)
In response to Ultimate165
bump
In response to Ultimate165
obj/Character
Attack
icon='spell icons.dmi'
icon_state="attack"

Click()
for(var/mob/M in oview(1)) // For any mob 1 space away

var/Damage = usr.attack
M.hp -= Damage
sleep(4)
view() << "[usr] attacks [M] for [Damage]!"
flick("attack",usr)
M.Die()
New(client/C)
screen_loc = "1,1"
C.screen+=src




client/New()
..()
new/obj/Character/Attack(src)
new/obj/Spell(src)
new/obj/Base(src)
new/obj/Character/Concentrate(src)


This is what i use and it works great.