Collect an item using bricks using python for script that spawns items(activate an item)?

Hi,

I wanted to activate one item, I wasn’t sure how it could work, other than like a brick setup where an item is added in, in the example I have.

import bge

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner

equipped = own[‘equipped’]
target = own[‘target’]
timer = own[‘timer’]
change = own[‘change’]
reload = own[‘reload’]
Grenades = own[‘Grenades’]
Smoke = own[‘Smoke’]
shoot = own[‘shoot’]

collide = cont.sensors[‘cutscene’]

#animations
down_m24 = cont.actuators[‘downm24’]
downGlock = cont.actuators[‘downGlock’]
upm24 = cont.actuators[‘pickupm24’]
upGlock = cont.actuators[‘pickupGlock’]
downGren = cont.actuators[‘down_Gren’]
upGren = cont.actuators[‘pickup_Gren’]
Sdown = cont.actuators[‘Sdown’]
Sup = cont.actuators[‘Sup’]
Kdown = cont.actuators[‘knifedown’]
Kup = cont.actuators[‘knifeup’]
opendoor = cont.actuators[‘opendoor’]

self = scene.objects[own.name]
movement = scene.objects[‘player’]

#sets change == True
if own[‘target’] != own[‘equipped’] and reload == False and shoot == True or own[‘equipped’] == 0 or own[‘target’] == 6:
self[‘change’] = True
elif own[‘target’] == own[‘equipped’]:
self[‘change’] = False

#timer
if change == True and timer < 30 and movement[‘door’] == False:
own[‘timer’] += 1

if change == True and movement[‘door’] == True and timer < 45:
own[‘timer’] += 1

#putting down weapon
if equipped == 0 and change == True and timer == 0:
cont.activate(downGlock)
if equipped == 1 and change == True and timer == 0:
cont.activate(down_m24)
if equipped == 2 and change == True and timer == 0:
cont.activate(downGlock)
if equipped == 3 and change == True and timer == 0:
cont.activate(downGren)
if equipped == 4 and change == True and timer == 0:
cont.activate(Sdown)
if equipped == 5 and change == True and timer == 0:
cont.activate(Kdown)
if equipped == 7 and change == True and timer == 0:
cont.activate(Kdown)
if equipped == 8 and change == True and timer == 0:
cont.activate(Kdown)

#ends old weapon + animation
if timer == 15 and change == True and equipped == 0:
cont.deactivate(downGlock)
if timer == 15 and change == True and equipped == 1:
own.sendMessage(“endm24”)
cont.deactivate(down_m24)
if timer == 15 and change == True and equipped == 2:
own.sendMessage(“endglock”)
cont.deactivate(downGlock)
if timer == 15 and change == True and equipped == 3:
own.sendMessage(“endGren”)
cont.deactivate(downGren)
if timer == 15 and change == True and equipped == 4:
own.sendMessage(“endSmoke”)
cont.deactivate(Sdown)
if timer == 15 and change == True and equipped == 5:
own.sendMessage(“endKnife”)
cont.deactivate(Kdown)
if timer == 15 and change == True and equipped == 7:
own.sendMessage(“endchain”)
cont.deactivate(Kdown)
if timer == 15 and change == True and equipped == 8:
own.sendMessage(“endhammer”)
cont.deactivate(Kdown)

#spawn new weapon
if timer == 15 and change == True and target == 1:
scene.addObject(‘m24’, own, 0)
if timer == 15 and change == True and target == 2:
scene.addObject(‘Glock’, own, 0)
if timer == 15 and change == True and target == 3:
scene.addObject(‘Grenade’, own, 0)
if timer == 15 and change == True and target == 4:
scene.addObject(‘smoke’, own, 0)
if timer == 15 and change == True and target == 5:
scene.addObject(‘knife’, own, 0)
if timer == 15 and change == True and target == 7:
scene.addObject(‘chain’, own, 0)
if timer == 15 and change == True and target == 8:
scene.addObject(‘hammer’, own, 0)

#picking up weapon
if timer > 15 and timer < 30 and change == True and target == 1:
cont.activate(upm24)
if timer > 15 and timer < 30 and change == True and target == 2:
cont.activate(upGlock)
if timer > 15 and timer < 30 and change == True and target == 3:
cont.activate(upGren)
if timer > 15 and timer < 30 and change == True and target == 4:
cont.activate(Sup)
if timer > 15 and timer < 30 and change == True and target == 5:
cont.activate(Kup)
if timer > 15 and timer < 30 and change == True and target == 7:
cont.activate(Kup)
if timer > 15 and timer < 30 and change == True and target == 8:
cont.activate(Kup)
if timer > 15 and timer < 45 and change == True and movement[‘door’] == True and target == 6:
cont.activate(opendoor)

#open sesame
doorsound = cont.actuators[‘doorsound’]
if timer == 30 and change == True and movement[‘door’] == True and target == 6:
select = collide.hitObjectList[0]
own.sendMessage(“opendoor”, “”, str(select))
cont.activate(doorsound)
#resetting properties
if timer == 30 and change == True and own[‘equipped’] != own[‘target’] and movement[‘door’] == False:
own[‘equipped’] = own[‘target’]
own[‘timer’] -= 31
cont.deactivate(upm24)
cont.deactivate(upGren)
cont.deactivate(upGlock)
cont.deactivate(Sup)
cont.deactivate(Kup)

#resetting after cutscene
if timer == 45 and change == True and movement[‘door’] == True and own[‘equipped’] != 6:
own[‘equipped’] = own[‘target’]
movement[‘door’] = False
movement[‘control’] = True
movement[‘cutscene’] = 0
self[‘timer’] = 0
own.sendMessage(“restorecontrol”)
cont.deactivate(opendoor)
own[‘target’] = 2

#intial weapon selection
if equipped == 0:
self[‘target’] = 2

Blockquote

So the player can collect an item instead of it being already in the player’s item spawn system.

I did try editing the example, to see if that could load an item once the player is nearby. That doesn’t work.

I am not sure how to activate an item from an oject and have that working from there on. I can only think of add object to load an item, but that wouldn’t be a functioning item.

from bge import logic

cont = logic.getCurrentController()
scene = logic.getCurrentScene()
own = cont.owner

#intial weapon selection
if equipped == 0:
self[‘target’] = 7

own.worldPosition = gunspawn.worldPosition
own.worldOrientation = gunspawn.worldOrientation

Or

gunspawn = scene.objects[“gun spawn”]

Including that above.

:thinking:

cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
own = cont.owner

equipped = own[‘equipped’]
target = own[‘target’]
timer = own[‘timer’]
chain = own[‘chain’]
reload = own[‘reload’]

#animations
down_m24 = cont.actuators[‘downm24’]
downGlock = cont.actuators[‘downGlock’]
upGlock = cont.actuators[‘pickupGlock’]
Sdown = cont.actuators[‘Sdown’]
Sup = cont.actuators[‘Sup’]
Kdown = cont.actuators[‘knifedown’]
Kup = cont.actuators[‘knifeup’]
opendoor = cont.actuators[‘opendoor’]

self = scene.objects[own.name]
movement = scene.objects[‘player’]

#sets chain == True
if own[‘target’] != own[‘equipped’] and reload == False and shoot == True or own[‘equipped’] == 0 or own[‘target’] == 7:
self[‘chain’] = True
elif own[‘target’] == own[‘equipped’]:
self[‘chain’] = False

#timer
if chain == True and timer < 30 and movement[‘door’] == False:
own[‘timer’] += 1

if chain == True and movement[‘door’] == True and timer < 45:
own[‘timer’] += 1

#putting down weapon
if equipped == 0 and chain == True and timer == 0:
cont.activate(downGlock)
if equipped == 2 and chain == True and timer == 0:
cont.activate(downGlock)
if equipped == 4 and chain == True and timer == 0:
cont.activate(Sdown)
if equipped == 5 and chain == True and timer == 0:
cont.activate(Kdown)
if equipped == 7 and chain == True and timer == 0:
cont.activate(Kdown)
if equipped == 8 and chain == True and timer == 0:
cont.activate(Kdown)

#ends old weapon + animation
if timer == 15 and chain == True and equipped == 0:
cont.deactivate(downGlock)
if timer == 15 and chain == True and equipped == 2:
own.sendMessage(“endglock”)
cont.deactivate(downGlock)
if timer == 15 and chain == True and equipped == 5:
own.sendMessage(“endKnife”)
cont.deactivate(Kdown)
if timer == 15 and chain == True and equipped == 7:
own.sendMessage(“endchain”)
cont.deactivate(Kdown)

#spawn new weapon
if timer == 15 and chain == True and target == 1:
scene.addObject(‘m24’, own, 0)
if timer == 15 and chain == True and target == 2:
scene.addObject(‘Glock’, own, 0)
if timer == 15 and chain == True and target == 3:
scene.addObject(‘Grenade’, own, 0)
if timer == 15 and chain == True and target == 4:
scene.addObject(‘smoke’, own, 0)
if timer == 15 and chain == True and target == 5:
scene.addObject(‘knife’, own, 0)
if timer == 15 and chain == True and target == 7:
scene.addObject(‘chain’, own, 0)
if timer == 15 and chain == True and target == 8:
scene.addObject(‘hammer’, own, 0)

#picking up weapon
if timer > 15 and timer < 30 and chain == True and target == 2:
cont.activate(upGlock)
if timer > 15 and timer < 30 and chain == True and target == 4:
cont.activate(Sup)
if timer > 15 and timer < 30 and chain == True and target == 5:
cont.activate(Kup)
if timer > 15 and timer < 30 and chain == True and target == 7:
cont.activate(Kup)
if timer > 15 and timer < 30 and chain == True and target == 8:
cont.activate(Kup)
if timer > 15 and timer < 45 and chain == True and movement[‘door’] == True and target == 7:
cont.activate(opendoor)

#open sesame
doorsound = cont.actuators[‘doorsound’]
if timer == 30 and chain == True and movement[‘door’] == True and target == 7:
select = collide.hitObjectList[0]
own.sendMessage(“opendoor”, “”, str(select))
cont.activate(doorsound)
#resetting properties
if timer == 30 and chain == True and own[‘equipped’] != own[‘target’] and movement[‘door’] == False:
own[‘equipped’] = own[‘target’]
own[‘timer’] -= 31
cont.deactivate(upGlock)
cont.deactivate(Sup)
cont.deactivate(Kup)

#resetting after cutscene
if timer == 45 and chain == True and movement[‘door’] == True and own[‘equipped’] != 7:
own[‘equipped’] = own[‘target’]
movement[‘door’] = False
movement[‘control’] = True
movement[‘cutscene’] = 0
self[‘timer’] = 0
own.sendMessage(“restorecontrol”)
cont.deactivate(opendoor)
own[‘target’] = 7

#intial weapon selection
if equipped == 0:
self[‘target’] = 7

That didn’t work. I am sure there is a simplier and to the point way of simply activating an item not available to the player without activation to spawn and use it.

guy, I really can’t understand your code, it’s so confusing - why don’t you make it easier? your pacifiers may contain information about objects that will be added when the player approaches, it is enough to check the scene through the for loop and these objects next to the player by property
for i in scene.objects: if “spawn_obj” in i and own.getDistanceTo(i)<=30.000:
add_obj = scene.addObject(i[‘spawn_obj’], i, 0) i.endObject() you can also add objects to inventory in another scene - just check the inventory cells - they are free or not (you need the cell property for this), if the cell is free, add the icon of your item there - for i in scenes['Scene_HUD].objects: if “cell” in i and i[‘cell’]==True: # do something with the cell - add an icon to it, move it or something else


I use such a system at home and it works very simply - information about the icons is stored in them - then it is convenient to read it and add the desired effect or object to the scene with the player through the icons

you need to stop copying and pasting someone else’s code in the hope that it will work - it won’t work that way because it was written for other purposes, you either have to write code for your project from the very beginning, or do everything on logic blocks - here you can help fix part of the code to indicate an error but not write code for the whole for free The block of the game - keep this in mind when asking for help

in order to be able to help you, in this case you will have to share your file and the original file from which you took the script - then you will be able to understand what you want, otherwise no one will guess from the pictures what can be fixed and what not

What methods are you using to optimize the performance of your game?
Just curious, I don’t want to create an discussion.
Why are you using Blender version 2.78?

In all your threads, it’s like you are talking to yourself :frowning_with_open_mouth:

if you do not provide the file, I, like others, will not be able to help you - good luck finding a solution

I tried the original version of the game. And it was slow asf … there’s definitively something wrong in the logics in the first place … let along the walls of code full of “if” … and i see now things like “sendMessage” to deactivate a controler that controls a simple temporary sprite plan in the game… it’s like there’s a tree of bricks for each single element (even a single sound)

It’s like the game was made from scratch in 24h in a game-jam tournament

Even if the youtuber that made this project was an absolute reference for me , really love his videos and knowledges, this game cannot be used to make some reverse-enginering to learn how to make a bge game. The whole architecture of the logics is absolutely disastrous as it seems that the creator didn’t took his time to make some analyse of the logic design … And i think modelinblender won’t even listen to this as he will keep post piece of code that won’t make sense when digged out of context…

I get you, buddy - well, if this is really the case, then it’s easier to write all the logic from the beginning than to use someone else’s code in the hope that it will miraculously work - especially if it is not logical, not optimized and very slow, there is no point in using it - using the “if” operator is really a typical beginner’s mistake when only this operator is used without branching conditions, I do not even know what to say here - I very rarely used someone else’s code for myself and never used code that I did not understand even if it worked perfectly for the author, of course I will see what the author of the post threw off, but I still do not understand what he wants to get? Maybe it’s a combat system? Maybe the control logic is a weapon? or the logic of artificial agents? or all at once - if the latter, then it’s like how beginners ask guys, I have something here, write the whole game for me - it’s funny on the one hand :smile:




and so I looked at your file there are 81 scripts I can’t figure out what your problem is? how did you write the problem in the 7 key? I click and I get a chainsaw that works when I press the mouse button, but there is no sound of a chainsaw - do you want to transfer all this to your game? I would advise you to make something simpler, since even with hints you are unlikely to be able to do what you plan, I also checked the console there are no errors in it, but your cube has a cyclic dependence on the curve object - curves do not work in the BGE game engine since in the viewing window - you may need to disable use this dependency to fix your problem
yes, something else - you do not have scale and rotation applied to all objects, this will cause problems with physical collisions when trying to process such logic, perhaps the errors that you have are related to scaling objects - to apply scale, use ctrl+A scaling and edit your objects then let us know if there are repeated errors

it has overall the potential to make you waste your time trying to figure out the logics behind :rofl:

This file has 2 values : the existing assets and some python syntax if any…

My advice : restart from 0 and rebuild the project your way and proceed by steps

1* Moving the character in a map (very simple for a fps)
2 * Game meta (load, save, restart , boot .. )
3 * Managing events (lights switching on/off, cutscenes .. )
n* …
9* … UI ?
10* .. Making enemies intelligent

You have to rethink how to conceptualize all the elements and try to group them into families , categories, classes or whatever you call them

I can’t explain it otherwise, it’s something you must get . You should be able to write it down on paper. If you don’t get by yourself in the first place that the logics in the game are just poorly designed (very very) then you have no chance to correct them afterwards ..

Removing assets will just break the game if the code is looking for things that are no exisiting anymore .. unless you have cut all the necessary in the code too without messing the dependencies …

Restart the project from 0 mb :wink:

Buddy, I did not change or fix the events related to the 7 key - I immediately turned on and checked pressed 7 and everything worked - you saw the screenshots and the screenshot of the console - I have no errors, this means that everything works - maybe your computer just can’t cope with the load of the game, it happens on weak machines - I would advise you split the logic - keep the file with the player and his weapons separately and link to it as a group and also keep complex elements - enemies, equipment, etc. separately, and attach everything to the game file through a link - then this will give you an increase in productivity and possibly errors that you have due to lack of grip the computer’s memory will disappear

when you use groups, it will be easier for you to edit everything than to try to do it in one file - you just select so the player does not pull out the gun on the 2 key - open the player’s blend file and fix the error there, save and return to the game file where you have game levels and links to the player, enemies, and others you check the items and I’ll tell you right away the links and their attachment work better than the original models - and it’s easier to fix errors

the original code is confusing and not clear, I previously wrote that it makes no sense for you to copy all this and try to assemble a game from this set, the same thing was written to you by blenderraptor - if you really want to create your own game where you can do something and understand how it works, you need to rewrite the player script - I will send you an example and you will do everything else yourself - for example, there is just everything - the selection of items, if they are not selected, you will not be able to use them, and random generation of ammunition. PS - do not try to make a dream game based on other people’s examples, it will not work, do everything yourself

fps_shooter_example.blend (168.2 KB)

just let him mumble

if you can’t create a game yourself, you won’t be able to do it even with the help of other people’s files - BGE and other engines don’t have a button to “make a beautiful game” - I repeat, there is no point in correcting someone else’s code for you, you will get stuck for a long time in this and won’t make your game - listen to the advice of experienced users - start studying BGE and python will benefit instead of trying to fix someone else’s not optimized and complicated code - if you don’t want to listen to me, then do as you see fit - good luck

in my opinion, it is useless to say something - the person does not want to listen to me, then it remains to leave him alone - let him try to fix other people’s files - as my friend the programmer who works on Unity says - let them do GTA, but I will have fewer competitors :rofl: