rand() expects parameter 1 to be long ???

C++, C#, Java, PHP, ect...
Post Reply
tomtefan
Posts: 46
Joined: Tue Jun 22, 2010 12:46 pm

rand() expects parameter 1 to be long ???

Post by tomtefan »

iam a noob, so i cant see whats wrong yet.


rand() expects parameter 1 to be long, string given in c:\blablabla

thats the warning i get on this line..



$creaturetext1 = $creatureinfo3['text1'];
$creaturetext2 = $creatureinfo3['text2'];

$attack = rand($creatureinfo3['text1'],$creatureinfo3['text2']);

CODE LINE:
echo "The " . $creature . " Rush towards you and " . $attack . " and hits " . $location ." .<br>";



it cant be to long in DB. cuz i have it Varchar 77 and iam only using around 20-30 letters for a hit text.


What the line SHOULD do is:


The bear........Rush towards you and....Attacks with its giant razor sharp claws,....and hits.....your Head.
$creature............................................(hittext1 or hittext2)...................................($location)

iam trying to flip around stuff, but i cant see whats wrong still.
Most likely something easy.

any help whould be nice :D
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: rand() expects parameter 1 to be long ???

Post by hallsofvallhalla »

because you rand() is only used for numbers, I am assuming craturetext is a characters and not numbers

Instead have the query select one of the texts by RAND.
tomtefan
Posts: 46
Joined: Tue Jun 22, 2010 12:46 pm

Re: rand() expects parameter 1 to be long ???

Post by tomtefan »

hmm, ye, figured it should be something like that.
but thats good also, cuz then i can put in more random hittexts :lol:

whould get boring to only have 2 random texts per monster.
think i will try and put in 5+ different ones atleast to get it more alive.


damn, wish i was more heavy on php coding. i just got more ideas about hittexts. damnit.

anyways ty for answer. :D
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: rand() expects parameter 1 to be long ???

Post by hallsofvallhalla »

i used it in quests of crocania

this is if we get a random encounter and no buildings are there

Code: Select all

$randevent = rand(0,100);
if ($randevent < 14 && $mapinfo3['stature'] ==  'none')
{
$event = 1;
$eventinfo="SELECT * from events where level<='$playerinfo3[level]' AND map = '$map' order by rand() limit 1";
      $eventinfo2=mysql_query($eventinfo) or die("Could not get event");
 $eventinfo3=mysql_fetch_array($eventinfo2);
  
}
i basically have a table in the DB called events, it has level of event and text of the event...along with other non important things(loot, creature id, ect..)

this way you just print the random event

Code: Select all

echo $eventinfo3['description'];
tomtefan
Posts: 46
Joined: Tue Jun 22, 2010 12:46 pm

Re: rand() expects parameter 1 to be long ???

Post by tomtefan »

i went along and did this with my random attack texts.


$attack = $randattack;
$randattack = rand(1,5);

if ($randattack <= 2)
{$attack = $creatureinfo3['text1'];}
elseif ($randattack <= 3)
{$attack = $creatureinfo3['text2'];}
elseif ($randattack <= 4)
{$attack = $creatureinfo3['text3'];}
elseif ($randattack <= 5)
{$attack = $creatureinfo3['text4'];}

echo "The " . $creature . " Rush towards you and " . $attack . " and hit your " . $location ." .<br>";


How ever i get a: Notice: Undefined variable: randattack
The random text works, but the warning is annoying.

how can i fix that ?
User avatar
Chris
Posts: 1581
Joined: Wed Sep 30, 2009 7:22 pm

Re: rand() expects parameter 1 to be long ???

Post by Chris »

if ($randattack <= 2)
elseif ($randattack <= 3)
Erm.. that means it's going to do something really weird here.. because.. basically that's saying:

if randattack is greater or equal to 2 do something, else if randattack is greater or equal to 3 do something..

So say randattack is 4.. well that's both grater than 2 and 3.. so both instances are true.

The warning you're getting is because you're calling a variable that doesn't exist.

Here's how it works:

Code: Select all

<?php
echo $variable;
?>
This should show a warning because $variable doesn't exist.

You would need to first check if it does exist:

Code: Select all

<?php
if( empty($variable) )
{
    echo 'Nope not there.. sorry';
}
else
{
   echo $variable;
}
 
Fighting for peace is declaring war on war. If you want peace be peaceful.
tomtefan
Posts: 46
Joined: Tue Jun 22, 2010 12:46 pm

Re: rand() expects parameter 1 to be long ???

Post by tomtefan »

ok.. ty. will check it when i get back home. :D


yey.. fixed it by: instead of < or > just using == :lol:

$randattack = rand(1,4);

if ($randattack == 1)
{$attack = $creatureinfo3['text1'];}

elseif ($randattack == 2)
{$attack = $creatureinfo3['text2'];}
Post Reply

Return to “Coding”