Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Quick help with a small script. (https://forums.graalonline.com/forums/showthread.php?t=134262924)

Jiroxys7 04-22-2011 01:22 AM

Quick help with a small script.
 
I had this old section of script that was giving me problems. Eventually I tore it down and rescripted the section, making it much much smaller as well as making it far more compatible with my current systems. However the problem I experienced before seems to have popped up once again. The issue is that for example, if I send through as the variable passedeffects:
"100:cause:Bleed:3:auto:460:bleed","75:cause:Disea se:15:auto:-15,120:spread_disease"
edit: I'm not sure why the forums are putting a space in "Disease" but disregard that.

only the first thing will be returned if I do:
echo(effect);

In this case in F2 I will only get:
"100:cause:Bleed:3:auto:460:bleed"
and not the other effect. Naturally, this means that the script is not processing the rest of the line. However, when I do:
echo(passedeffects);
on the same line, all effects will be outputted to F2.

In short, not matter how many buffs I send through, it will only ever use the first one on the list.
I'm sure it's some silly thing that I've been overlooking for the past two days, but perhaps one of you guys can help me figure out exactly what it is.
Here is the script:

PHP Code:

for(0passedeffects.tokenize(",").size(); ++){
    
temp.effect passedeffects.tokenize(",")[i].tokenize(":");
    
temp.effectroll random(0,100);
    if(
effectroll <= effect[0] && effect[1] == "cause"){
      
BuffSystem.addbuff(effect[2],effect[3],effect[4],effect[5],effect[6]);
    }
  } 

Any help is greatly appreciated.

cbk1994 04-22-2011 01:39 AM

Multi-dimensional arrays are your friend.

Jiroxys7 04-22-2011 02:04 AM

Quote:

Originally Posted by cbk1994 (Post 1644814)
Multi-dimensional arrays are your friend.

I'm unsure whether you're hinting to the solution or questioning my use of :'s.

If you're hinting at the solution-
I'm pretty sure the problem line is:
temp.effect = passedeffects.tokenize(",")[i].tokenize(":");
I'm not sure why temp.effect isn't taking the entire array though. However, come to think of it, I've sometimes had to initiate the former as an array. In this case, I'd need to do something like:
PHP Code:

for(i=0;i<passedeffects.tokenize(",").size();i++){
  
temp.effect.add("");


before proceeding. However, I'll have to hold off on trying that until my other staff member logs on again so I can look at his F2 logs.

If you're questioning the use of :'s -
The reason why I use the :'s is simple. take the disease line.
"75:cause:Disease:15:auto:-15,120:spread_disease"

the section in bold is inserted by the buff system as an array. If I were to replace the :'s with ,'s, then I would have to create some new form of separation or else the 120 would go to the wrong section, and "spread_disease" would be entered into an invalid field. And personally, I like that look better than:
"75,cause,Disease,15,auto,-15:120,spread_disease" or something similar.
edit:again, don't know why it's putting weird spaces.

DustyPorViva 04-22-2011 02:22 AM

I think he's saying why parse data like that when you can just use multidim arrays? For example you have:
PHP Code:

effects = {
  
"100:cause:Bleed:3:auto:460:bleed",
  
"75:cause:Disea se:15:auto:15,120:spread_disease"


Which with a multidim array could be:
PHP Code:

effects = {
  {
100,"cause","Bleed",3,"auto",460,"bleed"},
  {
75,"cause","Disease",15,"auto","15,120","spread_disease"}


And you avoid needing to do all the extra parsing. Alternatively to cover the damage problem, you can do:

PHP Code:

effects = {
  {
100,"cause","Bleed",3,"auto",460,"bleed"},
  {
75,"cause","Disease",15,"auto",{15,120},"spread_disease"}


Then, when you start working with the damage check the variable type, if it's numeric apply regularly, if it's an array apply min-max. You can do:
PHP Code:

array.add({75,"cause","Disease",15,"auto",{15,120},"spread_disease"}); 

you know?

Jiroxys7 04-22-2011 02:47 AM

Eh, sorry. There was an error in my examples. It's actually a -15, and that part represents the speed reduction. The 120 represents the example damage. Though it really doesn't change the point.

I'll change that later today when I get back to my server. Though it really won't do any good if the script is still broken.

Jiroxys7 04-22-2011 04:42 AM

I've narrowed down the issue somewhat.

If i do:
PHP Code:

echo(passedeffects.size()); 

It does echo 3 when I have 3 things coming through. This is so the for loop runs that many times of course. However, it gets weird..
When I do:
PHP Code:

echo(passedeffects[0]);
echo(
passedeffects[1]);
echo(
passedeffects[2]); 

It will return those 3 things in F2. But oddly..
PHP Code:

echo(passedeffects[i]); 

Will stop after only the first line. It seems like the for loop quits for some reason after only the first run through. What could be causing this?

fowlplay4 04-22-2011 05:08 AM

Quote:

Originally Posted by Jiroxys7 (Post 1644842)
It will return those 3 things in F2. But oddly..
PHP Code:

echo(passedeffects[i]); 

Will stop after only the first line. It seems like the for loop quits for some reason after only the first run through. What could be causing this?

You're probably doing something accidentally like..

PHP Code:

for (temp.0temp.whatevertemp.i++) {
  
/* Code */
  
for (temp.0temp.whatevertemp.i++) {
    
/* broke itself */
  
}


Feel free to include the whole function.

Jiroxys7 04-22-2011 05:45 AM

The code I posted is actually the only section that handles that. In a function on the attacker's side, I have things added to this.effectslist via:
this.effectlist.add("blah");
which gets sent over and comes out on the victim's side as passedeffects.

I'll post the whole code later. I'm taking a break from it right now. If not, I'll post it tomorrow. Before that, I'm going to try changing the "i" to something else, as I do have multiple separate loops in the functions. But none are actually contained in another loop (and when I do, I make sure to substitute the "i" because I'm afraid of it somehow conflicting.)

Jiroxys7 04-23-2011 06:20 AM

Well it looks like the problem was that i was using "i". I thought only nesting for loops in for loops would bring up that kind of problem, but it looks like as long as they're in the same function, it's best to give them separate names. Changing "i" to "ef" fixed the problem. Thanks for the help :)

fowlplay4 04-23-2011 06:26 AM

I bet if you changed it to temp.i instead of just i it would work as expected.

WhiteDragon 04-23-2011 06:31 AM

Right, without the 'temp.' before it, i becomes a global variable and causes all sorts of muck and crazy behavior!


Easy rule: always prefix your variables and function calls, either with this or temp.

This way it is very clear when you are actually working with global variables and functions (due to lack of prefix).

Jiroxys7 04-23-2011 06:53 AM

Quote:

Originally Posted by WhiteDragon (Post 1645039)
Right, without the 'temp.' before it, i becomes a global variable and causes all sorts of muck and crazy behavior!


Easy rule: always prefix your variables and function calls, either with this or temp.

This way it is very clear when you are actually working with global variables and functions (due to lack of prefix).

Oh man, and I just got done kicking that habit because variables without "temp." looked nicer..

WhiteDragon 04-23-2011 06:57 AM

Quote:

Originally Posted by Jiroxys7 (Post 1645042)
Oh man, and I just got done kicking that habit because variables without "temp." looked nicer..

Another option is to only use temp. with the first reference to the variable name, but I don't like this option. I like it when I can look at a variable and immediately know where it's coming from.

Deas_Voice 04-25-2011 06:31 PM

just a question, not to hijack the thread, but, i've got this weird habbit of doing temp.foo when i'm assigning something to the variable, and foo if im checking it for something.

i.e.
PHP Code:

temp.foo = {10,20};
if (
10 in foo)  {
  for (
temp.var : {"food","desert","pluffy"}) {
    
temp.foo.add( var );
  }


is this wrong too?

fowlplay4 04-25-2011 06:54 PM

Quote:

Originally Posted by Deas_Voice (Post 1645504)
just a question, not to hijack the thread, but, i've got this weird habbit of doing temp.foo when i'm assigning something to the variable, and foo if im checking it for something.

If something sets foo globally it may interfere. All I can really suggest is when in doubt put temp. in front.

cbk1994 04-25-2011 09:49 PM

Quote:

Originally Posted by Deas_Voice (Post 1645504)
just a question, not to hijack the thread, but, i've got this weird habbit of doing temp.foo when i'm assigning something to the variable, and foo if im checking it for something.

i.e.
PHP Code:

temp.foo = {10,20};
if (
10 in foo)  {
  for (
temp.var : {"food","desert","pluffy"}) {
    
temp.foo.add( var );
  }


is this wrong too?

Once you declare a variable as temp you don't need to use the prefix again. You should, though—take it from someone who leaves it out and is trying to kick that habit after doing it for years.


All times are GMT +2. The time now is 05:33 PM.

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