XenoBot Forums - Powered by vBulletin

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 22

Thread: Can someone explain Modules a little?

  1. #11
    Bastiat's Avatar
    Join Date
    Aug 2012
    Location
    Vancouver, BC
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hendy View Post
    Below is a description of modules, explanation from @Spectrus

    Code:
    A module is a function that when created, is added to the TIMER_TICK event. It is then executed every 200ms, as is the behavior of the TIMER_TICK. 
    
    To create a Module, simply call the constructor for it: Module.New(name, func). The parameter name is a string which can then be used to modify the status of the module (i.e. Module.Stop(name), etc.). The function is the function that will be called when the TIMER_TICK event occurs.
    
    In essence, a module is as simple as this:
    
    ###############################
    
    -- First create a function for your module.
    function checkCreature()
       local c = Creature.New('Demon')
       if c:isAlive() and c:isOnScreen() then
          print('Run!')
       end
    end
    
    -- Then create a module for that function.
    Module.New('creature check', checkCreature)
    
    ################################
    
    If you would like to control your module at any time, you can start or stop it with Module.Start(name) and Module.Stop(name).
    That somehow gave me a little insight and I rewrote the module and got it running seemingly perfect including Module.Start() and Module.Stop() on labels.

    Now I just need to figure out the whole Channel class. xD I need to start/stop modules by typing stuff in a channel.

  2. #12
    Administrator
    Join Date
    Dec 2010
    Posts
    2,565
    Mentioned
    228 Post(s)
    Tagged
    1 Thread(s)
    Here we goooooo! Explanation time:

    Code:
    The channel class is used to create custom console channels. To better understand how it works, let's first consider what classes are. There are several classes in Xenobot scripting; Self, Creature, Map, Item, etc. When you need to interact with one of these classes you create an object of that class. You do this by calling a constructor function (in most cases something like Creature.New()). Each constructor function for each class requires certain information in order to make your object how you desire. For a channel, that information is a name (shown on the channel tab), a function to be called when a user inputs text, and a function to be called when the channel is closed.
    
    What all this looks like:
    
    ################################
    
    -- note that the onSpeak and onClose functions should be defined before creating the channel.
    
    function onSpeak(channel, message)
         -- receives the channel object that called it and the message that was inputted.
         if (message == 'hi') then
              channel:SendOrangeMessage('Robot', 'Hi!')
         end
    end
    
    function onClose(channel)
         -- receives the channel object that called it
         print(channel:Name() .. ' has been closed.')
    end
    
    -- Now we create the channel.
    local customChannel = Channel.New('My Channel', onSpeak, onClose)
    
    ################################
    
    Some important functions for using channels.
    
    Channel:SendYellowMessage(senderName, message)
    Channel:SendOrangeMessage(senderName, message)
    Channel:Close()
    
    I don't really use any of the other ones... :(
    
    Also, it's important to note that we define the callback functions before creating the channel. If they were after, the channel would be created first and it would not know those functions exist (error plx). Also important is the fact that they are passed the channel that called them. Inside the scope of these functions, always refer to the channel object as it is passed in the parameter, not what you have named the channel object when you created it (in my example, inside the function use channel:SendOrangeMessage() and outside the function use customChannel:SendOrangeMessage()).
    
    Hope it helps!
    Last edited by Spectrus; 03-20-2013 at 09:06 AM.

  3. #13
    Bastiat's Avatar
    Join Date
    Aug 2012
    Location
    Vancouver, BC
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Spectrus View Post
    Here we goooooo! Explanation time:

    Code:
    The channel class is used to create custom console channels. To better understand how it works, let's first consider what classes are. There are several classes in Xenobot scripting; Self, Creature, Map, Item, etc. When you need to interact with one of these classes you create an object of that class. You do this by calling a constructor function (in most cases something like Creature.New()). Each constructor function for each class requires certain information in order to make your object how you desire. For a channel, that information is a name (shown on the channel tab), a function to be called when a user inputs text, and a function to be called when the channel is closed.
    
    What all this looks like:
    
    ################################
    
    -- note that the onSpeak and onClose functions should be defined before creating the channel.
    
    function onSpeak(channel, message)
         -- receives the channel object that called it and the message that was inputted.
         if (message == 'hi') then
              channel:SendOrangeMessage('Robot', 'Hi!')
         end
    end
    
    function onClose(channel)
         -- receives the channel object that called it
         print(channel:Name() .. ' has been closed.')
    end
    
    -- Now we create the channel.
    local customChannel = Channel.New('My Channel', onSpeak, onClose)
    
    ################################
    
    Some important functions for using channels.
    
    Channel:SendYellowMessage(senderName, message)
    Channel:SendOrangeMessage(senderName, message)
    Channel:Close()
    
    I don't really use any of the other ones... :(
    
    Also, it's important to note that we define the callback functions before creating the channel. If they were after, the channel would be created first and it would not know those functions exist (error plx). Also important is the fact that they are passed the channel that called them. Inside the scope of these functions, always refer to the channel object as it is passed in the parameter, not what you have named the channel object when you created it (in my example, inside the function use channel:SendOrangeMessage() and outside the function use customChannel:SendOrangeMessage()).
    
    Hope it helps!
    Hell yeah works like a charm!

    Last edited by Bastiat; 03-21-2013 at 05:55 AM.

  4. #14
    Senior Member Eion's Avatar
    Join Date
    Dec 2011
    Posts
    558
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Is it ok to use "wait(50)" inside modules or do you have to use "module: Delay(50)"? Also, If you call a function from outside the module is it ok to have "wait(50)" in that function or will those commands delay the execution of the main .LUA ?

  5. #15
    Administrator
    Join Date
    Dec 2010
    Posts
    2,565
    Mentioned
    228 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Eion View Post
    Is it ok to use "wait(50)" inside modules or do you have to use "module: Delay(50)"? Also, If you call a function from outside the module is it ok to have "wait(50)" in that function or will those commands delay the execution of the main .LUA ?
    wait(50) will delay the execution of other modules, whereas module:Delay() will yield to other modules until it's ready to start again, but module:Delay() can only be delayed in increments of 200ms.
    Last edited by Spectrus; 04-13-2013 at 07:28 AM.

  6. #16
    XenoBot Scripts Developer Syntax's Avatar
    Join Date
    Feb 2011
    Posts
    1,658
    Mentioned
    431 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Eion View Post
    Is it ok to use "wait(50)" inside modules or do you have to use "module: Delay(50)"? Also, If you call a function from outside the module is it ok to have "wait(50)" in that function or will those commands delay the execution of the main .LUA ?
    wait(50) will lock the thread (the entire script will stop 'thinking')
    moduleelay(50) will pause the module, it won't fire until the delay has passed (though there's no point in calling a delay under 200ms as Modules can't think faster than 200ms (TIMER_EVENT limitation)

    edit: fuuuuuuuuu Spectrus.

  7. #17
    Senior Member Eion's Avatar
    Join Date
    Dec 2011
    Posts
    558
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    @Spectrus @Syntax
    +1
    Thanks so much, this definitely explains a problem I was having. I think I am better off using separate .LUA scripts.

    What about if you call a function from outside a module within a running module will the wait interupt it?
    Last edited by Eion; 04-13-2013 at 04:12 PM.

  8. #18
    Administrator
    Join Date
    Dec 2010
    Posts
    2,565
    Mentioned
    228 Post(s)
    Tagged
    1 Thread(s)
    Yes.

  9. #19

    Join Date
    Oct 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have a probelm with modules :S!
    im using a script wich use a targetting script instead of using the classic targeting... im trying to use modules 'cause you can have all in one lua but... it suddenly stops the modules and i have to restart the script :S
    its anoying 'cause i want to afk bot :S

    anyone knows whats happening?

  10. #20
    Moderator Nakuu's Avatar
    Join Date
    Feb 2013
    Location
    EU
    Posts
    5,194
    Mentioned
    642 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by Danicast View Post
    i have a probelm with modules :S!
    im using a script wich use a targetting script instead of using the classic targeting... im trying to use modules 'cause you can have all in one lua but... it suddenly stops the modules and i have to restart the script :S
    its anoying 'cause i want to afk bot :S

    anyone knows whats happening?
    Show us the error message.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •