I tried to make an inventory of blocks i could place with this.But it did not work.Like VegetableJuiceF said.What am i doing wrong?
import bge
# block placement and deletion script
def manipulation():
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner
#create initial properties for owner
if not 'init' in own:
own['init'] = 1
# self explanatory
own['obj_list_index'] = 0
own['obj_list'] = ['block','Octogon','coloredblock','cobestick']
#access all the sensors
sensor = cont.sensors['over_any']
left_click = cont.sensors['left_click']
right_click = cont.sensors['right_click']
change_obj = cont.sensors["Keyboard"]
#################################
# user wants next obj in inventory
if change_obj.positive:
# use mod % to not loop back to start
own['obj_list_index'] = (own['obj_list_index'] + 1)% len(own['obj_list'])
else change_obj.negative:
own['obj_list_index'] = (own['obj_list_index'] - 1)% len(own['obj_list'])
#debug
print("Placement obj is now:", own['obj_list'] [ own['obj_list_index'] ] )
#current_item
inventory = own['obj_list']
index = own['obj_list_index']
current_item = inventory[index]
#################################
#get the information needed
hit_object = sensor.hitObject
hit_normal = sensor.hitNormal
#for debug
print(" ")
print("Hit object:",hit_object)
# proceed to add block
if left_click.positive:
#make sure we hit something
if hit_object :
#debug
print("adding blocks")
print("Normal:",hit_normal)
print("Hit obj pos:",hit_object.worldPosition)
print("New location:",hit_normal+hit_object.worldPosition)
# get new position based on mesh location and the handy hit_normal
NewPosition = hit_object.worldPosition + (hit_normal)
# add new block to scene
################################## use the object form inventory
new_block = scene.addObject( current_item ,own)
# by default i thas to spawn somewhere,
# the owner of this script is default
# now lets set its's position to where we meant it to be
# based on the calculations
new_block.worldPosition = NewPosition
# Be aware, that spawned object inherit the rotation of the script owenr
# the camera has rotation, so we must reset it
# you can comment this out to see why it's important
new_block.worldOrientation = (0,0,0)
# proceed to delete block
elif right_click.positive:
#make sure we hit something
if hit_object :
# debug
print("Delete block")
# just kill it
hit_object.endObject()
manipulation()
[ and ] change states
1 = places “Block” if you have any, subtracts 1 from own[‘Block’]
2 = places “Block2” if you have any, subtracts 1 from own[‘Block2’]
3 = places “Block3” if you have any, subtracts 1 from own[‘Block3’]
also added logic to display number of blocks remaining,
Attachments
PlaceBlockExample3types.blend (486 KB)
added ability to remove blocks with Q
Attachments
PlaceBlockExample3typesandRemove.blend (489 KB)
This is wrong.
if change_obj.positive:
# use mod % to not loop back to start
own['obj_list_index'] = (own['obj_list_index'] + 1)% len(own['obj_list'])
<b><i>else change_obj.negative: </i></b>
own['obj_list_index'] = (own['obj_list_index'] - 1)% len(own['obj_list'])
You use the key press down( sens.POSITIVE) for moving forward in the list,
and the key press up (sens.NEGATIVE) for moving back in the list.
Basically if you press the key and release it, you are back at +1-1=0.
Read more on (keyboard) sensors, and what they output.
(There should be like 3 states.)
Here is your modified blend back.I made it work.It was a simple fix.And this is what i did.
#access all the sensors
change_obj = cont.sensors["Keyboard"]
changed_obj =cont.sensors["Keyboard2"]
#################################
# user wants next obj in inventory
if change_obj.positive:
# use mod % to not loop back to start
own['obj_list_index'] = (own['obj_list_index'] + 1)% len(own['obj_list'])
if changed_obj.positive:
own['obj_list_index'] = (own['obj_list_index'] - 1)% len(own['obj_list'])
Attachments
block_placement_easy_with_inventory2.blend (363 KB)
Now make it look pretty also :).
It is bad practice to use names that are too similar.
You shoot yourself in the foot.
Later on you won’t remeber which one is change_obj and which one is changed_obj.
Also this:
change_obj = cont.sensors[“Keyboard”]
changed_obj =cont.sensors[“Keyboard2”]
Should be:
iterate_up = cont.sensors[“up_arrow”]
or
scroll_up = cont.sensors[“scroll_up”]
This is what i did to the code.What is your opinion?
import bge
# block placement and deletion script
def manipulation():
scene = bge.logic.getCurrentScene()
cont = bge.logic.getCurrentController()
own = cont.owner
#create initial properties for owner
if not 'init' in own:
own['init'] = 1
# self explanatory
own['obj_list_index'] = 0
own['obj_list'] = ['block','Octogon','coloredblock','cobestick']
#access all the sensors
sensor = cont.sensors['over_any']
left_click = cont.sensors['left_click']
right_click = cont.sensors['right_click']
tab_up = cont.sensors["up_keyboardkey"]
tab_down =cont.sensors["down_keyboardkey"]
#################################
# user wants next obj in inventory
if tab_up.positive:
# use mod % to not loop back to start
own['obj_list_index'] = (own['obj_list_index'] + 1)% len(own['obj_list'])
if tab_down.positive:
own['obj_list_index'] = (own['obj_list_index'] - 1)% len(own['obj_list'])
#debug
print("Placement obj is now:", own['obj_list'] [ own['obj_list_index'] ] )
#current_item
inventory = own['obj_list']
index = own['obj_list_index']
current_item = inventory[index]
#################################
#get the information needed
hit_object = sensor.hitObject
hit_normal = sensor.hitNormal
#for debug
print(" ")
print("Hit object:",hit_object)
# proceed to add block
if left_click.positive:
#make sure we hit something
if hit_object :
#debug
print("adding blocks")
print("Normal:",hit_normal)
print("Hit obj pos:",hit_object.worldPosition)
print("New location:",hit_normal+hit_object.worldPosition)
# get new position based on mesh location and the handy hit_normal
NewPosition = hit_object.worldPosition + (hit_normal)
# add new block to scene
################################## use the object form inventory
new_block = scene.addObject( current_item ,own)
# by default i thas to spawn somewhere,
# the owner of this script is default
# now lets set its's position to where we meant it to be
# based on the calculations
new_block.worldPosition = NewPosition
# Be aware, that spawned object inherit the rotation of the script owenr
# the camera has rotation, so we must reset it
# you can comment this out to see why it's important
new_block.worldOrientation = (0,0,0)
# proceed to delete block
elif right_click.positive:
#make sure we hit something
if hit_object :
# debug
print("Delete block")
# just kill it
hit_object.endObject()
manipulation()
All good.
I would delete comments that are not necessary for yourself,
as I wrote them to help the full beginner to understand .
They were never meant to be used in any productive way.
Most of them clutter the screen and make reading code more tedious than helpful.