problem with function moving an entity?

Blitz3D Forums/Blitz3D Beginners Area/problem with function moving an entity?

DQ(Posted 2005) [#1]
hey. basically, i'm adding a function that will make it easy for me to place props in my level.


Function moveProp()
	RenderWorld
	Text 100,100,"move mode enabled"
	Flip
	Repeat
		If MouseHit(1) Then picked=CameraPick(mainCam,MouseX(),MouseY())
		If KeyHit(keyM) Then Return
		updatePlayer()
		UpdateWorld
		RenderWorld
		Text 100,100,"move mode enabled"
		Flip
	Until picked
	entitypicked=PickedEntity()
	movePropDone=0
	For newProp.prop=Each prop
		If newprop\mesh=entitypicked
			Repeat
				If KeyDown(key1) Then TranslateEntity newprop\mesh,0,1,0
				If KeyDown(key2) Then TranslateEntity newprop\mesh,0,-1,0
				If KeyDown(key3) Then TranslateEntity newprop\mesh,1,0,0
				If KeyDown(key4) Then TranslateEntity newprop\mesh,-1,0,0
				If KeyDown(key5) Then TranslateEntity newprop\mesh,0,0,1
				If KeyDown(key6) Then TranslateEntity newprop\mesh,0,0,-1
				If KeyHit(keyM) Then movePropDone=1
				updatePlayer()
				UpdateWorld
				RenderWorld
				Text 100,100,"move mode enabled"
				Flip
			Until movePropDone
		EndIf
	Next
	Flip
	FlushKeys()
End Function



i know its messy and some stuff doesn't make sense yet..
basically it works like this: i press 'M' and it enters "move mode". then i click the entity i want selected and then move it with the number keys. then press 'M' again to exit move mode. the problem comes in when i exit move mode. when i do that, the entity in question makes a jump to some random other place between where it was originally and where it is afterwards, but then goes back to the correct position. (its only for a split second). i thought it was a problem with my buffer swapping, but now i don't think so. could it be because i'm using captureworld and renderworld tween in my main code for frame limiting?


octothorpe(Posted 2005) [#2]
Could it be that you're Flipping without Rendering at the very end of your function?


DQ(Posted 2005) [#3]
hmmm.. possible. i'll try fixing that when i get home. sometimes the "jump" between positions is pretty big, sometimes small.


octothorpe(Posted 2005) [#4]
Because of how double-buffering works, if you Flip without Rendering, you'll be displaying your second last render.


octothorpe(Posted 2005) [#5]
Keeping your code readable (to yourself) is paramount.

The less you put in blocks, the easier your code will be to read. Consider using temporary variables when your loop's purpose is to find something. Unless you're assigning to temporary variables in tight loops, the cost in speed is negligible.

	; find the prop picked
	entitypicked=PickedEntity()
	proppicked.prop = null
	For newProp.prop=Each prop
		If newprop\mesh=entitypicked
			proppicked = newprop
		EndIf
	Next

	; move the prop
	if proppicked <> null then
		movePropDone=0
		Repeat
			If KeyDown(key1) Then TranslateEntity proppicked\mesh,0,1,0
			If KeyDown(key2) Then TranslateEntity proppicked\mesh,0,-1,0
			If KeyDown(key3) Then TranslateEntity proppicked\mesh,1,0,0
			If KeyDown(key4) Then TranslateEntity proppicked\mesh,-1,0,0
			If KeyDown(key5) Then TranslateEntity proppicked\mesh,0,0,1
			If KeyDown(key6) Then TranslateEntity proppicked\mesh,0,0,-1
			If KeyHit(keyM) Then movePropDone=1
			updatePlayer()
			UpdateWorld
			RenderWorld
			Text 100,100,"move mode enabled"
			Flip
		Until movePropDone
	endif



octothorpe(Posted 2005) [#6]
This might be a little advanced for you, but I can't resist suggesting it. Here's a sneaky way to get at the object which you want associated with an entity without looping over all your objects: store the object's handle in the entity's name. It's probably also a good idea to store the type of the object you want associated in the name too.

function create_prop.prop()
	;;;
	nameentity(this_prop\mesh, "PROP"+handle(this_prop))
	;;;
	return this_prop
end function

;;;
entity_picked=PickedEntity()
prop_picked.prop = null
if left$(entityname(entity_picked), 4) = "PROP" then
	prop_handle = mid$(entityname(entity_picked), 4+1)   ; implicit int()
	prop_picked = object.prop(prop_handle)
endif



DQ(Posted 2005) [#7]
i had thought of that! unfortunately, i wrote this around 2am last night. i knew i could get the handle of it, but wasn't really sure how to go about using it. thanks..

found the prob by the way. since i was using captureworld in the main loop, it threw things off. since i move objects and renderworld WITHOUT redoing captureworld, when it was called again, it made the sudden jerky movement....so.. its fixed, but i'm gonna take both suggestions and rewrite this to be easier to use.