Help with events

pax
Posts: 7
Joined: Wed May 06, 2009 1:32 am

Help with events

Post by pax »

Not being a trained coder, I'll probably have lots of questions as I work on building a system here. I'll try to keep the lot of them in this thread, unless it's suggested otherwise.

For now,

Code: Select all

if affAnorexia == 0 then
	if balanceSip == 1 then
		if 8 * currentHealth < 7 * maxHealth then
			send ("sip health")
			balanceSip = 0
			lastSipped = "HEALTH"
		else 
			if 4 * currentMana < 3 * maxMana then
				send("sip mana")
				balanceSip = 0
				lastSip = "MANA"
			end
		end
	end
	if balanceToadstool == 1 then
		if 8 * currentHealth < 5 * maxHealth or 2 * currentMana < maxMana then
			send("outr toadstool")
			send("eat toadstool")
			balanceToadstool = 0
		end
	end
end
Why, when I try to save this as an event, does Mudlet tell me
Lua syntax error:[string "if affAnorexia == 0 then ..."]:16: attempt to compare number with string
I'm sure that all the variables I'm using are declared as part of a global script as integers with a value of zero.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: I'm confused...

Post by Heiko »

As there is no event section in the manual yet:

1. To make an event handler you need to wrap your code in

Code: Select all

function myEventHandlerOnLowEnergy( eventName )

         ( ... your code goes here ... )

end
2. put the name of the event (the string that is being used in raiseEvent("lowEnergy")) into the event list of the script that is going to act as an event handler for this event. Note that many scripts can define event handlers for your "lowEnergy" event in which case they'll all be called if such an event is raised.

3. And now comes the critical part: the *NAME* of the script has to be *identical* with the name of the event handler function inside this script. Otherwise the event system cannot know which script to call.
Consequently: script name=myEventHandlerOnLowEnergy
event (to be put in the list) = lowEnergy
Then add above code into the script of the script. Then you are done.

Hope this helps.

PS: What I assume happens in your case is that your code gets run as part of the global script space at a time before the variables have been defined. Normally, there is a strict order in mudlet items that items get processed from top to bottom, but scripts are different in this respect, as well as that scripts normally need proper function definitions unless you explicitely want to make this code globally available e.g. for variables. Without a function definition your code simply gets run once and never again unless you press compile -> necessity for function declarations.

pax
Posts: 7
Joined: Wed May 06, 2009 1:32 am

Re: I'm confused...

Post by pax »

Another addition to this. I apologize for being dense or plain stupid, but I don't get what I"m doing wrong with this.

In a trigger that fires on my prompt in Imperian:

Code: Select all

currentHealth = matches[2]
maxHealth = matches[3]
currentMana = matches[4]
maxMana = matches [5]

balanceStatus = matches[9]

raiseEvent("sipper")
I have a script related to this.

Script name: eventHandlerOnPrompt
Registered Event Handlers: sipper

Code: Select all

function eventHandlerOnPrompt()
	if affAnorexia == false then
		if balanceSip then
			if currentHealth < (3/4 * maxHealth) then
				send("sip health")
				balanceSip = false
			elseif currentMana < (5/8 * maxMana) then
				send("sip mana")
				balanceSip = false
			end
		elseif balaceToadstool and (currentHealth < (5/8 * maxHealth)) or (currentMana < (1/2 * maxMana)) then
			send("outr toadstool")
			send("eat toadstool")
			balanceToadstool = false
		end
	end
end
Can you tell me why this isn't working? I think I've done what you've suggested...

Thanks in advance

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: I'm confused...

Post by Heiko »

Your event handler function doesn't get called because your definition is missing a parameter. The number of parameters in the raiseEvent & eventHandler functions need to be identical.

As you have raiseEvent("sipper") a proper event handler function needs to look like this:

Code: Select all

function eventHandlerOnPrompt( event )
   [your code goes here]
end
Even if you don't do anything with the event-parameter you still need to declare it. The rationale behind this is that you sipper event handler function might also handle events such as "sipperOn", "sipperOff", "getSomeWhiskey" whatever.

pax
Posts: 7
Joined: Wed May 06, 2009 1:32 am

Re: I'm confused...

Post by pax »

Code: Select all

function eventHandlerOnPrompt(event)
       if affAnorexia == false then
          if balanceSip then
             if currentHealth < (3/4 * maxHealth) then
                send("sip health")
                balanceSip = false
             elseif currentMana < (5/8 * maxMana) then
                send("sip mana")
                balanceSip = false
             end
          elseif balaceToadstool and (currentHealth < (5/8 * maxHealth)) or (currentMana < (1/2 * maxMana)) then
             send("outr toadstool")
             send("eat toadstool")
             balanceToadstool = false
          end
       end
    end
Tried that, and it still doesn't work.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: I'm confused...

Post by Heiko »

short howto:
* script name & event handler function name needs to be *identical*
* event needs to be in the event handler list
* event function needs the same parameters as used in raiseEvent()

look at this little event demo package:
Attachments
eventDemo.xml
(3.43 KiB) Downloaded 545 times

pax
Posts: 7
Joined: Wed May 06, 2009 1:32 am

Re: I'm confused...

Post by pax »

gah. I was being stupid. The function was working, it just wasn't doing anything. Apparently it works all right when I take the math out of the if conditions. Is there a way to leave it there, or should I do math outside of the conditions before plugging it in, or am I just using an incorrect syntax?

Thanks for the help and your patience so far. I really appreciate it.

Andy

User avatar
Vadi
Posts: 5048
Joined: Sat Mar 14, 2009 3:13 pm

Re: Help with events

Post by Vadi »

In the script editor, there is a 'Debug' button - it comes handy in cases like this to track down an error.

I don't see an error in your script though for the maths in ifs, and it shouldn't matter where they are. Maybe something is wrong with variables?

pax
Posts: 7
Joined: Wed May 06, 2009 1:32 am

Re: Help with events

Post by pax »

So far in the debug all I've seen is that everything that I'm trying to capture gets captured and that my prompt trigger runs successfully. There are no reported errors, and no mention of anything but the initial trigger, from what I can tell.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Help with events

Post by Heiko »

This is obviously some Lua syntax error or misconception.
Please have a look at how "and" and "or" expressions are handled in Lua. It's different from what you'd normally expect:

http://lua-users.org/wiki/NumbersTutorial
http://lua-users.org/wiki/ExpressionsTutorial
http://lua-users.org/wiki/LuaDirectory

If you can't get it to work, make a package of the parts of the scripts that cause problems and we'll help you.
The little piece of code you showed is not enough as it misses the initializations code.

Post Reply