0
$\begingroup$

I want to draw a line using the BGE module. Start from this point:

enter image description here

From this question: Python script fail, look in the console for now

EDIT: I just tried the code you have and it works fine. I think your issue is how you are trying to execute the code. To execute this code in the BGE you will need to have an object that has a sensor (such as an always sensor with true level triggering enabled) connected to a python controller with your script selected. Then play your game and you will see your line. Simply running the script via the text editor will cause it to fail.

  • The first step would be create an object.
  • The second step would be to put a sensor in the object
  • The third step would be to put true level triggering enabled
  • The fourth step would be to connect the object with the sensor to a Python controller with the script selected.
  • The fifth step would be to play my game

I enumerated the steps but I don´t know how to do every step, that´s my question.

$\endgroup$
2
  • 1
    $\begingroup$ What does this screenshot show? $\endgroup$ Jan 6, 2018 at 19:44
  • $\begingroup$ @Duarte Farrajota Ramos: the initial state, for starting from nothing in the scene. $\endgroup$
    – Mr. Baldan
    Jan 6, 2018 at 19:53

1 Answer 1

2
$\begingroup$

In order to draw a line with the game engine, we'll need to start with an object. The reason for this is that we cannot execute a python script in the game engine without an object to execute that script.

Since the line is the focus here, we can use an empty so there will be no object rendered in the game.

enter image description here

On the left is the viewport, and on the right what the game looks like when we play it. Now we have an object to execute our python script, so we can go ahead with setting that up.

To do this we will need to use the Logic Editor which can be found here:

enter image description here

Once we are in the Logic Editor we need to add an always sensor and a python controller from the menus at the top. One you have them, drag and drop to connect the output from the Always sensor to the input of the Python controller. Finally we should chack True level triggering (The three raised dots) on the Always sensor. This will make the sensor trigger the controller every logic tick (which is 60 times per second by default if I recall correctly. Also note this isn't necessary to draw a line, but if you plan on adding anything requiring that will require updates while playing then it's a good idea).

One all that is done the Logic Editor should look like this:

enter image description here

The final step is to create the Python script and select it in the Python Controller. Open up the text editor, create a new text file and add the two lines of code. You already know how to do this part, but for those who don't the code is:

import bge

bge.render.drawLine([0,0,0],[10,10,10],[255,40,0])

Once you have your script (in my case the text file is simply called Text, but a more descriptive name would be better), save it to your computer by opening the Text menu in the text editor and choosing Save. Finally, select the test file in the field on the Python controller.

enter image description here

On the left is the controller, and on the right the text file that includes our script.

All that's left to do now is play the game. I recommend saving your blend file first, just in case. Then hover your mouse over the 3d view and press P to play the game. If the game doesn't start, make sure you are using the Blender Game engine, and not he Blender Render Engine or Cycles. If all went well, your game will look like this:

enter image description here

EDIT: It is a better idea to try to figure out the problem yourself, rather than ask for a complete guide, the simple reason being you will learn how things work a lot better by trial and error than by following a tutorial. If you have more problems like this in the future, try figuring out your problem by yourself, and then ask a single question about the first issue you encounter. If you simply don't know where to start, you can ask how you should go about starting. If you don't know how to execute a script properly, then ask that. In general, ask questions that solve your current issue and nothing more. That way you are forced to learn how to do something, as well as why it works that way, instead of just being able to follow a guide (I say this all from personal experience).

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .