Jump to content

lua scripts ran differently when from C++ program?


Rick
 Share

Recommended Posts

I'm having an issue where my lua script for my camera works fine from the editor, but when I load the map that has this script in it from my own C++ program it acts differently. I will now describe acting differently.

 

The script is my ThirdpersonCamera script. Basically wherever you place the editor model is where the fw.camera will be placed. Then you assign it a target and it'll point at that target. This all works just fine and great when run from the editor.

 

function object:Update()
	local t = object.model:GetTarget(0)

	if GetGlobalString("mode") == "GAME_MODE" then
		if object.oneTime == false then
			object.cameraPivot:SetPosition(EntityPosition(object.model, 1), 1)
			object.playerCube:Hide()
			local p = t:GetPosition(1)
			p.x = p.x + object.targetOffset.x
			p.y = p.y + object.targetOffset.y
			p.z = p.z + object.targetOffset.z

			object.playerPivot:SetPosition(Vec3(p.x, p.y, p.z))
			object.cameraPivot:Point(object.playerPivot, 3, 1, 0)
			fw.main.camera:SetMatrix(object.cameraPivot.mat)
			object.oneTime = true
		end


		if t == nil then
		else
			-- the pivot to follow the player around


			-- debug code
			--object.cameraCube:SetPosition(object.cameraPivot:GetPosition())
			--object.playerCube:SetPosition(object.playerPivot:GetPosition(1))
		end
	else
		if t ~= nil then
			local p = t:GetPosition(1)
			p.x = p.x + object.targetOffset.x
			p.y = p.y + object.targetOffset.y
			p.z = p.z + object.targetOffset.z

			object.playerPivot:SetPosition(Vec3(p.x, p.y, p.z))
			object.cameraPivot:SetPosition(EntityPosition(object.model))
		end

		-- debug code
		--object.cameraCube:SetPosition(object.cameraPivot:GetPosition())
		object.playerCube:SetPosition(object.playerPivot:GetPosition())
	end
end

 

I then created my own C++ program that has all the lua stuff loaded and working. I know this because I load the scene and I can see from the engine.log that it's loading the lua files and I see the lua stuff working. The problem is that the camera when ran from my program isn't in the same place. It looks to be at the target location instead of the editor model location. I did set the global string "mode" to "GAME_MODE".

 

So in the script object.cameraPivot:SetPosition(EntityPosition(object.model, 1), 1) should set the camera pivot to the location of the object model. Then fw.main.camera:SetMatrix(object.cameraPivot.mat) should set the fw.camera (which I did create in my C++ program and expose to lua) to the object.cameraPivot. So what am I missing?

Link to comment
Share on other sites

So after the camera gets set I added this code:

 

pos = fw.main.camera:GetPosition(1)

Notify("fw.main.camera pos = "..pos.x..","..pos.y..","..pos.z)

 

I do get different values when ran from my C++ program vs running from the editor. They are running the same script and loading the same scene so why would I get different values?

 

C++ program gives (in x,y,z)

-9.96,22.05,-18.01

 

Editor gives

-13.9,26.4,-11.3

Link to comment
Share on other sites

Of course you get different camera positions, since in C++ the camera starts from 0,0,0 when you create it, and in Editor it starts from where you left it in Editor.

Ryzen 9 RX 6800M ■ 16GB XF8 Windows 11 ■
Ultra ■ LE 2.53DWS 5.6  Reaper ■ C/C++ C# ■ Fortran 2008 ■ Story ■
■ Homepage: https://canardia.com ■

Link to comment
Share on other sites

What I don't get is if I do

 

object.cameraPivot:SetPosition(object.model:GetPosition(1), 1)
pos = object.cameraPivot:GetPosition(1)
Notify("object.cameraPivot position = "..pos.x..","..pos.y..","..pos.z)

 

I get (-13, 26, -11)

 

Then a couple lines down if I do

 

fw.main.camera:SetPosition(object.cameraPivot:GetPosition(1), 1)
pos = fw.main.camera:GetPosition(1)
Notify("camera = "..pos.x..","..pos.y..","..pos.z)

 

I get (-23,48,-29)

 

 

What am I missing? If the position of object.cameraPivot is A and I set fw.main.camera to object.cameraPivot position, how can fw.main.camera position be B?

Link to comment
Share on other sites

Would that matter between running from the editor vs running from my own C++ program? I mean both are running this lua script and in there I just do:

 

object.cameraPivot = CreatePivot()

 

to create the pivot. This all works perfect in the editor.

 

There just must be something I'm missing between running from the editor to running from C++.

Link to comment
Share on other sites

Here is the C++ code to expose the fw

 

BP L = GetLuaState();
lua_pushobject(L, framework);
lua_setglobal(L, "fw");
lua_pop(L, 1);

 

Do I need to do something else with this? I wouldn't think so because I can set the fw.main.camera to the position I know it should be and it works, but for some reason when I try to set the position based on the pivots position it doesn't work.

 

I get the offset thing, but I can't understand how the lua scripts would create the pivot differently based on when ran from my C++ code vs the editor. It's all being done in the script.

Link to comment
Share on other sites

Could it be something around:

 

object.cameraPivot = CreatePivot()

object.playerPivot = CreatePivot()

object.cameraPivot:SetParent(object.playerPivot, 1)

 

I do set the parent with the cameraPivot. But again, same script being ran so I wouldn't think so.

Link to comment
Share on other sites

The The fact that I do the following

object.cameraPivot:SetParent(object.playerPivot, 1)

 

is causing the issue. If I comment that out, then the camera is placed in the correct location. However, I want that behaviour. I want the cameraPivot to move when the playerPivot moves.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...