Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Send a param with catchEvent? (https://forums.graalonline.com/forums/showthread.php?t=134262321)

MrOmega 03-05-2011 06:41 PM

Send a param with catchEvent?
 
is it possible to send a param with catchEvent?

such as I have these in a GuiShowImgCtrl of a character

PHP Code:

thiso.catchEventname"onMouseDown""RotateChar");  
thiso.catchEventname"onRightMouseDown""RotateChar"); 

in the function 'rotateChar' I can't determine which button was clicked as leftmousedown returns false.

I know I could make two short functions but I was wondering if I could combine them. Both attempts when I try to send a param fails to get the event, my question is moreso how to determine which mouse button was pressed?

cbk1994 03-05-2011 07:02 PM

afaik it's not possible without separate functions

use this instead of name when you're catching events.

PHP Code:

thiso.catchEventname"onMouseDown""RotateChar");  
thiso.catchEventname"onRightMouseDown""RotateChar"); 

should be

PHP Code:

thiso.catchEventthis"onMouseDown""RotateChar");  
thiso.catchEventthis"onRightMouseDown""RotateChar"); 

the name stuff only works through some silly guessing stuff in the engine (same reason profile = "ProfileName" works?).

MrOmega 03-06-2011 12:36 AM

Quote:

Originally Posted by cbk1994 (Post 1634678)
the name stuff only works through some silly guessing stuff in the engine (same reason profile = "ProfileName" works?).

So is using 'this' just more efficient than 'name'? Or is it just a preference choice and trivial?

cbk1994 03-06-2011 12:51 AM

Quote:

Originally Posted by MrOmega (Post 1634721)
So is using 'this' just more efficient than 'name'? Or is it just a preference choice and trivial?

It's not personal preference, it's the proper way to do it. You're giving it a string when it's expecting an object.

Proof, from a wiki article written by Stefan:

PHP Code:

//#CLIENTSIDE
function onCreated() {
  
// Retrieve the playlist via http
  
this.req requestURL("[link removed]");
  
catchevent(this.req,"onReceiveData","onData");



salesman 03-06-2011 01:39 AM

I'm pretty sure when you catch an event you'll get the same arguments as when you just handle the event directly (i.e. MyControl.onMouseDown(...)). And the first argument for onMouseDown() is the mouse button iirc:
PHP Code:

thiso.catchEvent(this"onMouseDown""RotateChar");

//...

function RotateChar(butt) {
  if (
temp.butt == "right") {
    
// stuff
  
}



0PiX0 03-06-2011 03:26 AM

Why does the wiki show it as:
Quote:

catchevent(str,str,str)
adds an event handler for the specified object name and event, third parameter is the name of the function which receives the event (first parameter of the event will be the object for which the event occured)
http://www.graal.net/index.php/Creat...ient/TGraalVar

cbk1994 03-06-2011 03:30 AM

Quote:

Originally Posted by 0PiX0 (Post 1634756)

The wiki is wrong. It also endorses such travesties as using quotes around a GUI profile.

I suppose I could be wrong about this, but it seems very unlikely to me that Stefan would accept the name of an object rather than the object. It's like sending the player's account to a function instead of the player object—it doesn't make sense.

In addition,

Quote:

Originally Posted by scripthelp
TGraalVar.catchevent(str, str, str) - adds an event handler for the specified object and event, third parameter is the function which receives the event (first parameter of the event will be the object for which the event occured)


WhiteDragon 03-06-2011 03:31 AM

It would be funny if it coerced the object into its name, and then back into an object to actually apply the relevant changes to it in order to catch the event.

MrOmega 03-06-2011 06:25 AM

My whole question is, if it works, why is it allowed? I do use quotes for guiProfile names too, just purely because it highlights the names. Which I like.

WhiteDragon 03-06-2011 06:33 AM

Quote:

Originally Posted by MrOmega (Post 1634803)
My whole question is, if it works, why is it allowed?

GS2 is dynamically typed, so values are coerced based on whatever type they need to be. It's usually a Bad Idea™ to rely on the coercing though because it is sometimes unclear what is happening.

For example, you can check if a value exists like this:
PHP Code:

if (temp.someValue) {
// ...


However, what is really happening there, is temp.someValue is being coerced to a boolean (true/false). It will actually coerce to false in the case of say, temp.someValue = "0" or temp.someValue = 0, thus making the if statement not work even though the value does exist.

Basically, relying on the coercing can make strange bugs crop up in edge cases that are hard to find until they actually break something. It's usually best to be explicit.


I personally dislike dynamically typed languages and think they should slowly fade out of existence.


All times are GMT +2. The time now is 09:02 AM.

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