Small selecting problem with level editor

Blitz3D Forums/Blitz3D Beginners Area/Small selecting problem with level editor

Guy Fawkes(Posted 2013) [#1]
Hi! I was working on my level editor code and was wondering 2 simple things. 1) How can I hold Control & select as well as deselect 1 or more objects while holding control?

2) Why isn't my entity turning to half its' transparency when it is selected?

Here's the code I wrote so far:

Graphics3D(800, 600, 32, 2)

Type ent
	Field id
	Field x#
	Field y#
	Field z#
	Field pitch#
	Field yaw#
	Field roll#
End Type

Global LevelEd.ent = New ent

Global TYPE_OBJECT = 1
Global TYPE_TERRAIN = 2

Collisions TYPE_OBJECT, TYPE_OBJECT, 2, 3
Collisions TYPE_OBJECT, TYPE_TERRAIN, 2, 3
Collisions TYPE_TERRAIN, TYPE_OBJECT, 2, 3

Global cam = CreateCamera()
PositionEntity cam, 0, 1, 0
CameraRange cam, .1, 50000
EntityType cam, TYPE_OBJECT
EntityPickMode(cam, 2)

Global move_speed#

AmbientLight 255, 255, 255

Global sky=CreateCube()
EntityColor sky, 102, 102, 255
ScaleEntity sky, 10000, 10000, 10000
FlipMesh sky

Global land = CreatePlane()
EntityColor land, 0, 64, 0

Global pick, picked

Global oldid

For x = 1 To Rnd(10, 100)
	LevelEd\id = Create_Object()
	PositionEntity LevelEd\id, Rnd(-25, 75), 1, Rnd(-50, 100)
	EntityColor LevelEd\id, Rnd(0, 255), Rnd(0, 255), Rnd(0, 255)
	EntityAlpha LevelEd\id, Rnd(0.75, 1.0)
Next

While Not KeyHit(1)
			Set_Speeds#()
			Move(cam)
			picked = Pick_Object(cam)
			UpdateWorld()
			RenderWorld()
			Text GraphicsWidth()/2, GraphicsHeight()/2, "picked: "+picked, 1, 1
			Text GraphicsWidth()/2, GraphicsHeight()/2+20, "oldid: "+oldid, 1, 1
		Flip
	Wend
End
	
Function Set_Speeds#()
	If Not (KeyDown(42) Or KeyDown(54))
		move_speed# = .1
	Else If (KeyDown(42) Or KeyDown(54))
		move_speed# = .3
	EndIf
End Function

Function Move(ent)

	MoveEntity(ent, (KeyDown(32)-KeyDown(30) Or KeyDown(205)-KeyDown(203))*move_speed#, 0, (KeyDown(17)-KeyDown(31) Or KeyDown(200)-KeyDown(208))*move_speed#)

End Function

Function Create_Object()
	LevelEd.ent = New ent
	LevelEd\id = CreateCube()
	EntityType LevelEd\id, TYPE_OBJECT
	EntityPickMode(LevelEd\id, 2)
	Return LevelEd\id
End Function

Function Pick_Object(cam)

	;First, check to see if the user hit the mouse button
	If MouseHit(1)

		;Store the last picked object in this variable
		oldid = pick
		
		;Store the current picked object in this variable
		pick = CameraPick(cam, MouseX(), MouseY())

		;If we picked an object and it is not the object
		;BEFORE the currently picked object
		If pick And pick<>oldid
		
			;Set the CURRENTLY picked object's transparency
			;to half
			EntityAlpha pick, 0.5

		;Otherwise, if we selected a CURRENT object, 
		;and we click it again, 
		Else If pick And oldid = 0 Or pick = oldid
		
			;If we still have a VALID CURRENT object, 
			;and we click it again, 
			If pick
		
				;Set the CURRENTLY picked object's transparency back to normal
				EntityAlpha pick, 1.0
				
				;Reset the picked object to 0
				pick = 0
			
			;EndIf
			EndIf
		
		;EndIf
		EndIf
		
	;EndIf
	EndIf
	
	Return pick
	
End Function


Thank You!


Addi(Posted 2013) [#2]
1.
Use Types for storing the Ids of the selected objects then you are able to pick more than one objct while holding control:
...

Type selectedObId
     Field Id
End Type

...
     If objId <> pick and control Then
          ...
          sel.selectedObjId = New selectedObjId
          sel\id = pic
     Else If control Then
          For sel.selectedObjId = Each selectedObjId
               If sel\id = pic Then
                    ...
                    Delete sel
               End If
          Next
     Else
          Delete Each selectedObjId
          ...
     End If
...



Guy Fawkes(Posted 2013) [#3]
question.

Where did you get objid in this line?



I want to do this step by step to make sure I'm not missing anything.

Thanks!


Addi(Posted 2013) [#4]
sry :D objId is oldId from your code of course.
control is a variable in which the state of the control key is stored in.


Guy Fawkes(Posted 2013) [#5]
the state of the control key? So wait. I'm not RETURNING (Keydown(29)+KeyDown(157)) in the function, Control() ?


Addi(Posted 2013) [#6]
yes it is just a variable:
control = KeyDown(...)

Sry donīt know the number :D

But you are also free to write:
If ... And KeyDown(...) Then



Guy Fawkes(Posted 2013) [#7]
Ah, ok. will test after I get home from laser shopping :P


Guy Fawkes(Posted 2013) [#8]
Ok, here's what I have so far. It SORTA works. I need now to know how to for instance set the selected objects to semi-transparent, and when selected again, turn off transparency completely.

EDIT: I have it setting / selecting multiple objects transparent when holding down control as well as making the selected objects semi-transparent, but what about if i only want to select between 1 object and another object?

Or if i click 1 object & it's already selected, de-select it? And my last question is, what if I wanted to click any object that is not pickable (the background for instance) to have an object deselected?

How would I do that?



Thank You!


Addi(Posted 2013) [#9]
I forgot something:
Change this:

If oldId <> pick And control() Then


To

If pick <> 0 And oldId <> pick And control() Then



Guy Fawkes(Posted 2013) [#10]
Ok, I edited the function above to reflect on it.

Now that that's taken care of, I only have a few more things I need help with for now.

So you might wanna re-read to make sure I did the above function correctly so far.

Also, I'm getting a crash at:

If sel\id <> 0

when selecting single objects without holding control.

Thank You!


Addi(Posted 2013) [#11]
Tomorrow I will have a look at your function, but now it is to late ;)


Guy Fawkes(Posted 2013) [#12]
Ok, thanks alot, Addi! :)


Guy Fawkes(Posted 2013) [#13]
Any luck, bud? :)


Addi(Posted 2013) [#14]
Sure!
You need a global variable called "singleSelect"
Your old vars: oldId, pick and picked are no longer needed ;)

Function Pick_Object(cam)
	If MouseHit(1)

	    pick = CameraPick(cam, MouseX(), MouseY())
		
	    If pick <> 0
		
			If KeyDown(29) And isSelected(pick) = False Then
				sel.selectedObjId = New selectedObjId
				sel\id = pick
				EntityAlpha sel\id, 0.1
				
				If singleSelect Then
					sel2.selectedObjId = New selectedObjId
					sel2\id = singleSelect
					singleSelect = 0			
				End If
			Else If KeyDown(29) Then
				For sel.selectedObjId = Each selectedObjId
					If sel\id = pick Then
						EntityAlpha sel\id, 1
						Delete sel
					EndIf
				Next
			Else
				;unselect all selected objects
				clearSelection()
							
				;select or unselect the an object
				If singleSelect <> pick Then
					EntityAlpha pick, .1
					singleSelect = pick
				Else
					EntityAlpha pick, 1
					singleSelect = 0
				End If
			End If
			
		End If
	EndIf
End Function

;checks if an entity is selected
Function isSelected(entId)
	For s.selectedObjId = Each selectedObjId
		If s\id = entId Then Return True		
	Next
	
	Return False
End Function

;unselects all selected entities
Function clearSelection()
	For s.selectedObjId = Each selectedObjId
		EntityAlpha s\id, 1
		Delete s	
	Next
	
	If singleSelect Then EntityAlpha singleSelect, 1
End Function

;shows the ids of the selected entities
Function showEntityIds()
	Local c = 0
	
	For s.selectedObjId = Each selectedObjId
		Text 10, 25+c*FontHeight(), "multiselect: "+s\id
		c = c+1
	Next

	Text 10, 10, "singleselect: "+singleSelect
End Function



Guy Fawkes(Posted 2013) [#15]
THANK you! This is EXACTLY what I had in mind! :D

Hey, I would like to talk to you more, you seem like a real nice person. May I have the honor of sending you an email? :)

Thank You once again, my friend!


Addi(Posted 2013) [#16]
Sure, if you don't spam me ;).
I have not much experience with 3D programming but
if I can help you I will ;).

But I can only help you with your problems and give you examples.


Guy Fawkes(Posted 2013) [#17]
Wonderful, thanks alot! :)

Also, I tried to add a function that deletes all selected objects, it's only letting me delete them if I select them a few times first. I want to be able to delete the objects when they are selected, the 1st time round'.

I added a variable called "Global entselected", in order to tell it that I want to delete only when "entselected" is true.

What exactly am I doing wrong here?



Thank You!


Guy Fawkes(Posted 2013) [#18]
Hi man! :) So did you spot what I did wrong in the above code?


Addi(Posted 2013) [#19]
Havn't had time untill now ;).

Forget that code above you have to write a seperate function that is called
in the main loop if "x" was hitted. Also you have to delete the types in ent too. ;)


Guy Fawkes(Posted 2013) [#20]
? I don't understand.


Addi(Posted 2013) [#21]
What did you wrong:
You only delete the objects when MouseHit(1) returns true
so you have to klick on an object while holding down control and x ...

If you want to fix it just write your part after the MousHit() thing:

Function Pick_Object(cam)
	If MouseHit(1) Then
    	    ...
	End If

	If control() And KeyHit(45)
		For sel.SelectedObjId = Each SelectedObjId
			If sel\id<>0
				entselected = 0
				FreeEntity sel\id
				Delete sel
			EndIf
		Next
	EndIf
End Function


But I would make it so:
Delete this part of the function Pick_Object(cam):

If entselected
	If control() And KeyHit(45)
		For sel.SelectedObjId = Each SelectedObjId
			If sel\id<>0
				entselected = 0
				FreeEntity sel\id
				Delete sel
			EndIf
		Next
	EndIf
EndIf


If you have done this you have to write a function that delets the entitis
for example:

Function DelSelEntities()
	;Go through your list
	For s.selectedObjId = Each selectedObjId
	
		;Go through the ent list
		For e.ent = Each ent
		
			;If the e\id equals s\id delete the entity and the ent list entry
			...
		Next

		;Delete the entry in selectedObjId
		Delete s
	Next

	;If only one obj is selected delete that
	If singleSelect Then ...
End Function


Once you have done this you have to call that function in your main-loop:
While Not KeyHit(1)
	...
	If KeyHit(45) Then DelSelEntities():FlushKeys
	...
Wend



Guy Fawkes(Posted 2013) [#22]
Ok, here's what I have:



I deleted the above if statements as you said, and edited the DelSelEntities() function as you showed.

I put into the main loop,

If KeyHit(45) then DelSelEntities() : FlushKeys()


yet it still refuses to delete the entity or entities on the 1st press of the X key...

Here's the full code:



Thank You!


Addi(Posted 2013) [#23]
Hmm for me it works.
Code:




Guy Fawkes(Posted 2013) [#24]
That's AWESOME! NOW it works! I don't know what went wrong!

Now I can manipulate all the level editor functions this way. Delete can now become copy, etc...

I'll let you know if I need anymore help, bro!

Thanks again! :)


Guy Fawkes(Posted 2013) [#25]
Quick questions. How do I make the copy function I just created, select the newly copied entities?



And what am i doing wrong in this cut function?

I can't delete multiple copies.

I can only cut single selected entities.



Thank You!


Guy Fawkes(Posted 2013) [#26]
Just 2 more things to fix, then it should be good. :)

Thank You!


Guy Fawkes(Posted 2013) [#27]
Hi all! =) I'm having a bit of a problem trying to allow for movement of singly-selected objects. Multi-object movement works fine, but single object select won't let me move my object with the arrow keys / W/A/S/D...

What am I doing wrong here?



And yes, all variables are definitely global'd.

Thank You!


_PJ_(Posted 2013) [#28]
What are the ControlKeys() and spacekhit() functions?

And, if all vars are global as you state, why do you have:

whichmode=Which_Mode()

and Return whichmode from that function?

You may want to either just call a void function to change whichmode, or return a Local variable within the scope of the function that is promoted to the Global whichmode


Guy Fawkes(Posted 2013) [#29]
ControlKeys() return (KeyDown(29)+KeyDown(157))

spacekhit() returns (KeyHit(57))

I do it the way I do it. If it works for me, then it works for me no matter how "unsatisfactory" it makes someone.


Guy Fawkes(Posted 2013) [#30]
If someone could please help me with this final part of my level editor, I would appreciate it big time! :)

Thank You!


_PJ_(Posted 2013) [#31]

I do it the way I do it. If it works for me, then it works for me no matter how "unsatisfactory" it makes someone

The problem is, that it clearly doesn't work for you, else you wouldn't be having problems and quite frankly, so much of your code is so confusing and nonsensical it's very difficult to see what you're trying to do.

I only asked because I thought it may have been an oversight. If it was intentional, then fair enough, keep coding "unsatisfactoy" and keep having difficulties.

Function Set_Speeds#()
	If Not (KeyDown(42) Or KeyDown(54))
		move_speed# = .1
		turn_speed# = .3
	Else If (KeyDown(42) Or KeyDown(54))
		move_speed# = .3
		turn_speed# = .5
	EndIf
End Function


No need for both IFs. If it's NOT Keydown(42) or Keydown(54)
The it MUST be Keydown(42) OR Keydown(54)

Brush up on how logic works. No need for the # on the function name either, else it's always returning 0.0 for no reason whatsoever.

Function Set_Speeds()
Local Inc#=(Sgn((KeyDown(42))+(KeyDown(54))))*0.2

move_speed=0.1+Inc
turn_speed=0.3+Inc
End Function



YOUR MAIN PROBLEM, however, is with the Logic here:
If ControlKeys() And isSelected(pick) = False Then
				;...........
			Else If ControlKeys() Then
			


You need to really tighten up the specifics when dealing with Logic, dince it's very easy for the compiler to interpret very differently from what we may be assuming when reading as if in English.

I recommend using parentheses to ensure you're achieving what you intend to with the logic, and not uding too many Nested Ifs - If possible, separate some of it into Functions.

Something Like:
If (ControlKeys())
DoControlMovement(ent)
DoControlMovement(sel\id)
Else
DoNormalMovement(ent)
DoNormalMovement(sel\id)
End If







This should help you with logic:
[code]
If x And y = False Then
Print "Is True when X is <> 0 (or True) AND that Y=0 (or False)"
End If

If (x And y) = False Then
Print "Is True when there are NO common Bits between X and Y"
End If

If (x) And (y) = False
Print "Is True when Y=0 (or False) AND X has Bit "1" set (or True)"
End If


Guy Fawkes(Posted 2013) [#32]
Thanks, _PJ, I will try it out :)


Guy Fawkes(Posted 2013) [#33]
EDIT: Sorry for the double post. The forums must have messed up...


Guy Fawkes(Posted 2013) [#34]
Ok, so I did as you said, _PJ, and it's still allowing me to rotate my horse WHILE I try to move it using MoveEntity(). It's not supposed to turn at ALL when I'm moving it.



I'm trying to seperate the 3 while using 4 different modes controlled by a button in order to switch between the 4.

You have:

-Camera mode

-Positioning mode

-Rotating mode

-Scaling mode

I don't want any of these (other than the camera), to work while another mode is on.

Ex: I'm in position object mode with my mesh selected. I try to move him while holding the control keys, but he also rotates himself on the Y-Axis, causing a "circle-like" rotating effect while trying to position him, which is not right.

EDIT: Also, it's not letting me position, scale, OR rotate the object, while in "singleSelect" object mode. Meaning I can hold control and go into multiple object mode and have it selected, then the above happens, but when I select a single object, it doesn't let me position, rotate, OR scale it.

If you need any more info at all, or a better explanation, please feel free to ask!

Thank You!


RGR(Posted 2013) [#35]
Why don't you look for another hobby?
You are obviously not talented enough to program.

Since 2 or 3 years you ask in about 5000 posts for help in different forums always to fix a SMALL problem for you ...
There are 500 posts all over the place about YOUR "Level Editor"!
YOUR Level Editor? Just look through the threads and you will find out, that not one line of WORKING code is yours ... your's is the messed up Code you daily present to get fixed!

If you are not able to program, you must just look for something else what fits to your abilities!

.


Guy Fawkes(Posted 2013) [#36]
Take your rude comments, and leave this thread. NEVER come back. I'm done talking to you.


Bobysait(Posted 2013) [#37]
"YOUR MAIN PROBLEM"
_PJ_

to be honnest, his main problem is his attitude.

I do it the way I do it. If it works for me, then it works for me no matter how "unsatisfactory" it makes someone.


Sorry but, I've never met such a guy on every developer's community I use to participate.

Actually, your code is all but good, I'm sure there is a lot of people here that already have all the answers to your problems on this code, because the problem is quite easy but ... here comes the lack of motivation.
You can't stop firing people whenever they don't think the same as you. and fast come to insult them and that after you throw them out of the thread.
Give yourself a chance, and try to respect the persons helping you maybe.
You want some help, so be humble and start assuming your code is not good enough and your skill far from what it (maybe) deserves to be (else you wouldn't ask for help on such easy things).

Try and be more sympathic and I'll help you again.
Don't be, and you 'll give almost all the people here a good reason to forsake you. (cause actually, you really doesn't look like someone who wants to learn, but just someone who wants blitz-members to give you the product you want)


Guy Fawkes(Posted 2013) [#38]
I do it the way I do it. If it works for me, then it works for me no matter how "unsatisfactory" it makes someone.


This was an explanation. NOT an attitude.

BobbySait, you are not being answered again as well. Good Day.

If any of you are SERIOUS, then please. Feel free to talk to me, if not, then good day.


Bobysait(Posted 2013) [#39]
This was an explanation. NOT an attitude.

Stop being such an idiot, you know what I was talking about.

BobbySait, you are not being answered again as well. Good Day.

Do you only realise I'm not the one who need answers here ?
For myself, I think I can deal with you not answering me anymore :)

what about you ?


RemiD(Posted 2013) [#40]
Thundros>>Why don't you use the different commands available to debug your code ?

Debuglog("Variable = "+Variable)
Flushkeys()
Waitkey()

or

Print("Variable = "+Variable)
Flushkeys()
Waitkey()

Put one of these line when you want to check the state/value of a variable and you will manage to find what is the problem and how to correct the errors.

Asking for help or suggestions to learn how to do something is, i think, ok and the purpose of these forums, but asking to debug your code or even worse to code your game bit by bit is not a good way to learn.


_PJ_(Posted 2013) [#41]
I think, Thundros - that the issue with the rotating etc. comes from having the code for "A" "D" TurnEntity in the "PositionAllEnts" function, also, that W & S are also used to TurnEntity in the "RotateAllEnts" function.

I'm not sure, since it's not easy to follow the myriad of nested Ifs and repeated code blocks, but it's possible the answer may be there.