The online racing simulator
Beginner problem: Button Click error (unknown button)
Hello, this is my first time I'm trying to do something in PRISM, so excuse my noobness.

Basically I'm trying to make a clickable button and then show a msg after clicking on that button, nothing more. My code:


<?php 
class flame_test extends Plugins
{
    const 
URL 'http://lfsforum.net/forumdisplay.php?f=312';
    const 
NAME 'Flame Test';
    const 
AUTHOR 'Martin Kapal';
    const 
VERSION PHPInSimMod::VERSION;
    const 
DESCRIPTION 'Description';

    public function 
__construct()
    {
        
$this->registerPacket('onState'ISP_STA);
    }

    public function 
onState(IS_STA $STA)
    {
        
IS_BTN()->UCID(0)->ClickID(2)->ReqI(1)->T(50)->L(50)->W(30)->H(10)->BStyle(ISB_DARK ISB_LEFT ISB_CLICK 1)->Text('Test button')->registerOnClick($this'onBtnClick')->Send();
    }
    
    public function 
onBtnClick()
    {
        
IS_MSX()->Msg('Clicked a button!')->Send();
    }
}
?>

And when I try to click that button, the console says:
Error: Received click for unknown button!
object(IS_BTC)#10 (8) {
["Size":protected]=>
int(8)
["Type":protected]=>
&int(46)
["ReqI"]=>
int(1)
["UCID"]=>
int(0)
["ClickID"]=>
int(2)
["Inst"]=>
int(8)
["CFlags"]=>
int(1)
["Sp3":protected]=>
int(0)
}
array(0) {
}

... and the message does not get displayed.

I hope someone helps
Closed temporarily, I think I've found a solution, I'll look at it when I come back on Sunday...
Re-opened... well I cannot find any solution to this
Not sure if you tried this or not, but this should work...


<?php 
    
public function onBtnClick(IS_BTC $BTC)
    {
        
IS_MSX()->Msg('Clicked a button!')->Send();
    }
?>

What version of PRISM are you using exactly?
Using the Button class should be a better option. By the way, I'm at work, so I've not tested this code, and from my understanding that Button class is not working in the current version of PRISM, so it would be better to test with version 0.4.2.


<?php 
class flame_test extends Plugins
{
    const 
URL 'http://www.lfsforum.net/showthread.php?t=78721';
    const 
NAME 'Flame Test';
    const 
AUTHOR 'Martin Kapal';
    const 
VERSION '0.0.1';
    const 
DESCRIPTION 'Button Test Phase';

    public function 
__construct()
    {
        
$this->registerPacket('onState'ISP_STA);
    }

    public function 
onState(IS_STA $STA)
    {
        
$BTN = new Button(0'Test''Flame');
        
$BTN->T(50)->L(50)->W(30)->H(10)->BStyle(ISB_DARK ISB_LEFT 1);
        
$BTN->Text('Test button')->registerOnClick($this'onBtnClick')->Send();
    }
    
    public function 
onBtnClick(IS_BTC $BTC)
    {
        
IS_MSX()->Msg('Clicked a button!')->Send();
    }
}
?>

Thank you for the replies.

It really seems that the Button class does not work in 0.4.3. I tried it with 0.4.2 and it worked OK, even without that IS_BTC parameter in that case.

Anyway, I started to like PRISM, the only concern I have now that it is actually impossible to create an insim app for local usage and publish it as an EXE file, like it's usual other programming languages. But yes, that's why we actually have insim apps in C#, Java and others :P
Quote from Dygear :

<?php 
        $BTN
->T(50)->L(50)->W(30)->H(10)->BStyle(ISB_DARK ISB_LEFT 1);
?>


Sorry to derail the thread but could someone care to explain that line?
Quote from hyntty :Sorry to derail the thread but could someone care to explain that line?

It's called method chaining. Each method returns an instance of the object the method was called on, which allows you to chain the method calls together in this way. The code snippet just instantiates a new BTN object, then sets its various properties.
Quote from Flame CZE :the only concern I have now that it is actually impossible to create an insim app for local usage and publish it as an EXE file

That's by design. I want people sharing their code. It benefits the whole community.
Now I have a little problem. I've created a little app for local usage with some clickable buttons, so I set hostname to 127.0.0.1. It works OK in offline mode but when I go to multiplayer, I cannot click any buttons - they do not react . I have set UCID to 0 for all buttons I set.

Do I have get the UCID of the current local connection when in multiplayer or what?
Well, insim.txt says
Quote :UCID; // connection's unique id (0 = host)

So when you're in SP, you are the host and zero works. In MP, 0 is presumably reserved for the remote host and you should use the UCID assigned to your connection. Maybe.
If I'm not mistaken, UCID tells LFS which guests will see a specific button. When it's set to 0, only local host will see the button, 255 sends button to all guests.

I'd check if your application is actually getting IS_BTC packets.
#13 - PoVo
Quote from NotAnIllusion :Well, insim.txt says

So when you're in SP, you are the host and zero works. In MP, 0 is presumably reserved for the remote host and you should use the UCID assigned to your connection. Maybe.

That's correct.

When the connection joins the server you retrieve the UCID from the IS_NCN packet event and use it later to send out the buttons.
Alright guys thanks.

I've made a test plugin which displays a button and the button UCID is either 0 in local game or UCID if in multiplayer. It detects the UCID nicely but the button does not get displayed when in multiplayer.


<?php 
php
class testPlugin extends Plugins
{
    const 
URL 'http://www.lfsforum.net/showthread.php?t=78721';
    const 
NAME 'Test Plugin';
    const 
AUTHOR 'Martin Kapal';
    const 
VERSION '0.0.1';
    const 
DESCRIPTION 'desc';

    private 
$connID 0;

    public function 
__construct()
    {
        
$this->registerPacket('onConnect'ISP_NCN);
        
$this->registerPacket('onDisconnect'ISP_CNL);
        
$this->registerPacket('onMessageOut'ISP_MSO);
    }

    public function 
onMessageOut(IS_MSO $MSO)
    {
        if (
$MSO->UserType == && $MSO->Msg == 'button')
        {
            
$this->onLoad();
        }
    }

    public function 
onConnect(IS_NCN $NCN)
    {
        if (
$NCN->Flags == 0)
        {
            
$this->connID $NCN->UCID;
            
ButtonManager::removeButtonsByGroup(0'test');
            
ButtonManager::removeButtonsByGroup($this->connID'test');
            
$this->onLoad();
            
            
console('conn id ' $this->connID ' | ' $NCN->UName);
        }
    }

    public function 
onDisconnect(IS_CNL $CNL)
    {
        if (
$CNL->UCID == $this->connID)
        {
            
$this->connID 0;
            
ButtonManager::removeButtonsByGroup(0'test');
            
ButtonManager::removeButtonsByGroup($this->connID'test');
            
$this->onLoad();
            
            
console('disconn id ' $this->connID "\n");
        }
    }

    public function 
onLoad()
    {
        
ButtonManager::removeButtonsByGroup(0'test');
        
ButtonManager::removeButtonsByGroup($this->connID'test');
            
        
console('load id ' $this->connID);
        
        
$btn_close = new Button($this->connID'testbtn''test');
        
$btn_close->Text('Click me');
        
$btn_close->L(120)->T(40)->W(20)->H(20)->BStyle ISB_CLICK;
        
$btn_close->registerOnClick($this'click');
        
$btn_close->send();
    }
    
    public function 
click(IS_BTC $BTC)
    {
        
console('click');
    }
}
?>

Well I'm just guessing here. Insim.txt says
Quote : byte Flags; // bit 2 : remote

which means that you should do a bitwise comparison to check if bit 2 is set or not.


<?php 
if (~$NCN->Flags 4)
    
// connection is local/host
?>

Or something like that. If bits 0 or 1 are set, Flags == 0 will fail. I have no idea if the two other bits are used for anything, though. Probably a wild goose chase

Alternatively, since the lowest possible value with bit 2 set is 4, you could do

<?php 
if (! ($NCN->Flags >= 4) )
    
// connection is not remote
?>

Though that assumes none of the bits beyond 2 are used..

Sorry if this ends up being a waste of time
Well as I said, I can detect my UCID, because the flag is 0, where as other's flags are 4.
I have no knowledge of PRISM or PHP for that matter, so I might be completely wrong here, but calling onLoad() to send buttons to a new connection seems kinda wrong to me. I didn't really get the "this->connID" business, was your plan to use multiple instances of the plugin, one for each connection?

Anyway, does this work?


<?php 
    
public function onConnect(IS_NCN $NCN
    { 
        if (
$NCN->Flags == 0
        { 
            
$this->sendButton($NCN->UCID);
             
            
console('conn id ' $NCN->UCID ' | ' $NCN->UName); 
        } 
    } 

    public function 
onDisconnect(IS_CNL $CNL
    { 
            
ButtonManager::removeButtonsByGroup($CNL->UCID'test'); 
            
            
console('disconn id ' $this->connID "\n");  
    } 

    public function 
onLoad() 
    {   
        
sendButton(0);  
        
console('Test plugin loaded'); 
    }

    private function 
sendButton($_UCID)
    {
        
$btn_close = new Button($_UCID'testbtn''test'); 
        
$btn_close->Text('Click me'); 
        
$btn_close->L(120)->T(40)->W(20)->H(20)->BStyle ISB_CLICK
        
$btn_close->registerOnClick($this'click'); 
        
$btn_close->send(); 
    }
?>

What version of PRISM are you using? 0.4.3 has a bug still and it will be fixed by 0.4.4 along with a whole slew of other bugs.
Quote from Dygear :What version of PRISM are you using? 0.4.3 has a bug still and it will be fixed by 0.4.4 along with a whole slew of other bugs.

Running 0.4.2.
Quote from MadCatX :I have no knowledge of PRISM or PHP for that matter, so I might be completely wrong here, but calling onLoad() to send buttons to a new connection seems kinda wrong to me. I didn't really get the "this->connID" business, was your plan to use multiple instances of the plugin, one for each connection?

No, I just want to show all the buttons to the local user.

I tried your version but still I the button doesn't show up in multiplayer. Or when I start in local mode (e.g. entry screen), it works but as soon as I enter a multiplayer host, the button gets inactive (no message on click). Here is my current code:


<?php 
php
class testPlugin extends Plugins
{
    const 
URL 'http://www.lfsforum.net/showthread.php?t=78721';
    const 
NAME 'Test Plugin';
    const 
AUTHOR 'Martin Kapal';
    const 
VERSION '0.0.1';
    const 
DESCRIPTION 'desc';

    public function 
__construct()
    {
        
$this->registerPacket('onConnect'ISP_NCN);
        
$this->registerPacket('onDisconnect'ISP_CNL);
    }

    public function 
onConnect(IS_NCN $NCN)
    {
        if (
$NCN->Flags == 0)
        {
            
$this->sendButton($NCN->UCID);

            
console('conn id ' $NCN->UCID ' | ' $NCN->UName);
        }
    }

    public function 
onDisconnect(IS_CNL $CNL)
    {
        
ButtonManager::removeButtonsByGroup($CNL->UCID'test');
        
$this->sendButton(0);
        
console('disconn id ' $CNL->UCID "\n");
    }

    public function 
onLoad()
    {
        
$this->sendButton(0);
        
console('Test plugin loaded');
    }
    
    private function 
sendButton($_UCID)
    {
        
$btn_close = new Button($_UCID'testbtn''test');
        
$btn_close->Text('Click me');
        
$btn_close->L(120)->T(40)->W(20)->H(20)->BStyle ISB_CLICK;
        
$btn_close->registerOnClick($this'click');
        
$btn_close->send();
    }
    
    public function 
click(IS_BTC $BTC)
    {
        
console('click');
    }
}
?>


FGED GREDG RDFGDR GSFDG