How to combine it (var i and for var i) (solved)

Imiglikos

Member
Hi,

I have the text above the weapon and under the weapon on the HUD bar. I wanted to use a script that adds a shadow to my text ... but I have no idea how to put it together ..
plain text connection was not a problem, but here is a weapon selection that also uses a similar code ...

below is the text that I have above the weapon and under the weapon

create

GML:
//current weapon
current_weapon = 0;

//width of the text shadow
text_shadow_width = 3;

draw gui

GML:
draw_set_color(c_white);
draw_set_valign(fa_middle);
draw_set_halign(fa_center);
draw_set_font(fn_weapon);


var i = current_weapon;
{
if !(i = 0)



draw_text(560,105,""+string(global.wep[i,e_wep.ammo])); //draw ammo weapons
draw_text(560,25,""+string(global.wep[i,e_wep.name])); //draw weapons

}

below is a script that I want to insert instead of plain text ...

GML:
for (var i = 1; i <= text_shadow_width; i++) {
    
     draw_text_shadow(560, 105, ""+string(global.wep[i,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
     draw_text_shadow(560, 25, ""+string(global.wep[i,e_wep.name]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
    }

Thank you
 

Nidoking

Member
A variable name is any string of letters, numbers, and underscores that doesn't match a reserved word and doesn't start with a number. The number of possible variable names you can use is effectively infinite. Just use something other than i for one of them.
 

Imiglikos

Member
A variable name is any string of letters, numbers, and underscores that doesn't match a reserved word and doesn't start with a number. The number of possible variable names you can use is effectively infinite. Just use something other than i for one of them.

But here are already two other variable names in the create event ...
I do not really understand...
 

Nidoking

Member
Then consider this: Why do both of the variables called i need to be called i? What is the purpose for which you have used the name i for both variables?
 

Imiglikos

Member
Then consider this: Why do both of the variables called i need to be called i? What is the purpose for which you have used the name i for both variables?
you mean local variable?
should I use another local variable instead of the "i" variable? eg "k"?

local variable "i" was used in the first case, before a script for text with a shadow

ok one local variable needs to be changed ... but how do you combine it?
it will be more difficult ..
 

Nidoking

Member
In what way are you trying to "combine" the variables? It looks like you have a for loop that draws some text, and you want to run that loop instead of the draw calls you already have. You put the code you want to run in the place where you want to run it. The two local variables have no connection whatsoever.
 

TheouAegis

Member
You don't even need i in the first case.

Code:
if current_weapon != 0 {
    for (var i = 1; i <= text_shadow_width; i++) {
         draw_text_shadow(560, 105, ""+string(global.wep[current_weapon,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
         draw_text_shadow(560, 25, ""+string(global.wep[current_weapon,e_wep.name]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
    }
    draw_text(560,105,""+string(global.wep[current_weapon,e_wep.ammo])); //draw ammo weapons
    draw_text(560,25,""+string(global.wep[current_weapon,e_wep.name])); //draw weapons
}
 

Imiglikos

Member
Thank you,

Shouldn't it be instead of var i = 1 var i = 0?
because now it only shows me the name of the second, third, fourth and fifth weapon, not the first name weapon


GML:
if current_weapon != 0 {
    for (var i = 1; i <= text_shadow_width; i++) {
         draw_text_shadow(560, 105, ""+string(global.wep[current_weapon,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
         draw_text_shadow(560, 25, ""+string(global.wep[current_weapon,e_wep.name]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
    }
    draw_text(560,105,""+string(global.wep[current_weapon,e_wep.ammo])); //draw ammo weapons
    draw_text(560,25,""+string(global.wep[current_weapon,e_wep.name])); //draw weapons
}
 
Last edited:

chamaeleon

Member
because now it only shows me the name of the second, third, fourth and fifth weapon, not the first name weapon
That sounds more like you are not using proper values for current_weapon, since i has nothing to do with getting the string content itself. If global.wep[0, ...] contains a weapon you will never display it if you only display text when current_weapon is not 0.
 

Imiglikos

Member
That sounds more like you are not using proper values for current_weapon, since i has nothing to do with getting the string content itself. If global.wep[0, ...] contains a weapon you will never display it if you only display text when current_weapon is not 0.

So how is it supposed to be implemented to display all the values?
 

Imiglikos

Member
Use a different value than 0 to indicate that you don't wish to draw text?

I text want to draw ..
as it was before ..
here I use a script which adds a shadow to the text ..
text displays correctly here, but text above first weapon is missing. text only appears on second, third, fourth and fifth weapons
 

chamaeleon

Member
I text want to draw ..
as it was before ..
here I use a script which adds a shadow to the text ..
text displays correctly here, but text above first weapon is missing. text only appears on second, third, fourth and fifth weapons
You may need to show data structure and variable initialization code, and code that updates all involved data and variables in order to get better help. As it stands you have only shown code that draws text when current_weapon is not 0. This is suspiciously close to the first element or row in an array that you might expect to be drawn.
 

Imiglikos

Member
You may need to show data structure and variable initialization code, and code that updates all involved data and variables in order to get better help. As it stands you have only shown code that draws text when current_weapon is not 0. This is suspiciously close to the first element or row in an array that you might expect to be drawn.

sorry ... it shows ... this is what the code looks like ...


obj_game

create

GML:
enum e_wep { have,ammo,firerate,dmg_min,dmg_max,ammo_type,name,ammo_sound }
enum e_gun   { ball,flame_thrower,shotgun,blue_laser,ice }



weapons_total = 5;
current_weapon = 0;




var i = 0;


//basic weapon
global.wep[0 ,e_wep.ammo_type] = obj_ball;
global.wep[0, e_wep.have] = true;
global.wep[0 ,e_wep.ammo] = -1; //endless ammo
global.wep[0 ,e_wep.firerate] = 5;
global.wep[0 ,e_wep.dmg_min] = 3;
global.wep[0 ,e_wep.dmg_max] = 5;
global.wep[0 ,e_wep.name] = "ball";


//second weapon
global.wep[1 ,e_wep.ammo_type] = obj_flame_thrower;
global.wep[1, e_wep.have] = false;
global.wep[1 ,e_wep.ammo] = 30;
global.wep[1 ,e_wep.firerate] = 10;
global.wep[1 ,e_wep.dmg_min] = 3;
global.wep[1 ,e_wep.dmg_max] = 5;
global.wep[1 ,e_wep.name] = "flame_thrower";


//third weapon
global.wep[2 ,e_wep.ammo_type] = obj_shotgun;
global.wep[2, e_wep.have] = false;
global.wep[2 ,e_wep.ammo] = 20;
global.wep[2 ,e_wep.firerate] = 15;
global.wep[2 ,e_wep.dmg_min] = 3;
global.wep[2 ,e_wep.dmg_max] = 5;
global.wep[2 ,e_wep.name] = "shotgun";


//fourth weapon
global.wep[3 ,e_wep.ammo_type] = obj_blue_laser;
global.wep[3, e_wep.have] = false;
global.wep[3 ,e_wep.ammo] = 10;
global.wep[3 ,e_wep.firerate] = 20;
global.wep[3 ,e_wep.dmg_min] = 3;
global.wep[3 ,e_wep.dmg_max] = 5;
global.wep[3 ,e_wep.name] = "blue_laser";


//fifth weapon
global.wep[4 ,e_wep.ammo_type] = obj_ice;
global.wep[4, e_wep.have] = false;
global.wep[4 ,e_wep.ammo] = 5;
global.wep[4 ,e_wep.firerate] = 25;
global.wep[4 ,e_wep.dmg_min] = 3;
global.wep[4 ,e_wep.dmg_max] = 5;
global.wep[4 ,e_wep.name] = "ice";



here, when I use this old code ... where I have the text without a shadow, all weapons are displayed ...


draw gui


GML:
draw_set_color(c_white);
draw_set_valign(fa_middle);
draw_set_halign(fa_center);
draw_set_font(fn_weapon);


var i = current_weapon;
{
if !(i = 0)



draw_text(560,105,""+string(global.wep[i,e_wep.ammo])); //draw ammo weapons
draw_text(560,25,""+string(global.wep[i,e_wep.name])); //draw weapons

}

but if I use this code modified with a script that adds a shadow to the text, I only see 4 weapons out of 5 and it should display all five as above.


draw gui


GML:
if current_weapon != 0 {
    for (var i = 1; i <= text_shadow_width; i++) {
         draw_text_shadow(560, 105, ""+string(global.wep[current_weapon,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
         draw_text_shadow(560, 25, ""+string(global.wep[current_weapon,e_wep.name]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
    }
    draw_text(560,105,""+string(global.wep[current_weapon,e_wep.ammo])); //draw ammo weapons
    draw_text(560,25,""+string(global.wep[current_weapon,e_wep.name])); //draw weapons
}
 
Last edited:

chamaeleon

Member
In your original code if !(i = 0) only makes the first draw call conditional due to not using a code block {...}. And as expected, you are using index 0 to store a weapon in your global.wep array, but your condition in the code will only draw text if it is not 0 (so the second, third, etc., entries). Why don't you try initializing current_weapon to -1 for example and use if (current_weapon != -1) make text drawing conditional.
 

TheouAegis

Member
below is a script that I want to insert instead of plain text ...

GML:
for (var i = 1; i <= text_shadow_width; i++) {

draw_text_shadow(560, 105, ""+string(global.wep[i,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
draw_text_shadow(560, 25, ""+string(global.wep[i,e_wep.name]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
}
Thank you
I got the i=1 from your very first post. If you didn't want that, you shouldn't have used it.
 

Imiglikos

Member
I got the i=1 from your very first post. If you didn't want that, you shouldn't have used it.

changing the value from 0 to -1 causes displaying a negative number on the primary weapon ... The names are now all displayed.

last question for this thread ..
How to remove negative number on basic weapon? instead, it should show an infinity sign for that weapon, for example.
can this be done in code? such an infinity sign to be displayed instead of a negative number under the basic weapon?
can he insert a sprite with the infinity symbol here? and then on the primary weapon the infinity sign would be displayed underneath, and on the others, the amount of ammunition would be displayeded as it is now
 

TheouAegis

Member
You would have to look at your font data itself. Or you just make a sprite and use draw_sprite() instead of draw_text().

The ASCII code (hold ALT and on the numpad type the ASCII code) for infinity symbol ∞ is 236. I think you just need to make sure your font includes ASCII character 236.
 

Imiglikos

Member
You would have to look at your font data itself. Or you just make a sprite and use draw_sprite() instead of draw_text().

The ASCII code (hold ALT and on the numpad type the ASCII code) for infinity symbol ∞ is 236. I think you just need to make sure your font includes ASCII character 236.

probably it will be easier for me to use the infinity sign with the sprites ..

if I use the function for one weapon, ie the first weapon "ball" so that under the weapon icon instead of a negative number, the infinity sprite is displayed, what do I have to do?


GML:
if current_weapon != -1 {
    for (var i = 1; i <= text_shadow_width; i++) {
         draw_text_shadow(560, 105, ""+string(global.wep[current_weapon,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
         draw_text_shadow(560, 25, ""+string(global.wep[current_weapon,e_wep.name]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
    }
    draw_text(560,105,""+string(global.wep[current_weapon,e_wep.ammo])); //draw ammo weapons
    draw_text(560,25,""+string(global.wep[current_weapon,e_wep.name])); //draw weapons
}
or in the original version ...

GML:
draw_set_color(c_white);
draw_set_valign(fa_middle);
draw_set_halign(fa_center);
draw_set_font(fn_weapon);


var i = current_weapon;
{
if !(i = 0)



draw_text(560,105,""+string(global.wep[i,e_wep.ammo])); //draw ammo weapons
draw_text(560,25,""+string(global.wep[i,e_wep.name])); //draw weapons

}
 

Yal

🐧 *penguin noises*
GMC Elder
If you want to shadow everything, I'd use an approach like this:
  1. Make a function draw_hud(x,y) which draws the HUD relative to a starting position, move all your code there. (You could just add a +x to every x coordinate and +y to every y coordinate to adapt it)
  2. Draw it twice, but the first time you move it down/sideways a bit and also turn it black:
GML:
gpu_set_fog(true,c_black,0,0)
draw_hud(2,2)
gpu_set_fog(0,0,0,0)
draw_hud(0,0)
 

Imiglikos

Member
If you want to shadow everything, I'd use an approach like this:
  1. Make a function draw_hud(x,y) which draws the HUD relative to a starting position, move all your code there. (You could just add a +x to every x coordinate and +y to every y coordinate to adapt it)
  2. Draw it twice, but the first time you move it down/sideways a bit and also turn it black:
GML:
gpu_set_fog(true,c_black,0,0)
draw_hud(2,2)
gpu_set_fog(0,0,0,0)
draw_hud(0,0)
will use your method for the shade, thank you.


as for the infinity sign, if I draw it as an input by the draw_sprite () function, how do I do that in my code below .. with the first weapon "ball" is displayed where the infinity icon is displayed? instead of an empty field? the rest is to stay the same .. the next weapons are displayed correctly

draw_sprite(spr_infinity, 0, 560, 105);

GML:
draw_set_color(c_white);
draw_set_valign(fa_middle);
draw_set_halign(fa_center);
draw_set_font(fn_weapon);


var i = current_weapon;
{
if !(i = 0)



draw_text(560,105,""+string(global.wep[i,e_wep.ammo])); //draw ammo weapons
draw_text(560,25,""+string(global.wep[i,e_wep.name])); //draw weapons

}
 

TheouAegis

Member
Code:
if global.wep[current_weapon,e_wep.ammo] < 0
    draw_sprite(spr_Infinity, 0, 560,105);
else
    draw_text(560,105,""+string(global.wep[i,e_wep.ammo])); //draw ammo weapons
You could give teh sprite its own shadow, in which case you'd do the same thing in the shadow code, but don't draw anything. But if you use Yal's suggestion, you shouldn't need to worry about this last bit.
Code:
if global.wep[current_weapon,e_wep.ammo] >= 0
        draw_text_shadow(560, 105, ""+string(global.wep[current_weapon,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
 

Imiglikos

Member
Code:
if global.wep[current_weapon,e_wep.ammo] < 0
    draw_sprite(spr_Infinity, 0, 560,105);
else
    draw_text(560,105,""+string(global.wep[i,e_wep.ammo])); //draw ammo weapons
You could give teh sprite its own shadow, in which case you'd do the same thing in the shadow code, but don't draw anything. But if you use Yal's suggestion, you shouldn't need to worry about this last bit.
Code:
if global.wep[current_weapon,e_wep.ammo] >= 0
        draw_text_shadow(560, 105, ""+string(global.wep[current_weapon,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);

Something wrong .. because it does not display weapon sprites ... only the first one is displayed and the rest does not .. and when I change weapons, this error pops up ..



GML:
############################################################################################
FATAL ERROR in
action number 1
of Draw Event
for object obj_game

Variable obj_game.i(100095, -2147483648) not set before reading it.
 at gml_Object_obj_game_DrawGUI_1 (line 89) -     draw_text(560,105,""+string(global.wep[i,e_wep.ammo]));
############################################################################################
--------------------------------------------------------------------------------------------
stack frame is
gml_Object_obj_game_DrawGUI_1 (line 89)
 

Imiglikos

Member
Code:
if global.wep[current_weapon,e_wep.ammo] < 0
    draw_sprite(spr_Infinity, 0, 560,105);
else
    draw_text(560,105,""+string(global.wep[i,e_wep.ammo])); //draw ammo weapons
You could give teh sprite its own shadow, in which case you'd do the same thing in the shadow code, but don't draw anything. But if you use Yal's suggestion, you shouldn't need to worry about this last bit.
Code:
if global.wep[current_weapon,e_wep.ammo] >= 0
        draw_text_shadow(560, 105, ""+string(global.wep[current_weapon,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
this is missing, and hence I have a bug ... no weapons other than primary weapons are showing up for me

GML:
draw_text(560,25,""+string(global.wep[i,e_wep.name]));


GML:
if global.wep[current_weapon,e_wep.ammo] < 0
    draw_sprite(spr_Infinity, 0, 560,105);
else
    draw_text(560,105,""+string(global.wep[i,e_wep.ammo])); //draw ammo weapons
 

TheouAegis

Member
draw_text(560,25,""+string(global.wep[i,e_wep.name]));
Oh I see what you're talking about. That was a typo.
GML:
/* You can remove this block if you use Yal's suggestion
if current_weapon != 0 {
    for (var 0 = 1; i < text_shadow_width; i++) {
         draw_text_shadow(560, 105, ""+string(global.wep[current_weapon,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
         draw_text_shadow(560, 25, ""+string(global.wep[current_weapon,e_wep.name]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
    }
*/

    if global.wep[current_weapon,e_wep.ammo] < 0
        draw_sprite(spr_Infinity, 0, 560,105);
    else
        draw_text(560,105,""+string(global.wep[current_weapon,e_wep.ammo])); //draw ammo weapons
    draw_text(560,25,""+string(global.wep[current_weapon,e_wep.name])); //draw weapons
}
 

Imiglikos

Member
Oh I see what you're talking about. That was a typo.
GML:
/* You can remove this block if you use Yal's suggestion
if current_weapon != 0 {
    for (var 0 = 1; i < text_shadow_width; i++) {
         draw_text_shadow(560, 105, ""+string(global.wep[current_weapon,e_wep.ammo]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
         draw_text_shadow(560, 25, ""+string(global.wep[current_weapon,e_wep.name]), fn_weapon, i, c_black, -1, 120, 1, 1, 0, c_white, c_white, c_white, c_white, 1);
    }
*/

    if global.wep[current_weapon,e_wep.ammo] < 0
        draw_sprite(spr_Infinity, 0, 560,105);
    else
        draw_text(560,105,""+string(global.wep[current_weapon,e_wep.ammo])); //draw ammo weapons
    draw_text(560,25,""+string(global.wep[current_weapon,e_wep.name])); //draw weapons
}

Thank you ... that was it
 
Top