Page 1 of 2
Controlling when gameframe initiates
Posted: 23 Jul 2013, 23:04
by Automatics
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.
Re: Controlling when gameframe initiates
Posted: 23 Jul 2013, 23:38
by zwzsg
Have Gameframe run all the time.
Re: Controlling when gameframe initiates
Posted: 23 Jul 2013, 23:43
by Automatics
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?
Re: Controlling when gameframe initiates
Posted: 24 Jul 2013, 03:08
by SinbadEV
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.
if statements?
Re: Controlling when gameframe initiates
Posted: 24 Jul 2013, 03:57
by Automatics
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?
Re: Controlling when gameframe initiates
Posted: 24 Jul 2013, 04:04
by SinbadEV
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.
Re: Controlling when gameframe initiates
Posted: 24 Jul 2013, 06:52
by KingRaptor
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.
Re: Controlling when gameframe initiates
Posted: 24 Jul 2013, 16:04
by Automatics
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?
Re: Controlling when gameframe initiates
Posted: 24 Jul 2013, 16:08
by FLOZi
Use Spring.SetUnitMoveGoal
Re: Controlling when gameframe initiates
Posted: 24 Jul 2013, 19:55
by zwzsg
FLOZi wrote:Use Spring.SetUnitMoveGoal
Why not a simple move command?
Re: Controlling when gameframe initiates
Posted: 24 Jul 2013, 20:20
by FLOZi
zwzsg wrote:FLOZi wrote:Use Spring.SetUnitMoveGoal
Why not a simple move command?
it seems you want to do a distance check for being close enough to the mineral deposit
Re: Controlling when gameframe initiates
Posted: 24 Jul 2013, 20:44
by knorke
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.
I think so too. Actually there is a --activeLocators[unitID] = false but commented out..
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
Re: Controlling when gameframe initiates
Posted: 25 Jul 2013, 07:52
by Automatics
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 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
But if I do that, where should I put this:
Code: Select all
if depositLocated = true then
Spring.SetUnitMoveGoal(activeLocators[unitID], metalx, metaly, metalz)
else
end
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 '=')
Re: Controlling when gameframe initiates
Posted: 25 Jul 2013, 15:21
by zwzsg
if depositLocated = true then
I think you mean:
if depositLocated == true then
Re: Controlling when gameframe initiates
Posted: 25 Jul 2013, 16:14
by Automatics
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?

Re: Controlling when gameframe initiates
Posted: 25 Jul 2013, 16:28
by knorke
depositLocated == true

depositLocated = true
if you set variable to some value, then use =
to compare (like in if's) use ==
Re: Controlling when gameframe initiates
Posted: 25 Jul 2013, 17:01
by Automatics
Oh, okay. Well, now everything works except for SetUnitMoveGoal. Any help?
Re: Controlling when gameframe initiates
Posted: 25 Jul 2013, 17:21
by knorke
You do:
Code: Select all
Spring.SetUnitMoveGoal(activeLocators[unitID], metalx, metaly, metalz)
but activeLocators[unitID] will just be "true" or whatever, instead it needs to be unitID:
Code: Select all
Spring.SetUnitMoveGoal(unitID, metalx, metaly, metalz)
Re: Controlling when gameframe initiates
Posted: 25 Jul 2013, 17:41
by Automatics
Hmm, still doesn't get it to interrupt the command and then move.
Re: Controlling when gameframe initiates
Posted: 30 Jul 2013, 10:05
by Automatics
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.