Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Future Improvements (https://forums.graalonline.com/forums/forumdisplay.php?f=10)
-   -   array.random() (https://forums.graalonline.com/forums/showthread.php?t=134262434)

fowlplay4 03-16-2011 09:03 PM

array.random()
 
Wouldn't mind seeing something like this:

PHP Code:

function onCreated() {
  
temp.arr = {
    
12345
  
};
  
temp.element temp.arr.random();
  echo(
temp.element); // Echos 1, 2, 3, 4, or 5


Same thing as doing...

PHP Code:

function onCreated() {
  
temp.arr = {
    
12345
  
};
  
temp.element arr[int(random(0arr.size()))];
  echo(
temp.element);



Jiroxys7 03-16-2011 10:18 PM

Maybe also a temp.array.shuffle()? This would randomize the current array, allowing you to 'draw' each individual part in a sequential order.

This would be useful in, for example, a card deck script. You'd put all the cards in temp.cards, then use temp.cards.shuffle(); to randomize them (or temp.cardshuffle = temp.card.shuffle(); to store them in a new array.). You would then be able to grab the randomized variables and leave them in that order or clear the cards that you have drawn.



I ask this as last time I had to randomize an array, I needed to do something like this. I ended up having to take each variable in the array and throw them in random areas in a new array consisting of about a hundred null variables (so that they dont overlap.. too often) along with some other nonsense scripts, then I had to run through THAT, pick up the non-null variable, and then put them in a new array. Somewhere in there I needed to shift variables down a notch which took at least 15+ lines on it's own I'm sure.

tl:dr, it was an exhausting day.

salesman 03-16-2011 10:54 PM

Better yet, let us write our own functions.
Quote:

Originally Posted by cbk1994 (Post 1635010)
Adding methods to default objects would indeed be great. In JavaScript, you can edit object "prototypes":

PHP Code:

// from http://www.flzone.com/go?7678
String.prototype.replace = function(patternreplacement) {
  return 
this.split(pattern).join(replacement);


Another idea which I think would work better for GScript would be if you could create a class with the name "TServerPlayer" and all TServerPlayer objects would inherit those methods. This can already be done by joining classes to the player, but for stuff like TServerLevel and TGraalVar (for strings) it would be invaluable. I would love to be able to add string.replace(a, b).

PHP Code:

Array.prototype.random = function() {
  return 
this[int(random(0this.size()))];
}

// Or define it in class 'Array'
public function random() { ... } 


MattKan 03-18-2011 04:07 AM

Quote:

Originally Posted by fowlplay4 (Post 1636776)
Wouldn't mind seeing something like this:

PHP Code:

function onCreated() {
  
temp.arr = {
    
12345
  
};
  
temp.element temp.arr.random();
  echo(
temp.element); // Echos 1, 2, 3, 4, or 5


Same thing as doing...

PHP Code:

function onCreated() {
  
temp.arr = {
    
12345
  
};
  
temp.element arr[int(random(0arr.size()))];
  echo(
temp.element);



Yeah, that would be neat and useful

MrOmega 03-21-2011 02:50 PM

I thought random could never reach the limit so the last parameter would never be selected?

fowlplay4 03-21-2011 03:38 PM

Quote:

Originally Posted by MrOmega (Post 1638009)
I thought random could never reach the limit so the last parameter would never be selected?

Arrays start at 0, so the last element is always Array.size() - 1. Which is why you can just pass the size to the maximum parameter in random.


All times are GMT +2. The time now is 03:48 PM.

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