☑ Generate an entity with script

Hello, i want to create an entity when an item is dropped and i tryed this

// Create a new Entity
var bow = new pc.Entity();
bow.addComponent("model", {
    type: 'Asset',
    model: 'bow'
});
bow.setPosition=this.entity.getPosition();
// Add it to the Entity hierarchy
this.app.root.addChild(bow);

but don’t seem to work…what i do wrong? how i add a rigidbody?

This code looks pretty wrong.

Please use Dev Tools to catch exceptions and debug you code, here is a good page on how to: https://developers.google.com/web/tools/chrome-devtools/debug/console/track-exceptions?hl=en

I’ll be a bit direct here: but people could write code for you, but will you learn from it, that is the question. Because learning - is to be able to solve problems in first place, it is not about finding solution, it is more about being able to deal with challenges.
So other people could write your code - but there will be no use for your growth as developer.
Community is great when you’ve tried many things, you’ve encountered something very “wrong” and after number of tries and good investigation, you can ask community because people might have met similar problem before. But what you asking here is basically “please write code for me”.

There is User Manual, and API Reference, that actually hold many answers you are looking for.

Ability to learn and tackle challenges - is essential part of developers life.

you are right Max, but making big projects make u face problems all days and sometime asking for advices can speed up the work, when one explain how is done that make you learn, coz it’s not just cut and past action (not for me at least). There are many codes that one can read to learn, but most of them are very difficult coz functions are in different scripts and need a lot of time to understand. Anyway this code is being taken from the script in the developer section and slightly modified (maybe in the wrong way) anyway i will follow your advice and take a look at user manual and api reference, will also take a look at the dev tools u pointed out that i think will be of great help.

In long term - it does not save your or anyone else time actually. Opposite - you will be going through same: go ask question, approach, which is very slow way of doing things. But if you get better in solving problems by your self, you wont even need anyone to move forward, and it does gets better with time, just do not rush it. :slight_smile:

To help with JavaScript in general, really good place to start is to go through free course on codeacademy to do javascript.

Already doing codeacademy and freecodecamp curses :smiley: …and to not rush is a great advice…is just that sometime the urge to want achieve a result make u impatient :stuck_out_tongue: Anyway seems checking the api and the user manual bring me nowhere but i’m confident i will find the solution

Read on what addComponent method does.
Find out what arguments it has and what they mean.
At that point you’ll be able to find out how to add rigidbody. Remember that without collision component rigidbody wont do anything.
Then read what is setPosition and why setPosition = is wrong.

Start small, try simply add box from code to scene. Then improve as you go.
Use dev tools and console.log as you go to know what is going on.

Thanks for the advices, i will do that, u guys are always so useful :smile: I will show you the final result soon.

1 Like

ok… solved the mistery
if have found a reply about the addComponent issue with models here Addcomponent model asset
and i have corrected the setPosition like this: entity.setPosition(this.entity.getPosition());
now some minor things to adjust…like the size…it dropped a bow big as a palace lol

1 Like