Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Randomizing? (https://forums.graalonline.com/forums/showthread.php?t=134266809)

Fysez 07-17-2012 03:59 AM

Randomizing?
 
So, I've been trying to work with making words/ect random.
I understand how to make numbers randomize, But it seems
I cannot figure out other things like words.

What I have an tried to do, Did not work:

PHP Code:

function onPlayerTouchsMe() {
this.chat1 "Hey";
  
this.chat2 "Hi";
  
this.chat3 "How";
  
this.chat4 "Are";
  
this.chat5 "You";
  
this.chat6 "To";
  
this.chat7 "Day?";
  
this.speech this.chat(int(random(1,8)));
  
this.chat this.speech;


Please help and teach me how I can fix this?x_x

cbk1994 07-17-2012 05:24 AM

Your code doesn't make any sense. this.chat isn't a function. How would it know that what you really want is to append the random number to "this.chat" and read the value of that variable?

The best way is to just make an array.

PHP Code:

function onPlayerTouchsMe() {
  
temp.chats = {
    
"Hey",
    
"Hi",
    
"How",
    
"Are",
    
"You",
    
"To",
    
"Day?"
  
};
  
  
this.chat temp.chats[int(random(0temp.chats.size()))];


Somebody will probably post below me and point out that you can sort of use randomString, so let me say preemptively to avoid that; it's not intended for arrays and has some pretty big issues when used with them.

xXziroXx 07-17-2012 05:42 AM

Quote:

Originally Posted by cbk1994 (Post 1699635)
Somebody will probably post below me and point out that you can sort of use randomString, so let me say preemptively to avoid that; it's not intended for arrays and has some pretty big issues when used with them.

It might not have been created with the use of arrays in mind, but the only associated issue with it that I'm aware of is that it requires more than one element to return a value - something Stefan supposedly has fixed.


Quote:

Originally Posted by Stefan (Post 1673119)
Ok will fix that behaviour of randomstring(), it was originally not made for getting arrays as input.


Imperialistic 07-17-2012 05:54 AM

Quote:

Originally Posted by cbk1994 (Post 1699635)
Somebody will probably post below me and point out that you can sort of use randomString

i think we should play the lottery together

cbk1994 07-17-2012 05:55 AM

Quote:

Originally Posted by xXziroXx (Post 1699637)
It might not have been created with the use of arrays in mind, but the only associated issue with it that I'm aware of is that it requires more than one element to return a value - something Stefan supposedly has fixed.

Seems to be working now. Good catch, I never noticed it was fixed.

I would probably avoid it clientside for the time being, I'm guessing it's still broken in v5.

Jiroxys7 07-17-2012 02:31 PM

Try something like this:

PHP Code:

function onPlayerTouchsMe() {
  
this.word1 "Hey";
  
this.word2 "Hi";
  
this.word3 "How";
  
this.word4 "Are";
  
this.word5 "You";
  
this.word6 "To";
  
this.word7 "Day?";
  
this.speech this.word int(random(1,7)+0.5);
  
this.chat this.speech;


I think that's what you need. Or maybe it was this?
PHP Code:

this.word(@ int(random(1,7)+0.5)); 

I switched this.chat# with this.word# just to be on the safe side. I try to avoid getting custom variables mixed up with built-in variables.

Edit: The array approach looks a lot cleaner though. I must have missed cbk's post. Use that method instead.

BlueMelon 07-17-2012 02:35 PM

Quote:

Originally Posted by Jiroxys7 (Post 1699647)
Try something like this:

PHP Code:

function onPlayerTouchsMe() {
  
this.word1 "Hey";
  
this.word2 "Hi";
  
this.word3 "How";
  
this.word4 "Are";
  
this.word5 "You";
  
this.word6 "To";
  
this.word7 "Day?";
  
this.speech this.word int(random(1,7)+0.5);
  
this.chat this.speech;


I think that's what you need. Or maybe it was this?
PHP Code:

this.word(@ int(random(1,7)+0.5)); 

I switched this.chat# with this.word# just to be on the safe side. I try to avoid getting custom variables mixed up with built-in variables.

Edit: The array approach looks a lot cleaner though. I must have missed that post. Use that method instead.

This is alright to do if you are learning how to use variables, but should never be done like that. Look at Chris's post.

cbk1994 07-17-2012 06:48 PM

Quote:

Originally Posted by Jiroxys7 (Post 1699647)
Try something like this:

PHP Code:

function onPlayerTouchsMe() {
  
this.word1 "Hey";
  
this.word2 "Hi";
  
this.word3 "How";
  
this.word4 "Are";
  
this.word5 "You";
  
this.word6 "To";
  
this.word7 "Day?";
  
this.speech this.word int(random(1,7)+0.5);
  
this.chat this.speech;


I think that's what you need. Or maybe it was this?
PHP Code:

this.word(@ int(random(1,7)+0.5)); 

I switched this.chat# with this.word# just to be on the safe side. I try to avoid getting custom variables mixed up with built-in variables.

Neither of these are going to work anyway. With the first, you're appending the random number to the value of this.word, which is undefined, and then saying it. If you wanted to append to the variable name, you'd want to do...

PHP Code:

this.chat this.(@ "word" int(random(18))); 

The second will not work either, this.word is not a function, but you're treating it like one.


All times are GMT +2. The time now is 04:52 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.