Gothic Online Forums
addEvent and function of class - Printable Version

+- Gothic Online Forums (https://archive.gothic-online.com)
+-- Forum: Scripting (English) (https://archive.gothic-online.com/forum-11.html)
+--- Forum: Scripting Help (https://archive.gothic-online.com/forum-12.html)
+--- Thread: addEvent and function of class (/thread-1626.html)



addEvent and function of class - Xardas0327 - 22.05.2016

Code:
class Menu
{
    constructor()
    {
          addEvent("onClick", login);
           show();
    }
    
    function show()
    {
         setCursorVisible(true);
    }
    
    function hide()
    {
         setCursorVisible(false);
       }
    
    function login(button, x, y, wheel)
    {
        hide();
    }
}
If I use this class, the error: hide() is not exist.

what's wrong? Is it impossible?


RE: addEvent and function of class - Bimbol - 22.05.2016

(22.05.2016, 19:43)Xardas0327 Wrote:
Code:
class Menu
{
    constructor()
    {
          addEvent("onClick", login);
           show();
    }
    
    function show()
    {
         setCursorVisible(true);
    }
    
    function hide()
    {
         setCursorVisible(false);
       }
    
    function login(button, x, y, wheel)
    {
        hide();
    }
}
If I use this class, the error: hide() is not exist.

what's wrong? Is it impossible?

Is simply, because squirrel right now calling method as function. Difference between function and method is that, method need object for call.
Example:
function(); // Calling function
obj.method(); // Calling method, as u see object is needed

But addEvent second argument is a function, so in timer code it look like:
login();
not
obj.login();

Squirrel don't know, which object should be called, and login method is called as function, so in code we looking for function hide, not method Smile

Maybe is hard to understand, but is logic.

To fix this problem, create function outside the class.


RE: addEvent and function of class - Xardas0327 - 22.05.2016

Of course. I understand. I don't really known the Squirrel and i didn't find the solution only.
Thx the answer.


RE: addEvent and function of class - Galin - 23.05.2016

You can write your own EventHandler, which can call the function from objects too. What you need send to him it sholud be name of function as string, and object address (in class just send "this")

my EventHandler work like that


Code:
evetns_onHit <- EventHandler(); //Global object

function onHit(targetID, killerID){
events_onHit.call(targetID, killerID);
}


And i add to him like this


Code:
in class
function hookCallbacks(){
events_onHit.add("myOwnOnHit", this);
}

function myOwnOnHit(targetID, killerID){}



RE: addEvent and function of class - Xardas0327 - 23.05.2016

It is a good idea.
thx.