Tutorial: Introduction to Lua scripting - Page 3 - ED Forums
 


Notices

Reply
 
Thread Tools Display Modes
Old 05-29-2014, 09:43 PM   #21
piXel496
Member
 
piXel496's Avatar
 
Join Date: Oct 2013
Posts: 351
Default

Quote:
Originally Posted by Joyride View Post
Short version: how to you make a function name dynamic?

Long version:

I need to create an event handler within a function, but may have several of these event handlers going concurrently for different "CAS taskings". In all of the examples I've seen event handlers are global. Thus, is there a way with lua to create a dynamic variable name for the event handler?

For example, the event handler checks for blue-on-blue shots and is called BlueDamageHandler. This is called within function "CAS_1" which has a variable "Task" (function CAS_1(Task). So, task #1 is running and calls BlueDamageHandler to start checking for blue-on-blue shots.

Meanwhile, CAS task #2 is activated using the same overall function, and also needs to call on this BlueDamageHandler but with different units to check on, etc. So how do I make the damage handler specific to each task? What I want to do is have the damage handler be called "BlueDamageHandler_01" or something for the first task, "BlueDamageHandler02" for the second, and so on.

Hi Joyride,

If I understand the idea you want to script. I think you have to keep your eventhandler global, since it wil fire on that level in in the ED-world. And you have to create the dynamic requirement to make your script behave the way you like within the eventhandler. you could make a filter within your eventhandler that distiquishes for instance "if unitSet_1 then" and "if unitSet_2 then" or "if with_as_much_flavours_as_you_like then". To make it readable you could even send each if, to there own function outside the eventhandler.





.
__________________

Last edited by piXel496; 05-29-2014 at 09:45 PM. Reason: just if
piXel496 is offline   Reply With Quote
Old 05-30-2014, 12:32 AM   #22
Joyride
Veteran
 
Joyride's Avatar
 
Join Date: Apr 2009
Location: Colorado, USA
Posts: 513
Default

piXel496:

There will be too many variables to accommodate in a single event handler, I could potentially have many running at the same time checking blue-on-blue (example) for numerous group/unit combinations. I just want to be able to create a unique event handler for each of the those to run globally, but concurrently. All is generated by a single script though, thus the need to pass a variable into the name of the event handler function itself.

Getting back to LUA, what I want to do is similar to how you’d pass a variable into a text string.
Variable = blah
trigger.action.outText(‘message is: ‘ .. variable .., 10)

So that produces text message = “message is: blah”

How can I similarly pass a variable into a function or table name?
For example, the basic start of the event handler function creates the table:

BlueDamageHandler = {}

How do I make that dynamic? Using the LUA text string as a (bad) example…is something like this possible?

Variable = Task01
BlueDamageHandler .. Task01 .. = {}

So that when this event handler is created, it is create as “BlueDamageHandlerTask01”?
Joyride is offline   Reply With Quote
Old 05-30-2014, 01:35 AM   #23
ajax
Member
 
ajax's Avatar
 
Join Date: Feb 2010
Posts: 476
Default

Wouldn't all the functions used for the various event handlers have to have already been defined before you assign them as event handlers?

That is:

function BlueHandler1(eventdata)
...
end

function BlueHandler2(eventdata)
...
end

etc.

must be defined before

world.addEventHandler(BlueHandler1)

world.addEventHandler(BlueHandler2)

etc.

It seems to me that you need a single event handler that incorporates a "dispatcher" of sorts that initiates a coroutine or schedules another function to actually do the "tasks".

Now, my knowledge of coroutine usage is next to nothing, but from what I've read it might be more suited for what your trying to do.
ajax is offline   Reply With Quote
Old 05-30-2014, 01:49 AM   #24
ajax
Member
 
ajax's Avatar
 
Join Date: Feb 2010
Posts: 476
Default

Also,

Quote:
I could potentially have many running at the same time checking blue-on-blue (example) for numerous group/unit combinations.
I don't think event handlers work that way. They are event driven not polling functions.
ajax is offline   Reply With Quote
Old 05-30-2014, 03:19 PM   #25
Joyride
Veteran
 
Joyride's Avatar
 
Join Date: Apr 2009
Location: Colorado, USA
Posts: 513
Default

Quote:
Originally Posted by ajax View Post
Also,

I don't think event handlers work that way. They are event driven not polling functions.
The example would be where one event handler is checking to see if Group A is fired upon by client A-10s and another comes on line later to check if Group B is fired upon by client A-10s. The problem is that one event handler can't handle both, because when the first one is created the second group "Group B" is not known yet. Plus, all of the other variables used are specific and within a unique function created for that local CAS scenario.

So, without posting hundreds of line of code to explain that further, the question still stands if functions can have variables within their name to make the function name dynamic. Seeing stuff on LUA sites related to "loadstring" and use if "_G". Sounds like what I want to do it possible, just don't understand the syntax.

Here is an example that gets very close to what I am doing, but I don't understand the answers provided in this thread :

http://stackoverflow.com/questions/1...-function-name
Joyride is offline   Reply With Quote
Old 05-30-2014, 04:01 PM   #26
ajax
Member
 
ajax's Avatar
 
Join Date: Feb 2010
Posts: 476
Default

I understand what you are trying to do, but every event handler you create will receive every, all, and the same events. You don't get to choose that this event handler only receives events from say UnitA and that event handler from UnitB.

Last edited by ajax; 05-30-2014 at 06:47 PM.
ajax is offline   Reply With Quote
Old 05-30-2014, 06:39 PM   #27
piXel496
Member
 
piXel496's Avatar
 
Join Date: Oct 2013
Posts: 351
Default

If I was an eventHandler I did not want to be called again to say the same.









.
__________________

Last edited by piXel496; 05-30-2014 at 07:09 PM. Reason: I am completely lost here
piXel496 is offline   Reply With Quote
Old 05-30-2014, 09:25 PM   #28
Joyride
Veteran
 
Joyride's Avatar
 
Join Date: Apr 2009
Location: Colorado, USA
Posts: 513
Default

Let's say eventhandler1 checks "world.event.S_EVENT_HIT" if group A (initiator) shot at group B (target). Then eventhandler2 checks "world.event.S_EVENT_HIT" if group C shot at group D. No reason that those can't run concurrently right?

My desire is to simply make the number at the end of "eventhandler" dynamic based on a variable passed through the defining function. The groups defined in each event handler are defined within separate functions, thus the desire to not jam all the variables into a single event handler.
Joyride is offline   Reply With Quote
Old 05-30-2014, 09:58 PM   #29
xcom
Senior Member
 
xcom's Avatar
 
Join Date: Jun 2012
Posts: 1,392
Default

You can do that in one event handler -
Code:
local eHandler = {}
local groupAhit = {}
local groupDhit = {}

function eHandler:onEvent(e)
 if e.id == world.event.S_EVENT_HIT and Object.getCategory(e.initiator) == Object.Category.UNIT then
  local Igroup = e.initiator:getGroup()
  local IgroupName = Igroup:getName()
  local Tgroup = e.target:getGroup()
  local TgroupName = Tgroup:getName()
  if IgroupName = "groupA" and TgroupName = "groupB" then
   groupAhit[#groupAhit + 1] = TgroupName
  elseif IgroupName = "groupC" and TgroupName = "groupD" then
   groupDhit[#groupDhit +1] = TgroupName
  end
 end
end


world.addEventHandler(eHandler)


I've just written this quickly, there must be better ways but this code will do what you wanted with one event handler and put the hit group name into 2 different tables which you can use later to do whatever.

Last edited by xcom; 05-30-2014 at 10:18 PM.
xcom is offline   Reply With Quote
Old 05-30-2014, 10:33 PM   #30
Joyride
Veteran
 
Joyride's Avatar
 
Join Date: Apr 2009
Location: Colorado, USA
Posts: 513
Default

Thanks guys. I might find a way to make it work that way, but will easily have 50+ if-thens in one function to cover all variations, if not more. Thus the desire for dynamic naming of the function. I also want the group variations to be automatically added if I add more to the miz (drag/drop a single function to cover all this) rather than now having multiple functions to edit when I do.
Joyride is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT. The time now is 07:36 PM. vBulletin Skin by ForumMonkeys. Powered by vBulletin®.
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.