Posted: 29th Jul 2021 7:17
Hi everybody.

Im a beginner with only a few days of coding and I need help with "spawning" a box.
Its just the start of a long line of questions, but how do I do that?
My code is as follows:
//------------------------------------------------------------------------------------------------------
function EditLevel() //Adds object to the left of characters position
texture = LoadImage("skull-white.jpg") // Load image from the media folder called skull-white.jpg and assigned its ID to an integer variable called "texture"

MyBox = CreateObjectBox(1,1,1) //1 equals size in X,Y,Z
MoveObjectLocalX(MyBox, PlayerOne.ID -1)
MoveObjectLocalY(MyBox, PlayerOne.ID)
MoveObjectLocalZ(MyBox, PlayerOne.ID)
RotateObjectGlobalX(MyBox,0) // Rotate the box if you need to
RotateObjectGlobalY(MyBox,0) // Rotate the box if you need to
RotateObjectGlobalZ(MyBox,0) // Rotate the box if you need to
SetObjectImage(MyBox, texture, 0) // Assigns the texture image to MyBox in texture layer 0
sync()

endfunction
//------------------------------------------------------------------------------------------------------
I am calling the funtion in my player movements with this simple line:
if(GetRawKeyPressed(KEY_E))
EditLevel()
endif
//------------------------------------------------------------------------------------------------------
What am I missing here?
Any help appreciated!
-Jonas
Posted: 29th Jul 2021 12:45
You are close! You need to first place the box at the location of the player, and THEN move it "locally" to the "left" which is -X.

First you need to use SetObjectPosition, so something like:

SetObjectPosition(MyBox, GetObjectX(PlayerOne.ID), GetObjectY(PlayerOne.ID), GetObjectZ(PlayerOne.ID))

Then you move it with MoveObjectLocalX, and you actually don't need the next two lines (MoveObjectLocalY and MoveObjectLocalZ).

A couple more things as well, I would recommend that you load the texture somewhere else in the beginning of your code rather than in the function. The image only needs to get loaded one time, but this function will load it every time it's called. You don't need to call Sync() in the function, you typically only want to call Sync once per game loop, typically at the very end of your logic code.
Posted: 29th Jul 2021 15:14
Thanks Jon- got it working now.