Results 1 to 3 of 3

Thread: saftly detecting type of a script variable

  1. #1

    Default saftly detecting type of a script variable

    I was wondering if there is some sort of check to detect the type of a variable ?
    Like string, int, vector or entity.

    I have a concept for a reliably working type detector which I am going to code unless there is something that can do this already.
    The purpose is that in the coop mod parameters sometimes need to be a entity or a int, I don't want to have two functions and I don't want to convert all the time.

  2. #2
    Developer RyBack's Avatar
    Join Date
    Apr 2014
    Location
    In Front of the screen
    Posts
    1,603

    Default

    reborn has typeof.
    Normal mohaa doesn't. Casting can be use to tell if variable is of that cast type, except for separate cases where a string "10" could be casted to an int 10 (you can use .size to tell it was string tho).

  3. #3

    Default

    Well, we are trying to get the mod working without anything else needed (regardless if that is a smart thing or not), we can upgrade or modify things later.

    After some thinking and testing I came up with this code.
    There might be room for improvement, feel free to comment.
    So far I am pleased with the result, this will make many problems go away.

    PHP Code:
    //return type of variable
    //performs a series of checks to determine the variable type ?
    //type: 0-NIL, 1-string,2-entity,3-vector,4-int/float
    //-1 could not detect type
    //=========================================================================
    returnVarType local.var:{
    //=========================================================================
        
    if(local.var == NIL){ end 0 }

        
    local.stringified string(local.var)
        
    local.stringifiedSize local.stringified.size
        
    //var is of type entity or string
        
    if(local.var.size == 0){
        
    //it can be either a empty string or a entity
            
    if(local.stringifiedSize == ){ end 1 }
            else{ 
    end 2 }
        }
    //var is type of integer, entity or vector or singleletter string
        
    else if(local.var.size == 1){
            
    local.firstChar string(local.stringified[0])
        
    //assume it is a vector if it is in brackets
            
    if( local.stringified[0] == "(" && local.stringified[local.stringifiedSize-1] == ")" ){
                
    end 3
            
    }
        
    //note: cant detect entities with a targetname < 2 chars
            
    if(local.stringifiedSize 1){
            
    //entity targetnames never start with a number or a colon not a number will be 0 when converted
                
    if(    int(local.firstChar) == && int(local.stringified) == && local.stringified != "0.000" && local.stringified[0] != "."){
                    
    end 2
                
    }            
                if( 
    waitthread isFloat == || int(local.stringified) != 0){
                    
    end 4
                
    }
            }else{
                if(
    int(local.stringified) != || local.stringified == "0" || local.stringified != "0.000"){
                    
    end 4
                
    }
            }
        
    //fallback to string
            
    end 1
        
    }
    //must be a string
        
    else{
            
    end 1
        
    }
    }
    end -1

    //check if it is a float, game makes sure float always has 3 digits after the dot
    //=========================================================================
    isFloat local.var:{
    //=========================================================================
        
    local.stringified string(local.var)
        if( 
    local.stringified == "0.000" ){ end 1 }
        if( 
    float(local.stringified) == ){ end 0 }
        
        
    local.dotAt = -1
        local
    .afterDot 0
        
    for(local.i=0;local.i<local.stringified.size;local.i++){
            if(
    local.afterDot != -1){
                
    local.afterDot++
            }
            if(
    local.stringified[local.i] == "."){
                
    local.dotAt local.i
                local
    .afterDot 0
            
    }
        }
        
        if(
    local.afterDot == 3){ end 1    }
    }
    end 0 
    Last edited by chrissstrahl; September 15th, 2019 at 09:02 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •