Edit - see post #10 for latest developments including any number of targets/followers
Hi all, I’ve recently been taking my first steps in the blender GE and I’m surprised but I’ve actually had a small amount of success :eek:
Concept - A simple scene with two objects; 1 target, 1 follower. The target is placed by the mouse point/click & the follower then moves to the target. Currently target/follower are basic meshes for proof of concept, but could be more complex meshes/characters.
Implementation - The following script (looks simple enough but was frustratingly difficult to write since I had to learn a lot of python basics :o) & GE setup for the target, and another GE setup for the follower.
Target Script (‘move’)
import GameLogic
import Rasterizer
#init stuff, show mouse
if not hasattr(GameLogic,'init'):
GameLogic.init = 1
Rasterizer.showMouse(1)
# load controller, set sensors
py = GameLogic.getCurrentController()
always,move,click = py.sensors
# load target "Cube"
obj = GameLogic.getCurrentScene().getObjectList()["OBCube"]
# find mouse
if move.positive:
pos = move.getHitPosition()
# set "Cube" at mouse position on click
if click.positive:
obj.setPosition(pos)
Explanation - when the lmb is clicked the value ‘track’ is shifted from 0 to 1; when the follower reaches the target, the value is changed back to 0 (also some decelleration takes place to stop it bumping off the target). While track = 1 the cube will track-to the follower and advance along it’s local y-axis. There is a slight delay to the track-to constraint which makes it turn while moving - for personal preference!
Rendered screenshot since I’m having some issues making the runtime - I’m working on my girlfriends computer at the mo, it doesn’t have all the .dlls required but I don’t think I’m allowed to change anything!
One question - In the terminal when running the game I get a message saying certain things are ‘deprecated’. What does that mean/what should I change?
Thats very cool, it’s really easy to get started with the BGE somehow.
For now I have only played around with logic bricks but could already do a lot with that, how did you get around learniny Python?
I sound lazy for asking, think I better just get started :o
Well I already have some coding experience (C++) for uni, especially in problem solving, so it wasn’t much of a jump - and Python seems a lot easier to use than C!
For this I had to learn some python because I distinctly wanted a point/click control interface and that just wasn’t available with the sensor/control/actuator blocks alone, although I tried pretty hard at first!
Eventually I googled the problem (mouse control in blender game engine) and had a look at what other people had done - there wasn’t any one solution that fitted my requirements, and no straightforward tutorial, but there were some that were pretty close and by studying those scripts and the blender game engine python reference docs, notably - GameLogic,I managed to figure out what was needed.
Nice idea! It works great, the depreciated code just means that it’s using older code but it should still work. You don’t need to change anything unless it doesn’t work. I’m figuring out how to get the Cube object that the player follows to stick to the plane only, and not raise up when itself is clicked.
Ok that was quick. I found out how to limit the marker that the player follows to the plane. Select the Cube marker and add 1 sensor, controller, and actuator and link them up. Make the Actuator a constraint along the Z axis and set it’s coordinates to the markers Z value for both Min and Max box. Select the Camera and enable the True level triggering. Select the red player cube and also enable True level triggering on the Mouse Left click sensor. I can’t send the .blend file though, it’s over 1mb.
Oh neat, I hadn’t thought of that. I didn’t actually try clicking on the marker or the follower red cube, so I missed it.
I’ve been trying to think of how to apply this function to an uneven surface, for example a fractalised (is that a word?) plane to simulate a landscape. The follower cube could easily interact using the blender physics but I don’t really want to do that for the target pointer…
If there’s a function that calls the z value of the plane at a certain x/y value, it would be easy to implement that in the script… will have to look at the Python ref docs again and give it a go.
Thought of another, easier way of doing it; apply the script to the plane, not camera, and change the ‘mouse’ sensor input to mouse over rather than mouse over any. This way the script only returns the mouse coordinates if the cursor is over the plane; over the cube or target, it does nothing (and they stay where they are).
Edit - tweaked a bit more - changed the followers motion to location rather than linV, and added a constant downwards force to the cube to simulate gravity. Finally I subdivided the plane a couple of times, moved some of the v’s up and down to make slopes, then subsurfed it. Result; point/click movement command tool over a simple landscape!
Even more development of this and I’ve made the following changes
Added ‘select’ state to follower cube, activated/deactivated by rmb over cube. Cube only follows when- select=1.
Added second set of target/follower cube. Implemented extra code in script that means target cursor is only placed if relevant follower cube is selected. Also added code so that right-clicking on the plane deselects all.
Made the target spin (it looks cooler:cool:)
I’m not sure I’ve done this the best way, since the script uses a list of all the targets/followers and therefore for a larger number of sets needs a longer list… I might be able to get around this by generating an automatic array of targets/followers from the scene and basing the pt/clk if on a loop through that array, but I need to learn more about python and the blender GE before I can do that. But for now it works! :)
Current problems; if both followers are going to the same place they collide, and I’d like to have the targets disappear unless the relevant follower cube has select & track = 1, but I think I can implement both of these pretty easily.
Here’s the new script
import GameLogic
import Rasterizer
#init stuff, show mouse
if not hasattr(GameLogic,'init'):
GameLogic.init = 1
Rasterizer.showMouse(1)
# load controller, set sensors
py = GameLogic.getCurrentController()
always,move,click1,click2 = py.sensors
# load actor/target objects
obj1 = GameLogic.getCurrentScene().getObjectList()["OBtarget_1"]
obj2 = GameLogic.getCurrentScene().getObjectList()["OBtarget_2"]
actor1 = GameLogic.getCurrentScene().getObjectList()["OBactor_1"]
actor2 = GameLogic.getCurrentScene().getObjectList()["OBactor_2"]
# find mouse
if move.positive:
pos = move.getHitPosition()
# if mouse is clicked
if click1.positive:
# if actor is selected
if actor1["select"]==1:
# set actors' target at mouse location on plane
obj1.setPosition(pos)
if actor2["select"]==1:
obj2.setPosition(pos)
# deselect all actors if rmb is clicked on plane
if click2.positive:
actor1["select"] = 0
actor2["select"] = 0
I’m still working on this, although it doesn’t seem to be generating much interest…
After learning more python I’ve rewritten part of the script to look for any object with the ‘select’ property, and then it runs through all of those in turn to see which are selected. Any that are selected are told to track to move to the target object on a leftclick, as in the previous script.
The benefit of this is that you can have any number of objects under the players control without editing the script; all you have to do is each of them give them ‘select’ and ‘track’ parameters, then set up the GE blocks like the previous examples, with ‘track=1’ making them track and move to the cursor, then getting near the cursor causing them to stop.
Here’s the new script
import GameLogic
import Rasterizer
# init stuff, show mouse
if not hasattr(GameLogic, 'init'):
GameLogic.init = 1
Rasterizer.showMouse(1)
# load controllers, get sensors, actuators, scene
g = GameLogic
s = GameLogic.getCurrentScene()
py = g.getCurrentController()
always,move,lmb,rmb = py.sensors
# load objects
objList = s.getObjectList()
# choose cursor
cursor = objList["OBcursor"]
# find cursor
if move.positive:
pos = move.getHitPosition()
# with lmb click, place cursor
if lmb.positive:
cursor.setPosition(pos)
# load all objects with 'select' attribute
for i in range (0, len(objList)):
obj = objList[i]
if hasattr(obj,"select"):
#if object is selected
if obj["select"] == 1:
#if mouse over plane & rmb, deselect
if move.positive and rmb.positive:
obj["select"] = 0
#if lmb is clicked, track cursor
if lmb.positive:
obj["track"] = 1
And a blendfile of several objects with one cursor. Also here is another little project I’ve been working on, combining this with character animation; click to make the blue dude walk around!
One problem with the new script is that objects which are set to track, don’t stop tracking the cursor until they reach it, even if they are deselected. So if a new cursor is placed all currently moving objects will track to the new location, rather than just the selected ones. I’m still working on it.
You may not be getting many replies but this is the kind of thing hundreds of people are going to be looking to for possibly years into the future when they try to make their games. Keep it up! (:
Very nice work!
Thank you for sharing!
About the interest, well, this is the “foundations”, it takes time to “get” something!
Sooner or later you’ll be “rewarded”!
Bye
Man I am very, very interested in this! I am trying to make a point and click adventure, and I was hoping to find a template like the one for FPS but I hadn’t found one. This is very nice work, the blueman demo really blew me away cause Its something I was trying to do, but I’m a noob in python by now. I think this should go sticky in the forum “How to make FPS, RPG…” etc, cause this would be the first Adventure Game template. Awesome work man, if you make an Adventure Game template it would be so awesome and hundreds of people would use it, starting by me Thanks and keep up the good work!
Hey I just gotta say, this kind of stuff could lead to a pretty complex point click RPG game. I made a Blend file with the click cursor like in the online game RuneScape. I’ll attach the Blend file.
Edit: I forgot, click to move and spacebar is the action button. The brown square bin is the shipment bin, put the metal in that.
This will take some examination but you guys have pointed me in the right direction. Many Thanks.
One note: Normally, isn’t left click the selector and Right click the de-selector? Food for thought.