Controlling when gameframe initiates
Moderator: Moderators
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Controlling when gameframe initiates
So I'm trying to make the gameframe function in this code run when the command is called, or when commandfallback is initiated, but the last time I tried to do this I had issues. Could I get some help with how I should make gameframe start when I give the command?
Code is here:
http://pastebin.com/7nfk7iu9
commandfallback is at line 66
Gameframe is at line 113
Thanks in advance.
Code is here:
http://pastebin.com/7nfk7iu9
commandfallback is at line 66
Gameframe is at line 113
Thanks in advance.
Re: Controlling when gameframe initiates
Have Gameframe run all the time.
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Re: Controlling when gameframe initiates
Well then how do I do checks during the command instead of all the time? I don't want it to constantly check when the command isn't on.
Do you suggest I use something else?
Do you suggest I use something else?
Re: Controlling when gameframe initiates
if statements?Automatics wrote:Well then how do I do checks during the command instead of all the time? I don't want it to constantly check when the command isn't on.
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Re: Controlling when gameframe initiates
Well others had told me to change my code when I had used if statements. I'd have to just put the gameframe function inside of commandfallback and that would be a conditional in itself because the conditional should make gameframe start when commandfallback does, but I had been told, on the moddev channel, to pull gameframe of commandfallback last time I had done this.
If having gameframe inside of commandfallback is fine, then I can just base it off of the conditional which checks which the id of the command.
Would that be fine?
If having gameframe inside of commandfallback is fine, then I can just base it off of the conditional which checks which the id of the command.
Would that be fine?
Re: Controlling when gameframe initiates
I meant to say that you could have your gameframe code run every frame (or 10th or whatever) and just check to see if the command has been called but not resolved... so your command (or fallback) would set a variable to true and then the gameframe would check if it was true and execute your "checks" code and set it to false... or just do nothing if it was already false.
Probably a more elegant way to do it but I never finished my CS degree.
Probably a more elegant way to do it but I never finished my CS degree.
- KingRaptor
- Zero-K Developer
- Posts: 838
- Joined: 14 Mar 2007, 03:44
Re: Controlling when gameframe initiates
Perhaps first you should explain what you're trying to do (as in what the purpose of the gadget is).
Eyeballing the code and the OP, it seems you want to do a distance check for being close enough to the mineral deposit at regular intervals once the "locate" command has been given. If so, the code as pasted should work fine (if no locators are active, the activeLocator table is empty, so the pairs iterator does nothing.
The only thing missing is that the locator's activeLocator entry isn't cleared once it finishes working, so it's always "on" after the first time.
Eyeballing the code and the OP, it seems you want to do a distance check for being close enough to the mineral deposit at regular intervals once the "locate" command has been given. If so, the code as pasted should work fine (if no locators are active, the activeLocator table is empty, so the pairs iterator does nothing.
The only thing missing is that the locator's activeLocator entry isn't cleared once it finishes working, so it's always "on" after the first time.
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Re: Controlling when gameframe initiates
Right, my apologies for not explaining what I'm trying to do.
I'm trying to do exactly that, but also make the locator unit move to the deposit if it locates one. When it goes inside of my set range for distance checking, it constantly reports with the echos that I set, but I just need to get it to move there after it locates it.
And about ending the constant checking, should I just add a conditional inside of gameframe after "if frame%10 == 0 then" to make it start and stop when the unit starts the command and when I choose to end it, or would that give an issue of constantly checking as well? Or should I just set activeLocators to nil if the unit stops, command is overridden, unit is destroyed?
I'm trying to do exactly that, but also make the locator unit move to the deposit if it locates one. When it goes inside of my set range for distance checking, it constantly reports with the echos that I set, but I just need to get it to move there after it locates it.
And about ending the constant checking, should I just add a conditional inside of gameframe after "if frame%10 == 0 then" to make it start and stop when the unit starts the command and when I choose to end it, or would that give an issue of constantly checking as well? Or should I just set activeLocators to nil if the unit stops, command is overridden, unit is destroyed?
Last edited by Automatics on 24 Jul 2013, 16:08, edited 1 time in total.
Re: Controlling when gameframe initiates
Use Spring.SetUnitMoveGoal
Re: Controlling when gameframe initiates
Why not a simple move command?FLOZi wrote:Use Spring.SetUnitMoveGoal
Re: Controlling when gameframe initiates
zwzsg wrote:Why not a simple move command?FLOZi wrote:Use Spring.SetUnitMoveGoal
it seems you want to do a distance check for being close enough to the mineral deposit
Re: Controlling when gameframe initiates
I think so too. Actually there is a --activeLocators[unitID] = false but commented out..KingRaptor wrote: The only thing missing is that the locator's activeLocator entry isn't cleared once it finishes working, so it's always "on" after the first time.
Maybe like this:
Code: Select all
function gadget:CommandFallback(unitID, unitDefID, unitTeam, cmdID, cmdParams, cmdOptions, cmdTag)
if cmdID == CMD_STRT_LOC then --is beloved our custom command?
if LOCATORS[unitDefID] then --is it a unit that should care about our custom command?
activeLocators[unitID] = true
--locator activity stuff here--
...
..
end
else -- it is not our beloved custom command anymore!
activeLocators[unitID] = nil
end
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Re: Controlling when gameframe initiates
But wouldn't setting activeLocators to nil mess it up for other locators?
Also, I'm guessing I should set a variable to true if the deposit is in range like so:
If I put it in gameframe, it will constantly tell it to move, will it not? But putting it into commandfallback disables the command due to the following error: [f=0000000] Error: Failed to load: locatecmd.lua ([string "LuaRules/Gadgets/locatecmd.lua"]:109: 'then' expected near '=')
Also, I'm guessing I should set a variable to true if the deposit is in range like so:
But if I do that, where should I put this:if math.abs(metalx - locatorPosX)<=200 and math.abs(metalz - locatorPosZ)<=200 then
Spring.Echo("The Locator is within range of a mineral deposit!")
depositLocated = true
end
Code: Select all
if depositLocated = true then
Spring.SetUnitMoveGoal(activeLocators[unitID], metalx, metaly, metalz)
else
end
Re: Controlling when gameframe initiates
I think you mean:if depositLocated = true then
if depositLocated == true then
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Re: Controlling when gameframe initiates
Oh, right. I missed that. :p
But now it just gives this:
[f=0000000] Error: Failed to load: locatecmd.lua ([string "LuaRules/Gadgets/locatecmd.lua"]:125: '=' expected near '==')
http://pastebin.com/2CeAgybX
What did I do this time?
But now it just gives this:
[f=0000000] Error: Failed to load: locatecmd.lua ([string "LuaRules/Gadgets/locatecmd.lua"]:125: '=' expected near '==')
http://pastebin.com/2CeAgybX
What did I do this time?

Re: Controlling when gameframe initiates
depositLocated == true
depositLocated = true
if you set variable to some value, then use =
to compare (like in if's) use ==

if you set variable to some value, then use =
to compare (like in if's) use ==
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Re: Controlling when gameframe initiates
Oh, okay. Well, now everything works except for SetUnitMoveGoal. Any help?
Re: Controlling when gameframe initiates
You do:
but activeLocators[unitID] will just be "true" or whatever, instead it needs to be unitID:
Code: Select all
Spring.SetUnitMoveGoal(activeLocators[unitID], metalx, metaly, metalz)
Code: Select all
Spring.SetUnitMoveGoal(unitID, metalx, metaly, metalz)
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Re: Controlling when gameframe initiates
Hmm, still doesn't get it to interrupt the command and then move.
- Automatics
- Posts: 15
- Joined: 04 Aug 2011, 08:37
Re: Controlling when gameframe initiates
Sorry for double posting, but I believe I changed everything you guys suggested, but it still doesn't seem to be moving to the metal spot after it finds one, but it does give off echos.
Changed code is here: http://pastebin.com/6RdSCzwF
Help is appreciated.
Changed code is here: http://pastebin.com/6RdSCzwF
Help is appreciated.