Controlling when gameframe initiates

Controlling when gameframe initiates

Discuss game development here, from a distinct game project to an accessible third-party mutator, down to the interaction and design of individual units if you like.

Moderator: Moderators

User avatar
Automatics
Posts: 15
Joined: 04 Aug 2011, 08:37

Controlling when gameframe initiates

Post 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.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Controlling when gameframe initiates

Post by zwzsg »

Have Gameframe run all the time.
User avatar
Automatics
Posts: 15
Joined: 04 Aug 2011, 08:37

Re: Controlling when gameframe initiates

Post 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?
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: Controlling when gameframe initiates

Post 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?
User avatar
Automatics
Posts: 15
Joined: 04 Aug 2011, 08:37

Re: Controlling when gameframe initiates

Post 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?
User avatar
SinbadEV
Posts: 6475
Joined: 02 May 2005, 03:56

Re: Controlling when gameframe initiates

Post 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.
User avatar
KingRaptor
Zero-K Developer
Posts: 838
Joined: 14 Mar 2007, 03:44

Re: Controlling when gameframe initiates

Post 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.
User avatar
Automatics
Posts: 15
Joined: 04 Aug 2011, 08:37

Re: Controlling when gameframe initiates

Post 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?
Last edited by Automatics on 24 Jul 2013, 16:08, edited 1 time in total.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Controlling when gameframe initiates

Post by FLOZi »

Use Spring.SetUnitMoveGoal
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Controlling when gameframe initiates

Post by zwzsg »

FLOZi wrote:Use Spring.SetUnitMoveGoal
Why not a simple move command?
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Controlling when gameframe initiates

Post 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
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Controlling when gameframe initiates

Post 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
User avatar
Automatics
Posts: 15
Joined: 04 Aug 2011, 08:37

Re: Controlling when gameframe initiates

Post 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 '=')
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Controlling when gameframe initiates

Post by zwzsg »

if depositLocated = true then
I think you mean:
if depositLocated == true then
User avatar
Automatics
Posts: 15
Joined: 04 Aug 2011, 08:37

Re: Controlling when gameframe initiates

Post 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? :?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Controlling when gameframe initiates

Post by knorke »

depositLocated == true :arrow: depositLocated = true

if you set variable to some value, then use =
to compare (like in if's) use ==
User avatar
Automatics
Posts: 15
Joined: 04 Aug 2011, 08:37

Re: Controlling when gameframe initiates

Post by Automatics »

Oh, okay. Well, now everything works except for SetUnitMoveGoal. Any help?
User avatar
knorke
Posts: 7971
Joined: 22 Feb 2006, 01:02

Re: Controlling when gameframe initiates

Post 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)
User avatar
Automatics
Posts: 15
Joined: 04 Aug 2011, 08:37

Re: Controlling when gameframe initiates

Post by Automatics »

Hmm, still doesn't get it to interrupt the command and then move.
User avatar
Automatics
Posts: 15
Joined: 04 Aug 2011, 08:37

Re: Controlling when gameframe initiates

Post 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.
Post Reply

Return to “Game Development”