Page 1 of 1

unexpected T_IF why is it not working?

Posted: Sun Mar 30, 2014 3:18 am
by hybris
Hi,

Im trying to do a small RPG and have the creature class hold a body container where I place an body object (containing different slots like haedslot, handslot and so on. I do this way so it will be easy to mix different classes to get new monstertypes..

anyway I want the body container to report the sum of all buffs (like +str or + hp) for the objects it holds to the creature class.

I give the body class all the buff properties and thought i would add for example all $this->diffslot->hitpoints into (body class)->hitpoints and then simply return the bodyclass hitpoints with get_hitpoints(){return $this->hitpoints}

However if I dont do an if_object check and have an empty slot I get error for calling an object parameter where it is no object (something like that) and if I try to check if it is an object i get the unexpected T_IF error.

I cant find out what is wrong.

Code: Select all

class humanoidSlots {
    //-------------Armour Slots--------------------------------
    public $HeadSlot;
    public $EarSlot = array("Right","Left");
    public $NeclaceSlot;
    /../ SHORTENED FOR READABILITY
    private $hitpoints;
    private $manapoints;
    private $actionpoints;
    
    /../ Shortened for readability
  
    private function set_hitpoints(){
        $this->hitpoints = (
            if(is_object($this->HeadSlot)){$this->HeadSlot->get_hitpoints()} +               <-----------This gets me the T_IF error
            if(is_object($this->EarSlot[Right])){$this->EarSlot[Right]->get_hitpoints()}+
            $this->EarSlot[Left]->get_hitpoints()+                                                         <----- This give me the no object error if the slot is empty
            $this->NeclaceSlot->get_hitpoints()+
            /../Shortened
            );
    } 
    public function get_hitpoints(){$this->set_hitpoints(); return $this->hitpoints;}
    
Thanks.

Re: unexpected T_IF why is it not working?

Posted: Sun Mar 30, 2014 4:35 am
by hybris

Code: Select all

$a = 0;
        if(is_object($this->HeadSlot)){$a=$a+ $this->HeadSlot->get_hitpoints();}
        if(is_object($this->EarSlot[Right])){$a=$a+ $this->EarSlot[Right]->get_hitpoints();}
        if(is_object($this->EarSlot[Left])){$a=$a+ $this->EarSlot[Right]->get_hitpoints();}
        $this->hitpoints=$a;
I solved it but why would my first example not work ( where i did if_object...+if_object.. )?

Re: unexpected T_IF why is it not working?

Posted: Sun Mar 30, 2014 5:07 am
by Jonnycake
The first wouldn't work because you can't add two if statements, they're not like functions. If you want to just return a specific value given a certain condition you can use ternary operators, however. Their syntax is as follows:

Code: Select all

$a=(($x==1)? true:false );
The problem with this is that given a situation like yours, it can get really really ugly:

Code: Select all

$a+=((is_object($this->HeadSlot))?$this->HeadSlot->get_hitpoints():((is_object($this->EarSlot[Right]))?$this->EarSlot[Right]->get_hitpoints():((is_object($this->EarSlot[Left]))?$this->EarSlot[Left]:0)));
Now mind you, that will only add one of the objects hitpints, so if you want multiple objects to be "hit" at the same time, that won't do it for you. This code will but it's still ugly:

Code: Select all

$a+=((is_object($this->HeadSlot))?$this->HeadSlot->get_hitpoints():0)+((is_object($this->EarSlot[Right]))?$this->EarSlot[Right]->get_hitpoints():0)+((is_object($this->EarSlot[Left]))?$this->EarSlot[Left]:0);

Re: unexpected T_IF why is it not working?

Posted: Sun Mar 30, 2014 6:00 am
by hybris
Ok thanks Jonnycake,

now I have another problem that I haven't been able to figure out myself..

unexpected T_VARIABLE

Code: Select all

 private function set_param($par){
        $a = 0;
//        $par = "focus";
        if(is_object($this->HeadSlot)){$a=$a+ $this->HeadSlot->get_$par();} 
I tried various things like $f = "get_focus()"; and if(is_object($this->HeadSlot)){$a=$a+ $this->HeadSlot->$f;}

but it doesnt work... if I do a normal set_focus(){ if(is_object($this->HeadSlot)){$a=$a+ $this->HeadSlot->get_focus();} function it works fine

I tried eveything i can think of
get_.$par.();
get_&$par&();
get_&&$par&&();
get_+$par+();

Re: unexpected T_IF why is it not working?

Posted: Sun Mar 30, 2014 6:23 am
by Celauran
hybris wrote:I tried various things like $f = "get_focus()"; and if(is_object($this->HeadSlot)){$a=$a+ $this->HeadSlot->$f;}
You were actually pretty close here.

Code: Select all

$f = 'get_focus';
$this->HeadSlot->$f();
will work fine.

Re: unexpected T_IF why is it not working?

Posted: Sun Mar 30, 2014 12:20 pm
by hybris
Yes sir it does :)

Thanks, saved me like 10000 lines of get_ and set_ :)

I also tried $f = 'focus'; and $this->HeadSlot->get_$f(); but it didnt work.. also $g='get_'.$f.'()'; but it didnt work either... is it possible to do this function with only 'focus' as input or do i have to have get_focus as input?

edit ok solved it by excluding the () from the string... dunno why it solved the problem but it worked so im happy :)

Thanks again to the both of You :)

Re: unexpected T_IF why is it not working?

Posted: Sun Mar 30, 2014 12:43 pm
by Celauran
You may also want to take a look at call_user_func()