Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 12-15-2014, 05:18 PM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
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?
Reply With Quote
  #2  
Old 12-16-2014, 04:34 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
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);

__________________
Reply With Quote
  #3  
Old 12-16-2014, 05:48 AM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
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.
Reply With Quote
  #4  
Old 12-16-2014, 06:13 AM
xAndrewx xAndrewx is offline
Registered User
xAndrewx's Avatar
Join Date: Sep 2004
Posts: 5,260
xAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud ofxAndrewx has much to be proud of
Instead of text I would've used your method. Creating 26 images (or however many keys you want) and using int(random(0, 27))
__________________
Reply With Quote
  #5  
Old 12-16-2014, 08:29 PM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
Quote:
Originally Posted by xAndrewx View Post
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.
Reply With Quote
  #6  
Old 12-18-2014, 05:37 AM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is just really niceMysticalDragon is just really nice
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
Quote:
Originally Posted by khortez View Post
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);

__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]



Reply With Quote
  #7  
Old 12-19-2014, 12:03 AM
khortez khortez is offline
PrototypeX
khortez's Avatar
Join Date: Dec 2008
Posts: 91
khortez will become famous soon enough
Quote:
Originally Posted by MysticalDragon View Post
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
Reply With Quote
  #8  
Old 12-19-2014, 07:01 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by MysticalDragon View Post
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 View Post
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.
__________________
Reply With Quote
  #9  
Old 12-19-2014, 10:47 AM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is just really niceMysticalDragon is just really nice
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
Quote:
Originally Posted by cbk1994 View Post
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
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]



Reply With Quote
  #10  
Old 12-19-2014, 11:56 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by MysticalDragon View Post
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.
__________________
Reply With Quote
  #11  
Old 12-20-2014, 01:57 PM
callimuc callimuc is offline
callimuc's Avatar
Join Date: Nov 2010
Location: Germany
Posts: 1,015
callimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to beholdcallimuc is a splendid one to behold
Quote:
Originally Posted by khortez View Post
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
__________________
MEEP!
Reply With Quote
  #12  
Old 12-21-2014, 05:06 AM
MysticalDragon MysticalDragon is offline
Global Administration
MysticalDragon's Avatar
Join Date: Oct 2002
Location: Lynn Ma
Posts: 883
MysticalDragon is just really niceMysticalDragon is just really nice
Send a message via AIM to MysticalDragon Send a message via MSN to MysticalDragon
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
__________________
~Delteria Support
~Playerworld Support
~PWA Chief
http://support.toonslab.com
[email protected]



Reply With Quote
  #13  
Old 12-21-2014, 04:30 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by MysticalDragon View Post
Works fine
Glad to see, and looks like v5 can no longer be downloaded either.
__________________
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 12:31 PM.


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