boardman94 Posted February 11, 2015 Share Posted February 11, 2015 How would I run a function every 1 second? I know about "DoPeriodicTask" and "DoTaskInTime" but how would i call them? It seems that they are methods of prefabs, but I don't have any prefabs in the modmain. Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/ Share on other sites More sharing options...
Blueberrys Posted February 11, 2015 Share Posted February 11, 2015 What are you trying to do with the task? There might be a more efficient way of doing it than constantly calling a function unless you are actually repeating something significant. As for how you can use it, why not attach it to the player's instance? That's an always available prefab, unless you completely change the game.AddSimPostInit(function(inst) -- inst:DoPeriodicTask()end) Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-611933 Share on other sites More sharing options...
chromiumboy Posted February 11, 2015 Share Posted February 11, 2015 Yup, either attach the task to the player or to the world (e.g. GetPlayer():DoPeriodicTask(etc), GetWorld():DoPeriodicTask(etc) ). These tasks must be attached to a prefab somehow. Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-611935 Share on other sites More sharing options...
boardman94 Posted February 11, 2015 Author Share Posted February 11, 2015 What are you trying to do with the task? I am adding a custom button to the HUD. I have to check every now and then if I should change the button. GetWorld():DoPeriodicTask(etc) ). Thanks, I'll try that. Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-611944 Share on other sites More sharing options...
Corrosive Posted February 11, 2015 Share Posted February 11, 2015 @boardman94, @chromiumboy, To be clear, DoPeriodicTask is a method of EntityScript, so it can be invoked on any entity, not just prefabs. Essentially anything that has been generated using CreateEntity() Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-611960 Share on other sites More sharing options...
chromiumboy Posted February 11, 2015 Share Posted February 11, 2015 @boardman94, @chromiumboy, To be clear, DoPeriodicTask is a method of EntityScript, so it can be invoked on any entity, not just prefabs. Essentially anything that has been generated using CreateEntity() True, thanks for clarifying that. Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-611964 Share on other sites More sharing options...
Blueberrys Posted February 11, 2015 Share Posted February 11, 2015 I have to check every now and then if I should change the button. What are the conditions for it to change? Sounds like something that can be done without a timer. Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-611968 Share on other sites More sharing options...
boardman94 Posted February 12, 2015 Author Share Posted February 12, 2015 @Blueberrys,The button spawns two prefabs (NPCs) Then waits for them both to be dead then starts a cool down of one day before it becomes active again.I also am not sure how to make a button visibly inactive (other than hiding it.) Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-612192 Share on other sites More sharing options...
Blueberrys Posted February 12, 2015 Share Posted February 12, 2015 @boardman94You can set the onkilledbyother function in the combat component for your prefabs, which will be called when they die.prefab_inst.components.combat.onkilledbyother = function(inst, attacker) -- ...endThe cooldown can be done with DoTaskInTime. This way the code will only run when you need it to. Hmm. How bout using a tint to make it appear deactivated, and remove the click function temporarily (or put a check for the cooldown in it). Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-612232 Share on other sites More sharing options...
boardman94 Posted February 12, 2015 Author Share Posted February 12, 2015 @Blueberrys,I am now having issues with setting up an event listener in modmain. I have rewritten the NPC so it does its own cool down then uses "PushEvent("mydeath")." but now I can't create an event listener in modmain. I have triedboth :GLOBAL.GetPlayer().entity:ListenForEvent("mydeath", buttonUpdate)--andGLOBAL.GetWorld().entity:ListenForEvent("mydeath", buttonUpdate)But neither one works, (the error states: "Attempt to call "ListenForEvent" a nil value") Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-612277 Share on other sites More sharing options...
Blueberrys Posted February 12, 2015 Share Posted February 12, 2015 @boardman94 Hmm. Try it without the "entity". I think the events are attached directly to the prefab instance.GLOBAL.GetPlayer():ListenForEvent("mydeath", buttonUpdate)Make sure you're listening for the event on the same prefab that it's being pushed to. If you push the event to the instance of the NPC, it won't be sent to the player or world instance. Link to comment https://forums.kleientertainment.com/forums/topic/50829-createing-a-repeating-task-in-modmainlua/#findComment-612297 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now