mIRC Homepage
Posted By: Coca_Bear If's Question - 08/01/03 07:31 AM
Hi, i've only been working with mirc scripting for a week.. and i've sorta made a Fighting Style game for mirc.. and i have this (simple problem).. i'll try to explain it the best i can..

what i want to know is can if's do this..

if %this == True { do this}
if %this == Flase {do that}

Is this possible?

Cause i have have this on !attack

on *:text:!attack:#: {
if ($2 == $null) { /randattack }
else if ($2 == $read(test.txt, w, $2)) { /attackplayer }
else { /msg $chan The character $2 is not in our database }
}

and what i was hoping was putting

else if ($read(test.txt, w, $2) == True) { /attackplayer }
else if ($read(test.txt, w, $2) == Fasle) { /msg $chan The character $2 is not in our database }

Does this make any sense?

Thanks

Coca-Bear
Posted By: Hammer Re: If's Question - 08/01/03 11:47 AM
Quote:
if %this == True { do this}
if %this == Flase {do that}

Code:

  if (%this) {
    do this
    and this
    and this too
  }
  elseif (%that) {
    do this
    and that
  }
  else {
    do that
  }
  
or
  
  if (%this) { do this | and this | and this too }
  elseif (%that) { do this | and that }
  else do that 


Quote:
on *:text:!attack:#: {
if ($2 == $null) { /randattack }
else if ($2 == $read(test.txt, w, $2)) { /attackplayer }
else { /msg $chan The character $2 is not in our database }
}

Code:

on *:text:!attack:#: {
  [color:#006600];  No nick was specified[/color]
  if ($2 == $null) randattack
  
  [color:#006600];  Nick was specified and found[/color]
  elseif ($read(test.txt, w, $+(*,$2,*))) attackplayer
  
  [color:#006600];  Nick was specified but not found[/color]
  else msg $chan The character $2 is not in our database
}

In any IF, ELSEIF or WHILE (condition), you are really checking for not $false rather than anything specific. There are 3 values that mIRC treats as $false: $false, 0 and $null. Anything else is $true, as far as the condition is concerned. 1 is $true, 0 is not. Coca_Bear is $true. $true is obviously $true as well. $false is obviously not $true.

This is extremely handy in coding. You can easily check to see if a variable has been set (to something other than 0/$false/$null) using (%variable) or check for it's not having been set with (!%variable). The same thing applies to identifiers. If a value is returned that's not 0 or $false, then it's $true.

$read(test.txt,w,$+(*,$2,*)) wiill return $null if $2 is not found anywhere on any line of test.txt. If it is found, it will return the first matching line - which does not equal $false/$null/0, and so is therefore equivalent to $true.
Posted By: Coca_Bear Re: If's Question - 08/01/03 12:57 PM
ok. basiclly all test.txt is a checking file to make sure that the $2 character is there if its $false... it wont attack the player.. if $2 is $true it will attack the player. So would the code looking something like this?

if ($read(test.txt,w,$+(*,$2,*) == $true) { /attackplayer }
else if ($read(test.txt,w,$+(*,$2,*) == $false) { /msg $chan The character $2 is not in our database }

edit: fixed typos :tongue:

Thanks for the help.

Coca-Bear
Posted By: Hammer Re: If's Question - 08/01/03 01:41 PM
You went back to your old faulty code for some reason. You also removed the closing ) from the $read( ).

if ($read(test.txt,w,$+(*,$2,*) == $true) { /attackplayer }
This condition can ONLY, ONLY, ONLY be true if you are literally searching for "$true" in that file, not a nick or whatever else; you would be attacking someone called $true.

else if ($read(test.txt,w,$+(*,$2,*) == $false) { /msg $chan The character $2 is not in our database }
It's "elseif".
This condition can ONLY, ONLY, ONLY be true if you are literally searching for "$false" in the file, not a nick or whatever else; you would be messaging the channel that $false is not in your database.

However, if you are looking to see if $2 (which is a nick) is found in that text file, then your example will NOT work at all for the reasons given above. Read very closely the following code (which is exactly the same code I posted before, but this time without comments.
Code:

on *:text:!attack:#: {
  if ($2 == $null) randattack
  elseif ($read(test.txt, w, $+(*,$2,*))) attackplayer
  else msg $chan The character $2 is not in our database
}

This code will do exactly what you want it to do. Yours will not.
Posted By: Coca_Bear Re: If's Question - 08/01/03 01:55 PM
ah ok.. Thanks for your help :tongue: i understand now hehe

Coca-Bear
Posted By: Nimue Re: If's Question - 08/01/03 02:01 PM
Quote:
on *:text:!attack:#: {
No it won't
There will never be a $2. "!attack" will only match "!attack", and not "!attack <name>" :tongue:
Posted By: Hammer Re: If's Question - 08/01/03 02:18 PM
Code:

on *:text:!attack*:#: {
  if ($2 == $null) randattack
  elseif ($read(test.txt, w, $+(*,$2,*))) attackplayer
  else msg $chan The character $2 is not in our database
}
Oops.
Posted By: Coca_Bear Re: If's Question - 08/01/03 02:32 PM
Yes i noticed that...ok i tried this it didn't work..

Code:
 on *:text:!attack*:#: {  
if ($2 == $null){ /randattack }
else if ($read(test.txt, w, $+(*,$2,*))) { /attackplayer }
else { /msg $chan The character $2 is not in our database}
}
 


i can do !attack and it does a random player. but still it attacks players that are not in test.txt.

Also i do !attack playername and it does nothing.. nothing at all

Any help is greatly appreciated :tongue:

Thanks

Coca-Bear

Coca-Bear
Posted By: Hammer Re: If's Question - 08/01/03 02:45 PM
I am not going to keep re-correcting your script when you insist on switching the code I give you and breaking it. CAREFULLY re-read this entire thread and you will figure out why it does not work. (HINT: elseif). Happy hunting and good luck.
Posted By: Nimue Re: If's Question - 08/01/03 02:55 PM
Quote:
if ($2 == $null){ /randattack }

Code:
if ($2 == $null[color:red]) {[/color] randattack }

Also
Quote:
else { /msg $chan The character $2 is not in our database}
Code:
else { /msg $chan The character $2 is not in our [color:red]database }[/color]
Posted By: Nimue Re: If's Question - 08/01/03 03:04 PM
Wrong smile
The lack of spaces is the problem
Posted By: Collective Re: If's Question - 08/01/03 03:06 PM
Whoops, I deleted the post, was gonna make a new one, but the first space you pointed out is irrelevant smile (apart from making it look pretty)
Posted By: Nimue Re: If's Question - 08/01/03 03:09 PM
It totally is not irrelevant :tongue:
Paste that code without the space into mIRC editor and hit the {} button and see what happens wink
Posted By: Coca_Bear Re: If's Question - 08/01/03 03:09 PM
well all i can see wrong is elseif else if..

I've tried it and still it doesn't !attack playername and still attacks players that are not in the list smirk ...

If i have missed something than i have no clue what it is smirk

Code:
on *:text:!attack*:#: {  
  if ($2 == $null) { /randattack }
  elseif ($read(test.txt, w, $+(*,$2,*))) { /attackplayer  }
  else { /msg $chan The character $2 is not in our database }
}
  


Thanks

Coca-Bear
Posted By: Collective Re: If's Question - 08/01/03 03:12 PM
Damn, you win :'(
Posted By: Nimue Re: If's Question - 08/01/03 03:18 PM
I imagine you might be needing to know which player to "attackplayer"
Code:
on *:text:!attack*:#:{
  if ($2 == $null) { randattack }
  elseif ($read(test.txt, w, $+(*,$2,*))) { attackplayer $2 }
  else { msg $chan The character $2 is not in our database }
}
Posted By: Coca_Bear Re: If's Question - 08/01/03 03:25 PM
no its not that.. because attackplayer is /attackplayer and calls a alias/function in my code i have included the "/".. i dunno why Hammer took them out?

Coca-Bear
Posted By: Collective Re: If's Question - 08/01/03 03:27 PM
From the help file:

Note: I didn't prefix the above commands with the / command prefix. This is because the command prefix is really only needed when entering a command on the command line. In scripts, all lines are assumed to start with a command, so you don't need to use the / command prefix. [/color]
Posted By: Nimue Re: If's Question - 08/01/03 03:30 PM
Because the "/"'s are not needed in script files. smile
If it doesn't work I suggest looking to the other aliases, that code works as far it goes.
Posted By: Coca_Bear Re: If's Question - 08/01/03 03:38 PM
i tried that... it works.. thanks for that... and thanks to everyone that has helped me.. especially Hammer :tongue:

Coca-Bear
Posted By: Hammer Re: If's Question - 08/01/03 04:15 PM
Now that you have it fixed, I will explain why that else if wouldn't have worked (even if you had kept your spacing correct):
Code:

  if (condition) { randattack }
  else if (condition2) { attackplayer $2 }
  [color:red]else { msg $chan Not found. }[/color]

That red ELSE line will never ever be run because it has no matching IF.

As you had it written, it would have looked like this using all the braces and separate lines to show it more clearly:
Code:

  if (condition1) {
    randattack
  }
  else {
    if (condition2) {
      attackplayer $2
    }
  }
  else {
    msg $chan Not found.
  }

Notice that you have 2 else's, one after the other, there. The second one has no IF associated with it, therefore it can never get executed. If you wanted it to go with if (condition2) and both of them under the else, then you have to group them with { }, like this:
Code:

  if (condition1) { randattack }
  else {
    if (condition2) { attackplayer $2) }
    else { msg $chan Not found.  }
  }

else if is NOT the same thing as elseif.
  • if (this condition works) { do this }

    ; Ok, that one didn't work, let's try a different condition
    elseif (a different condition works) { do that }

    ; Well, none of the above worked, so do this last thing in an "otherwise" condition
    else { do something else entirely }
Control drops out of the IF-ELSEIF-ELSE structure when it finds its first matching ($true) condition. The others are not even tested or considered at all. So, if the IF condition works, ELSEIF and ELSE are completely disregarded. If the IF fails, then the ELSEIF condition is tested and, if it is true, then the code in the ELSEIF block is executed and the ELSE section is completely disregarded. If neither the IF nor the ELSEIF match, then the ELSE code is executed.

The main function of { } is to group multiple commands, either on a single line separated by | or on multiple lines; in the case of multiple commands, { } are required to group all the commands into a block. They are not required for a single command on the same line; that is why I repeatedly removed them from my example code. They are required for anything that is multi-lined, single command or multiple commands. (Note that this is not the same thing at all as $&, which is line continuation ... continuing the same line on the next line but still treating it as if it were all on one line, which is used for readability for very long script lines.)
Posted By: Coca_Bear Re: If's Question - 10/01/03 11:27 AM
Ok i have another goodie Question for you.. :P

Can an on text go into an if?

e.g.

if (on *:text:This:#: {} ) {/msg $chan That} - i've tried this and i know its wrong, but is there anyway it is possible?

Thanks again

Coca-Bear
Posted By: MonoSex Re: If's Question - 10/01/03 11:34 AM
if (<text> isin $1-) { }
Posted By: Coca_Bear Re: If's Question - 10/01/03 12:57 PM
ok i tested that.. it didn't work.. i'll show you what i have.

Code:
alias buy {

  /msg $chan Welcome $nick What would you like to by?
  /notice $nick Blue Potion 500coins Restores 10HP
  /notice $nick Crystal Blue Potion 1000coins Restores 50HP
  /notice $nick Heal Potion 3000coins Restores 150HP 
  /notice $nick Elixer of Heal 10000coins Restores 5000HP

  if (Blue_Potion isin $1-) {/msg $chan Just Testing}

}
  


Coca-Bear
Posted By: Collective Re: If's Question - 10/01/03 01:01 PM
on *:TEXT:!buy*:#:{
buy $2-
}
alias buy {
msg $chan Welcome $nick What would you like to by?
notice $nick Blue Potion 500coins Restores 10HP
notice $nick Crystal Blue Potion 1000coins Restores 50HP
notice $nick Heal Potion 3000coins Restores 150HP
notice $nick Elixer of Heal 10000coins Restores 5000HP
if ( Blue_Potion isin $1- ) { msg $chan Just Testing }
}
Posted By: Coca_Bear Re: If's Question - 10/01/03 01:10 PM
ok i don think i've explained myself here :tongue:

this is how i washoping to go..

they type !buy it /notices them the items.. then they type Blue_Potion and it says "you have just brought a Blue_Potion".. or whatever my item they brought. i dont want to have it !buy Blue_Potion because i want them to go though the !buy (like a shop).

Does this make any sense? hehe

Coca-Bear
Posted By: Collective Re: If's Question - 10/01/03 01:18 PM
on *:TEXT:!buy*:#:{
msg $chan Welcome $nick What would you like to by?
notice $nick Blue Potion 500coins Restores 10HP
notice $nick Crystal Blue Potion 1000coins Restores 50HP
notice $nick Heal Potion 3000coins Restores 150HP
notice $nick Elixer of Heal 10000coins Restores 5000HP
}
on *:TEXT:blue_potion:#:{
notice $nick You have just bought a blue_potion!
}

Something like that would work..
Posted By: Coca_Bear Re: If's Question - 10/01/03 01:48 PM
i wanted the person to be in !buy to actually buy the blue potion.. with that code.. i can just do Blue_Potion and it buys it for me.. i dont have to use !buy

e.g.

!buy
Blue_Potion.

They have to do !buy before they can do Blue_Potion.

Coca-Bear
Posted By: Collective Re: If's Question - 10/01/03 01:52 PM
Edit: Scratch this post, I didn't read properly

Quote:
they type !buy it /notices them the items.. then they type Blue_Potion and it says "you have just brought a Blue_Potion"


You didn't say they had to type !buy blue_potion, but whatever.

on *:TEXT:!buy*:#:{
if ( $2 == Blue_Potion ) {
notice $nick You have bought a blue potion!
}
if ( $2 == Crystal_Blue_Potion ) {
notice $nick You have bought a crystal blue potion!
}
if ( $2 == $null ) {
msg $chan Welcome $nick What would you like to by?
notice $nick Blue Potion 500coins Restores 10HP
notice $nick Crystal Blue Potion 1000coins Restores 50HP
notice $nick Heal Potion 3000coins Restores 150HP
notice $nick Elixer of Heal 10000coins Restores 5000HP
}
}

Posted By: Coca_Bear Re: If's Question - 10/01/03 02:08 PM
Ok again i dont think you understand what i'm tryint to get at...

!buy
It lists the items then can buy.
after they have type !buy they can purchase items. "Blue_Potion" this buys the potion.. But i only want the items to be purchased AFTER they have !buy and it lists the items.

Does this make any sense? or you still dont understand..

Coca-Bear
Posted By: Collective Re: If's Question - 10/01/03 02:14 PM
on *:TEXT:!buy*:#:{
set %buying $+ $nick 1
msg $chan Welcome $nick What would you like to by?
notice $nick Blue Potion 500coins Restores 10HP
notice $nick Crystal Blue Potion 1000coins Restores 50HP
notice $nick Heal Potion 3000coins Restores 150HP
notice $nick Elixer of Heal 10000coins Restores 5000HP
}
on *:TEXT:Blue_Potion:#:{
if ( $eval(% $+ buying $+ $nick,2) == 1 ) {
notice $nick You have bought a blue potion!
unset %buying $+ $nick 1
}
}

When someone types !buy it sets a variable to say they are buying something.

When someone types blue_potion it unsets the variable and tells them they have bought it...if this isn't it I give up..
Posted By: Coca_Bear Re: If's Question - 10/01/03 02:21 PM
This is EXACTLY what i wanted.. thank you VERY much.. :P :P blush

Coca-Bear
Posted By: Collective Re: If's Question - 10/01/03 02:23 PM
Sorry it took me so long lol
© mIRC Discussion Forums