Spring.CreateUnit()

Spring.CreateUnit()

Discuss Lua based Spring scripts (LuaUI widgets, mission scripts, gaia scripts, mod-rules scripts, scripted keybindings, etc...)

Moderator: Moderators

Post Reply
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Spring.CreateUnit()

Post by Voidranaut »

I have been playing with the Spring.CreateUnit() command in the following script

Code: Select all

function gadget:GetInfo()
   return {
      name = "CUTests",
      desc = "creates units",
      author = "zwzsg (and then horribly ruined by Voidranaut)",
      date = "not sure",
      version = "1.0",
      license = "Public Domain",
      layer = 0,
      enabled = true
   }
end

local message_identifier = gadget:GetInfo().name..": "
local once = 1

if (gadgetHandler:IsSyncedCode()) then--SYNCED

   function gadget:RecvLuaMsg(msg,player)
 
      local MsgForMe=string.match(msg,message_identifier.."(.*)") 

      if MsgForMe then 

         Spring.Echo("Synced recieved a message from unsynced:")
         Spring.Echo(MsgForMe)

		if( once == 1) then
	 	 Spring.CreateUnit( 367, 7110, 2700, 38,(0), 1, 0 )
	 	 once = 0
		end
      end

   end


	
else--UNSYNCED

   function gadget:KeyPress(key) --key scope exists within the function
	if( key == 121 ) then
		for _,u in ipairs(Spring.GetAllUnits()) do
			
			Spring.SendLuaRulesMsg(message_identifier..u.." was pressed")
			Spring.Echo( u )
		end
	end


  	Spring.Echo("Key "..key.." pressed in unsynced") 
	
      	Spring.SendLuaRulesMsg(message_identifier..key.." was pressed")
   end

end
the locations dictated by the x,y,z position in the Spring.CreateUnit() is just an area on the map that my first unit spawns near (I got the cowardinates from just holding my cursor over a blank peice of land)

when I hit the key to trigger the event I get this error

Code: Select all

Key 121 pressed in unsynced
[    833] Synced recieved a message from unsynced:
[    833] 6344 was pressed
[    833] LuaRules::RunCallIn: error = 2, RecvLuaMsg, [string "LuaRules/Gadgets/cutests.lua"]:29: Unsafe attempt to change game state
[    833] Synced recieved a message from unsynced:
[    833] 2738 was pressed
[    833] LuaRules::RunCallIn: error = 2, RecvLuaMsg, [string "LuaRules/Gadgets/cutests.lua"]:29: Unsafe attempt to change game state
[    833] Synced recieved a message from unsynced:
[    833] 1096 was pressed
[    833] LuaRules::RunCallIn: error = 2, RecvLuaMsg, [string "LuaRules/Gadgets/cutests.lua"]:29: Unsafe attempt to change game state
[    833] Synced recieved a message from unsynced:
[    833] 121 was pressed
[    833] LuaRules::RunCallIn: error = 2, RecvLuaMsg, [string "LuaRules/Gadgets/cutests.lua"]:29: Unsafe attempt to change game state


now there is alot of this error that is undoubtibly because I badly wrote the scirpt BUT my BIGGEST question does come from this part here

Code: Select all

Unsafe attempt to change game state
question: what exactly are the needed requirements for a unit to be "safely" spawned?

(loard know I did 100 things wrong on this scirpt but that is my main quesiton)
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Spring.CreateUnit()

Post by FLOZi »

You can use

Code: Select all

AllowUnsafeChanges("USE AT YOUR OWN PERIL")
-- Allows the use of the following call-outs when
-- not in the GameFrame() or GotChatMsg() call-ins:
--
-- CreateUnit()
-- DestroyUnit()
-- TransferUnit()
-- CreateFeature()
-- DestroyFeature()
-- TransferFeature()
-- GiveOrderToUnit()
-- GiveOrderToUnitMap()
-- GiveOrderToUnitArray()
-- GiveOrderArrayToUnitMap()
-- GiveOrderArrayToUnitArray()
--
-- *** The string argument must be an exact match ***

S44, CA and others use it without any negative impact afaik.
User avatar
zwzsg
Kernel Panic Co-Developer
Posts: 7052
Joined: 16 Nov 2004, 13:08

Re: Spring.CreateUnit()

Post by zwzsg »

Though, it would be more safe to create units in GameFrame!

Or are you saying the safety is useless?
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: Spring.CreateUnit()

Post by Voidranaut »

zwzsg wrote:Though, it would be more safe to create units in GameFrame!

Or are you saying the safety is useless?
I see I did not realize that the game frame was the missing componet

I will look into that now

thanks for pointing me in the right direction :)

and thanks for letting me know about the possiblity of Allowing unsafe changes :)
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Spring.CreateUnit()

Post by FLOZi »

zwzsg wrote:Though, it would be more safe to create units in GameFrame!

Or are you saying the safety is useless?
Quantum assured me that trepan explained to him excellent reasons why such things are unsafe. Empirically, however, I've not seen any problems caused by it in S44. And afaik CA still uses it, as do all of Argh's projects that I know of.

Also, seen as lua unit scripts (LUS) are in gadgetland, I assume that without it you couldn't use CreateUnit in LUS, which can be a useful thing to be able to do.
Tobi
Spring Developer
Posts: 4598
Joined: 01 Jun 2005, 11:36

Re: Spring.CreateUnit()

Post by Tobi »

FLOZi wrote:Also, seen as lua unit scripts (LUS) are in gadgetland, I assume that without it you couldn't use CreateUnit in LUS, which can be a useful thing to be able to do.
You could still put them in a list in GG and create them in GameFrame in a special gadget for this...

And yes, AllowUnsafeChanges allows you to crash Spring in quite nasty ways, but also yes, accidentally doing that doesn't happen too often.
User avatar
FLOZi
MC: Legacy & Spring 1944 Developer
Posts: 6241
Joined: 29 Apr 2005, 01:14

Re: Spring.CreateUnit()

Post by FLOZi »

Tobi wrote:
FLOZi wrote:Also, seen as lua unit scripts (LUS) are in gadgetland, I assume that without it you couldn't use CreateUnit in LUS, which can be a useful thing to be able to do.
You could still put them in a list in GG and create them in GameFrame in a special gadget for this...
Yeah but, considerably more hassle and harder to read than just having CreateUnit() right there in your code.
And yes, AllowUnsafeChanges allows you to crash Spring in quite nasty ways, but also yes, accidentally doing that doesn't happen too often.
Now I'm intrigued. :wink:
Voidranaut
Posts: 47
Joined: 20 Dec 2009, 21:16

Re: Spring.CreateUnit()

Post by Voidranaut »

with this spring creat unit stuff I was hopeing to make a gadget that would make a specific unit on a specific part of the map when I hit a key on the key board

so far my code looks like this

Code: Select all

function gadget:GetInfo()
   return {
      name = "CUTests",
      desc = "creates units",
      author = "zwzsg (and then horribly ruined by Voidranaut)",
      date = "not sure",
      version = "1.0",
      license = "Public Domain",
      layer = 0,
      enabled = true
   }
end

local message_identifier = gadget:GetInfo().name..": "
local once = 1

if (gadgetHandler:IsSyncedCode()) then--SYNCED

   function gadget:RecvLuaMsg(msg,player)
 
      local MsgForMe=string.match(msg,message_identifier.."(.*)") 

      if MsgForMe then 

         Spring.Echo("Synced recieved a message from unsynced:")
         Spring.Echo(MsgForMe)

		
      end

   end


function gadget:GameFrame(n)


	function gadget:RecvLuaMsg(msg,player)
 
      		local MsgForMe=string.match(msg,message_identifier.."(.*)") 

      		if MsgForMe then 
			
			if( once == 1) then
				Spring.CreateUnit( 367, 7110, 2700, 38,(0), 1, 0 )
	 			once = 0
			end
        		 Spring.Echo("Synced recieved a message from unsynced:")
         		Spring.Echo(MsgForMe)

		
      		end

   	end



	


end

	
else--UNSYNCED

   function gadget:KeyPress(key) --key scope exists within the function
	if( key == 121 ) then
		for _,u in ipairs(Spring.GetAllUnits()) do
			
			Spring.SendLuaRulesMsg(message_identifier..u.." was pressed")
			Spring.Echo( u )
		end
	end


  	Spring.Echo("Key "..key.." pressed in unsynced") 
	
      	Spring.SendLuaRulesMsg(message_identifier..key.." was pressed")
   end

end
now as you can see I dont know at all how to work with fuction gadget:GameFrame()... I LITERALY just stuck my command in it and hoped it would not crash... the nice thing is it did not :) YAYA small victory!!.. but of course it did nto work and gave me the following error

Code: Select all

LuaRules::RunCallIn: error = 2, RecvLuaMsg, [string "LuaRules/Gadgets/cutests.lua"]:44: Unsafe attempt to change game state
Probleam: now I know I dont know anything about game frame but I am also haveing ALOT of trouble finding more info on it (all I know is it is called every game frame in synced code)

Question: is there any good sources of info on GameFrame() that anyone can point me to? and esspecially how exactly it is envolved in the act of Spring.CreateUnit()

EDIT!: sorry one last question
question: is gameframe needed to change resources? or command buttons to unit menues?
Post Reply

Return to “Lua Scripts”