|
1 registered members (TipmyPip),
18,633
guests, and 5
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
can someone please correct this code?
#373604
06/11/11 12:36
06/11/11 12:36
|
Joined: Jun 2011
Posts: 22 Germany
Bennett
OP
Newbie
|
OP
Newbie
Joined: Jun 2011
Posts: 22
Germany
|
I wanna be able to change weapons with some key, for example q. this is above function_main and works:
TEXT* t_selected_weapon =
{
pos_x = 0;
pos_y = 0;
layer = 1;
string (str_selected_weapon);
string (str_selected_number);
flags = SHOW;
}
but this does not work:
while(1)
{
if (key_q)
{
v_selected_wpn += 1;
v_selected_wpn %= 2;
if (v_selected_wpn == 1)
{
STRING* str_selected_number = "2";
}
if (v_selected_wpn == 0)
{
STRING* str_selected_number = "1";
}
any tips?
|
|
|
Re: can someone please correct this code?
[Re: Bennett]
#373605
06/11/11 12:44
06/11/11 12:44
|
Joined: Nov 2010
Posts: 125 Germany
chrisp1
Member
|
Member
Joined: Nov 2010
Posts: 125
Germany
|
while(1)
{
if (key_q)
{
v_selected_wpn += 1;
v_selected_wpn %= 2;
if (v_selected_wpn == 1)
{
STRING* str_selected_number = "2";
}
if (v_selected_wpn == 0)
{
STRING* str_selected_number = "1";
}
wait(1); ///////////////////////////////////////////// you forgot wait(1); ?
}
1. Did you forget a wait(1); ??? 2. What happens when you run your script ???
Last edited by chrisp1; 06/11/11 12:46.
|
|
|
Re: can someone please correct this code?
[Re: chrisp1]
#373608
06/11/11 12:56
06/11/11 12:56
|
Joined: Jun 2006
Posts: 379 Flevoland, 5 meters under wate...
Roel
Senior Member
|
Senior Member
Joined: Jun 2006
Posts: 379
Flevoland, 5 meters under wate...
|
I think your code switches weapons very fast: everytime the loop runs, and you still have the q key pressed, the weapons will switch again and again.
function switch_weapons()
{
v_selected_wpn += 1;
v_selected_wpn %= 2;
if (v_selected_wpn == 1)
{
STRING* str_selected_number = "2";
}
if (v_selected_wpn == 0)
{
STRING* str_selected_number = "1";
}
}
and then somewhere else: this makes your code run only once, when you press the q button
Last edited by Roel; 06/11/11 12:59.
|
|
|
Re: can someone please correct this code?
[Re: Bennett]
#373636
06/11/11 15:40
06/11/11 15:40
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
Serious User
|
Serious User
Joined: Jul 2008
Posts: 1,178
England
|
here's your problem!
.
if (v_selected_wpn == 1)
{
STRING* str_selected_number = "2";
}
if (v_selected_wpn == 0)
{
STRING* str_selected_number = "1";
}
you're not changing str_selected_number just creating a new local variable, use
str_cpy(str_selected_number, "1");
also, if you've only two weapons
v_selected_wpn += 1;
v_selected_wpn %= 2;
would be better written as
v_selected_wpn = 1 - v_selected_wpn;
|
|
|
Re: can someone please correct this code?
[Re: MrGuest]
#373638
06/11/11 15:53
06/11/11 15:53
|
Joined: Jun 2011
Posts: 22 Germany
Bennett
OP
Newbie
|
OP
Newbie
Joined: Jun 2011
Posts: 22
Germany
|
thank you very much! so what does
str_cpy(str_selected_number, "1");
do exactly? I wanna understand it
|
|
|
Re: can someone please correct this code?
[Re: Bennett]
#373675
06/11/11 23:30
06/11/11 23:30
|
Joined: Jul 2008
Posts: 1,178 England
MrGuest
Serious User
|
Serious User
Joined: Jul 2008
Posts: 1,178
England
|
TEXT* is a struct for displaying strings onscreen. str_cpy replaces the content of one string with something else, have a look in the manual at STRING str_cpy and str_cat which you'll probably need later
|
|
|
Re: can someone please correct this code?
[Re: MrGuest]
#373709
06/12/11 11:28
06/12/11 11:28
|
Joined: Aug 2007
Posts: 1,922 Schweiz
Widi
Serious User
|
Serious User
Joined: Aug 2007
Posts: 1,922
Schweiz
|
|
|
|
Moderated by mk_1, Perro, rayp, Realspawn, Rei_Ayanami, rvL_eXile, Spirit, Superku, Tobias, TSG_Torsten, VeT
|
|
|