Graal Forums  

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

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-16-2010, 11:55 PM
Zeltino Zeltino is offline
Gov'nah
Zeltino's Avatar
Join Date: May 2005
Location: USA
Posts: 239
Zeltino will become famous soon enough
Using Random() {Major Help needed}

Hey guys,

I'm trying to put together a script that will make an NPC warp from one location to a list of ~100 locations on a server. The locations will be pre-determined. That means that I will have a list of the level, x, and y position that the NPC will warp to. I just need to know how to make the NPC randomly select between these locations, and how it will warp there, etc. It will randomly choose from the specified list of levels every 5 minutes.

I have very little scripting knowledge. I'm just trying to pull this together last-minute (needs to be done by tonight, ew). Any help would be tremendously helpful.

So far, I've started scripting the NPC in a class script (I assumed this would be better than a level NPC).

This is all I have so far:
NPC Code:
function onCreated(){ 
setimg("block.png");
this.locationsarray = {"test.nw","30","30"}, //Random Movement
{"zeltest.nw","40","40"};

scheduleevent("0.05","MoveMe","");
}

function onMoveMe(){
temp.i=int(random(0,99));
this.location=this.locationsarray[temp.i];
scheduleevent("300","MoveMe","");
}

function onPlayertouchsme(){
}


Almost certain none of the above is correct.

Can anyone help me <3?
__________________
Developer
Crescent Pirates
Email: [email protected]
Reply With Quote
  #2  
Old 03-17-2010, 12:08 AM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
I suggest putting it in a DBNPC since you're in a hurry. Doing so might be kinda ugly but eliminates a range of possible errors.

You can define the array of locations like this:
PHP Code:
this.locations = {
  {
"test.nw"3030},
  {
"foo.nw"1040},
}; 
The array indeces range from 0 to array size - 1, so you'd generate a random index like this:
PHP Code:
temp.index int(random(0this.locations.size())); 
This will work regardless of how many locations you've put into the array.

I think warpto() is what you use for NPCs.
PHP Code:
temp.newLocation this.locations[temp.index];
this.warpto(temp.newLocation[0], temp.newLocation[1], temp.newLocation[2]);
echo(
this.name " warped to " temp.newLocation); 
I think the rest of the code you posted should work.

Last edited by DrakilorP2P; 03-17-2010 at 12:11 AM.. Reason: Corrected error.
Reply With Quote
  #3  
Old 03-17-2010, 12:14 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
Edit: Appears I'm a couple minutes late..

For arrays inside arrays (multi-dimensional) you need to wrap { }'s around them like so:

PHP Code:
temp.array = {
  {
"innerarray""member"},
  {
"another""array"}
}; 
You can then use random, and the size of the array to pick a random array out of it.

PHP Code:
function onCreated() {
  
temp.examplearray = {
    {
"a""b""c"},
    {
"d""e""f"}
  };
  
temp.randomindex int(random(0temp.examplearray.size()));
  
temp.example temp.examplearray[temp.randomindex];

int(value) - Rounds the number (technically it converts it to an integer, but this is the result) to the number below it. I.e: int(3.8) = 3

random(float min, float max) - Picks a number between the minimum and maximum number, it will/should never equal the max value.

Therefore int(random(0, temp.array.size())) will return a valid index in the array.

To move NPCs you have to use warpto which accepts the same parameters as setlevel2.

warpto(str levelname, float x, float y);

When we put it altogether we get a nice little function like so:

PHP Code:
function warp() {
  
// Declare Locations Array
  
temp.locations = {
    {
"onlinestartlocal.nw"3030},
    {
"someotherlevel.nw"3030}
  };
  
// Select a location
  
temp.location temp.locations[int(random(0temp.locations.size()))];
  
// Warpto Location
  
warpto(temp.location[0], temp.location[1], temp.location[2]);
  
// Update (Forces NPC to save changes, probably not needed but whatever)
  
this.trigger("NPCUpdate""");

__________________
Quote:
Reply With Quote
  #4  
Old 03-17-2010, 08:56 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
HTML Code:
function warp() { 
  // Declare Locations Array 
  temp.locations = { 
    {"onlinestartlocal.nw", 30, 30}, 
    {"someotherlevel.nw", 30, 30} 
  }; 

  // Select a location 
  temp.location = randomstring(temp.locations); 

  // Warpto Location 
  warpto(temp.location[0], temp.location[1], temp.location[2]); 

  // Update (Forces NPC to save changes, probably not needed but whatever) 
  this.trigger("NPCUpdate", ""); 
}  
I just used randomstring
__________________
Reply With Quote
  #5  
Old 03-17-2010, 01:05 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 xAndrewx View Post
I just used randomstring
randomstring has some weird problems when working with arrays with one object or less. I try to avoid it whenever possible because it just tends to lead to problems in the future.
__________________
Reply With Quote
  #6  
Old 03-17-2010, 08:43 PM
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
Oh, such as...?
__________________
Reply With Quote
  #7  
Old 03-17-2010, 10:40 PM
WhiteDragon WhiteDragon is offline
Banned
Join Date: Feb 2007
Posts: 1,002
WhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to beholdWhiteDragon is a splendid one to behold
Quote:
Originally Posted by xAndrewx View Post
Oh, such as...?
I don't know precisely what Chris is referring to, but I have my own issues with it. The way randomstring works is something like this:

Your array of:
PHP Code:
  temp.locations = { 
    {
"onlinestartlocal.nw"3030}, 
    {
"someotherlevel.nw"3030
  }; 
Turns into
PHP Code:
  temp.locations = { 
    
"onlinestartlocal.nw,30,30,"
    
"someotherlevel.nw,30,30," 
  
}; 
Which randomstring then runs on, and returns a single value such as
PHP Code:
temp.value "onlinestartlocal.nw,30,30,"
When you then run temp.value[0] it changes temp.value into
PHP Code:
temp.value = {"onlinestartstartlocal.nw""30""30"}; 
And you continue on with your script.



The issue with this is all the implicit casting between arrays and strings, and it is essentially an abuse of the internal handling of data in GS2.

In the midst of all this complexity, there is a lot of room for error and inefficiency.
Reply With Quote
  #8  
Old 03-17-2010, 11:08 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 xAndrewx View Post
Oh, such as...?
PHP Code:
temp.arrayExample = {"one"};

for (
temp.010++) {
  echo(
": " randomString(arrayExample));

echoed the following on Era with the latest NPC-server:

Quote:
Originally Posted by RC
0:
1:
2:
3: one
4:
5:
6: one
7: one
8:
9: one
__________________
Reply With Quote
  #9  
Old 03-18-2010, 03:38 AM
adam adam is offline
http://wiki.graal.us/
adam's Avatar
Join Date: Nov 2001
Posts: 2,247
adam has a spectacular aura aboutadam has a spectacular aura about
Send a message via AIM to adam
Yes, when using an array with one element, it is totally broken. Seems to work with more than one well enough. And I didn't have any trouble with multi-dimensional arrays. But I would avoid it just on the single element array issue.
__________________
Rogue Shadow (TCN)(NAT)(Global Development Team)

For development help, contact the patrons of the #graaldt irc channel below, I am usually there.
Click Here to Join IRC Chat Now! -- irc.freenode.net Channel: #graaldt
Quote:
<Dustyshouri> no, RogueShadow is always talking about scripts lol
<Dustyshouri> in fact, he pretty much brought Graal back as a topic single-handedly
Reply With Quote
  #10  
Old 03-19-2010, 09:35 AM
Zeltino Zeltino is offline
Gov'nah
Zeltino's Avatar
Join Date: May 2005
Location: USA
Posts: 239
Zeltino will become famous soon enough
Hey guys, thanks for the help. Got it working .
__________________
Developer
Crescent Pirates
Email: [email protected]
Reply With Quote
  #11  
Old 03-19-2010, 09:45 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
Quote:
Originally Posted by cbk1994 View Post
PHP Code:
temp.arrayExample = {"one"};

for (
temp.010++) {
  echo(
": " randomString(arrayExample));

echoed the following on Era with the latest NPC-server:
Oh I see what you mean, however it's looking for at least two or more options.
__________________
Reply With Quote
  #12  
Old 03-19-2010, 12:35 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 xAndrewx View Post
Oh I see what you mean, however it's looking for at least two or more options.
Yeah, but in the future it might not be, so I would avoid it just for the sake of making the code require the least maintenance. In the future if it was changed it might result in that glitch if you didn't recheck the code, and it's best to avoid potential glitches like that from the start.
__________________
Reply With Quote
Reply


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 10:43 AM.


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