Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   confusion on array and random() (https://forums.graalonline.com/forums/showthread.php?t=134269722)

khortez 12-15-2014 05:18 PM

confusion on array and random()
 
someone was asking for help on this (probably have an idea of what you're thinking or going to say about that but anyway...)


they wanted to make it so whenever they pressed a key, a random image would be selected every time. I suggested putting the images in an array and using random(); referencing the array list. but what i attempted actually didn't work. and i'm not sure why. any clue on what happened? here is my attempt

i won't use their actual image for this example since they're all strings anyway.

PHP Code:

function onCreated(){
  
temp.images = { "test1",  "test2"}; 
  
temp.random int(random(temp.images[0], temp.images[1]));
  echo(
temp.random);


i also tried something a friend of mine did. which was

PHP Code:

function onCreated(){
  
temp.images = { "test1""test2" };
  
temp.random int(random(0temp.images.size() ));
  echo(
temp.random);


i also put the data in echo directly. but no matter how i did it they all came out as: -1, 0, -1

or: 1, 0.

are those correct outputs? was i wrong in expecting it to output the images name or whats happening?

cbk1994 12-16-2014 04:34 AM

random(a, b) returns a random number in the range [a, b).

So what you probably want to do is take the number you got in your second example and use it as the index of your array.

PHP Code:

function onCreated(){ 
  
temp.images = { "test1""test2" }; 
  
temp.random temp.images[int(random(0temp.images.size()))];
  echo(
temp.random);



khortez 12-16-2014 05:48 AM

Suddenly i can see how dumb my second attempt looks. I didn't even refer anything from the array at that point. aside from it's size anyway. but anyways thanks. i can see what i did wrong/should have done.

xAndrewx 12-16-2014 06:13 AM

Instead of text I would've used your method. Creating 26 images (or however many keys you want) and using int(random(0, 27))

khortez 12-16-2014 08:29 PM

Quote:

Originally Posted by xAndrewx (Post 1733180)
Instead of text I would've used your method. Creating 26 images (or however many keys you want) and using int(random(0, 27))


Ah. if i understand what you're saying correctly, he wasn't trying to use a lot of the keys. he wanted to use onWeaponFired() for his project. the array was just meant to contain his images names. :P


he was trying to create a bomb that would randomly change images each time he pressed D. using putbomb2();


Note:

just for quick reference, when i say "he" i meant the guy i was trying to help.

MysticalDragon 12-18-2014 05:37 AM

Quote:

Originally Posted by khortez (Post 1733178)
Suddenly i can see how dumb my second attempt looks. I didn't even refer any, thing from the array at that point. aside from it's size anyway. but anyways thanks. i can see what i did wrong/should have done.

There is also a simpler method then this that will give you the same results.

PHP Code:

function onCreated(){ 
  
temp.images = { "test1""test2"}; 
  
temp.random randomstring(temp.images);
  echo(
temp.random);



khortez 12-19-2014 12:03 AM

Quote:

Originally Posted by MysticalDragon (Post 1733294)
There is also a simpler method then this that will give you the same results.

PHP Code:

function onCreated(){ 
  
temp.images = { "test1""test2"}; 
  
temp.random randomstring(temp.images);
  echo(
temp.random);



And yet... another little tidbit that was off in cyberspace until this moment.. how do you guys remember all these prefixes and what not?

thanks though i got two methods to use now

cbk1994 12-19-2014 07:01 AM

Quote:

Originally Posted by MysticalDragon (Post 1733294)
There is also a simpler method then this that will give you the same results.

PHP Code:

function onCreated(){ 
  
temp.images = { "test1""test2"}; 
  
temp.random randomstring(temp.images);
  echo(
temp.random);



Was randomstring ever fixed? Last post I can find about it is from December 2012 indicating it's still broken:

Quote:

Originally Posted by xXziroXx (Post 1710365)
It's not been fixed, I tried it a few days ago.

If it's still broken, I'd avoid it entirely (even if you're aware of how it's broken). You never know when somebody will change something without realizing you were relying on a broken function.

If it was fixed, I wouldn't use it until v6 is mandatory (or is it already?), since it's broken v5 clientside.

MysticalDragon 12-19-2014 10:47 AM

Quote:

Originally Posted by cbk1994 (Post 1733332)
Was randomstring ever fixed? Last post I can find about it is from December 2012 indicating it's still broken:



If it's still broken, I'd avoid it entirely (even if you're aware of how it's broken). You never know when somebody will change something without realizing you were relying on a broken function.

If it was fixed, I wouldn't use it until v6 is mandatory (or is it already?), since it's broken v5 clientside.

It's not broking we use it Heavily on delteria, if it's broking please elaborate how? Seems to work fine for me.

OutPut

Weapon/GUI-script tempMD added/updated by Carlito
test2
Weapon/GUI-script tempMD added/updated by Carlito
test1

Mis-Read your response, didn't know it was broking on V5 Clientside. Will test sometime on that theory.

Edit: Also confirmed it works on clientside on V6.
PM: received from (npcserver):Kingdoms (Server)
test1
test2
test2
test2
test2
test1
test2

cbk1994 12-19-2014 11:56 PM

Quote:

Originally Posted by MysticalDragon (Post 1733335)
It's not broking we use it Heavily on delteria, if it's broking please elaborate how? Seems to work fine for me.

Try this on both clientside and serverside. I have no idea if it's still broken, though:

PHP Code:

temp.arr = {"foo"};

for (
temp.0temp.10temp.i++)
  echo(
temp.": " randomstring(temp.arr)); 

If it's broken, you'll find that half the time it returns an empty string instead of the only element in the array.

callimuc 12-20-2014 01:57 PM

Quote:

Originally Posted by khortez (Post 1733316)
And yet... another little tidbit that was off in cyberspace until this moment.. how do you guys remember all these prefixes and what not?

you'll recall them all the time, kinda like learning a new language. also you can find some functions by using /scripthelp keyword on rc, while keyword is some part of the functions name or description. that way you can find some stuff or parameters of a function

MysticalDragon 12-21-2014 05:06 AM

Serverside Output

Weapon/GUI-script RandomBrokeing added/updated by Carlito
0: foo
1: foo
2: foo
3: foo
4: foo
5: foo
6: foo
7: foo
8: foo
9: foo


ClientSide Output

PM: received from (npcserver):Kingdoms (Server)
0: foo
1: foo
2: foo
3: foo
4: foo
5: foo
6: foo
7: foo
8: foo
9: foo

Works fine

cbk1994 12-21-2014 04:30 PM

Quote:

Originally Posted by MysticalDragon (Post 1733421)
Works fine

Glad to see, and looks like v5 can no longer be downloaded either.


All times are GMT +2. The time now is 08:42 AM.

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