Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Bool detection (https://forums.graalonline.com/forums/showthread.php?t=77919)

gemini2 12-14-2007 12:54 AM

Bool detection
 
PHP Code:

function onCreated() {
  
temp.array = {"float""string""object"};
  
temp.object = new TStaticVar();
       
object.array = {"foo""bar"};
       
object.float 3.14;
       
object.integer 7;
       
object.bool true;
       
object.string "baz";
  
  
temp.prefix "temp.object.";
  
  
temp.dynamic object.getDynamicVarNames();
  
temp.vars filter(object.getVarNames(), dynamic);
  
  
temp.lines = {"<objecttype='"object.objecttype() @"'>"};
  
temp.0;
  for(
temp.var : dynamic) {
    
1;
    if (
makevar(prefix @ var).type() == 3) {
      
lines.add(addSpace("<array='"@ var @"'>"i));
      
2;
      
temp.0;
      for(
temp.var2 makevar(prefix @ var)) {
        
lines.add(addSpace("<attr["@"]="makevar(prefix @ var)[c] @">"i));
        
++;
      }
      
lines.add(addSpace("</array>"1));
    }else{
    
temp.suffix = array[makevar(prefix @ var).type()];
    if (
suffix == "float") {
      if (
makevar(prefix @ var) == int(makevar(prefix @ var))) suffix "integer";
    }
    
lines.add(addSpace("<"suffix @"='"@ var @"'>" makevar(prefix @ var) @"</"suffix @">"i));
    }
  }
  
temp.lines.add("</objecttype>");
  for(
temp.line lines) {
    echo(
line);
  }
}
function 
filter(ab) {
  
temp.out a;
  for(
temp.b) {
    
temp.ind a.index(i) > -1;
    if (
ind > -1out.remove(i);
  }
  return 
out;
}
function 
addSpace(strint) {
  
temp.0temp.out "";;
  while(
int*2) {
    
out @= "  ";
    
+= 2;
  }
  
out @= str;
  return 
out;


I have gotten Chompy to teach me alot, so I've been getting better :D

anyways, the output of the file above:

PHP Code:

<objecttype='TStaticVar'>
  <array=
'array'>
    <
attr[0]=foo>
    <
attr[1]=bar>
  </array>
  <
integer='bool'>1</integer>
  <
float='float'>3.14</float>
  <
integer='integer'>7</integer>
  <
string='string'>baz</string>
</
objecttype

NOW, it says:
NPC Code:

<integer='bool'>1</integer>



Is there a way I can check if it is a bool, because if I check with obj.type() it returns integer :(
(true = 1, false = 0)

Chompy is away for a week, and this has been on my mind, so I thought I could post it here to get the answer. So.., anyone know?

coreys 12-14-2007 01:35 AM

I see you're using Chompy's style as well. lol

Novo 12-14-2007 02:40 AM

PHP Code:

if ( suffix == "integer" && value in {falsetrue} )
  
suffix "Boolean"

Ideally, just keep it integer. Booleans are integers in Graal, all your doing is adding complexity where it isn't needed.

Kyranki 12-14-2007 03:55 AM

if (val == true/false) works I believe.

Twinny 12-14-2007 04:22 AM

But that doesn't necessarily reflect a true boolean type. As long as var isn't 0/null/"" , it's true. A true boolean type can only be 0 or 1 and Graal doesn't seem to have this type.

Kyranki 12-14-2007 05:03 AM

Chompy is always parsing something.

Kyranki 12-14-2007 05:37 AM

PHP Code:

public function isbool(val)
return (
val.type() == && val in {truefalse})? true false

Basically, what Novo said.

Angel_Light 12-14-2007 06:12 AM

in true scripting you can replace FALSE with 0 and TRUE with 1. This is a binary principle. Since FALSE is a value of nothing, "0" is used and vice versa. JUst check for a 1 or 0.

coreys 12-14-2007 06:24 AM

Quote:

Originally Posted by Kyranki (Post 1363709)
Chompy is always parsing something.

Oh man, that's so true it's almost not funny. But it is funny. Very, very funny.

xAndrewx 12-14-2007 09:35 AM

PHP Code:

public function isBool(val) {
  return (
temp.val in {truefalse});  


should cover it

Twinny 12-14-2007 09:37 AM

I think the best thing to do is to stop pretending that Graal has boolean data types.

Crow 12-14-2007 02:49 PM

Quote:

Originally Posted by coreys (Post 1363677)
I see you're using Chompy's style as well. lol

Thats actually a pretty common style..

gemini2 12-14-2007 04:39 PM

Well, the problem I thought would arise was when I was going to make it into an object again.. But I guess I'll just do that you guys said, to check if its either 0 or 1..

And about me making a parsing function is basically that Chompy told me that making parsing stuff is good practice :O

But some way to detect if it was a bool or an integer would be nice, for example:
PHP Code:

temp.bool true;
echo(
temp.bool.isBool());
// would echo 1

temp.integer 1;
echo(
temp.integer.isBool());
// would echo 0 


Anyways, thanks guys

Admins 12-14-2007 06:14 PM

Float, integer and boolean values are all floating point numbers in Graal script.

This is made for keeping the scripting simple, since you don't need to convert values all the time. You can just do "myvar = 1.5; myvar++;" and the result will be 2.5, while in some languages you can only use "++" on integers. You can check "if (myvar)" instead of doing "if (bool(mybar))", you can do "myvar = true + true + true" to get three.

Basicly the operator automatically converts the variable into the right type if necessary: @ is converting to string, "+" is converting to a number, "&&" is convert to boolean values, "&" is converting to integer (since bit operations only work fine on integer).


All times are GMT +2. The time now is 05:00 AM.

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