Author Topic: Solution for bullet  (Read 7380 times)

Dasca

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Solution for bullet
« on: April 12, 2014, 01:13:31 PM »
Hi all,

I'm stuck in a problem that I can't figure out.

This is the easy rappresentation of the situation:
Left mouse click
Tank (TNObject) create a bullet via  TNManager.Create(tankShell, etc..
Bullet (TNObject) fly away!
Bullet collider hit something
Bullet check if there is something interesting to blow out

this is the point where I can't understand how to continue...

test 1: Bullet tno.Send("DestroySomething", Target.All, c.gameObject); c is the collider
this solution doesn't work because I receive this message: Unable to write type UnityEngine.GameObject (gameObject already insert into the TNManager list!)

test 2: Bullet tno.Send("DestroySomething", Target.AllSaved, c.gameObject.GetInstanceID());
I can't retrieve the gameObject by the Id I receive to destroy it on all the clients.

Any idea?

thank

Davide

djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: Solution for bullet
« Reply #1 on: April 12, 2014, 01:38:29 PM »
Are the objects you are talking about other tanks, or are they other things within the scene? I'll assume you're doing a peer to peer architecture based on your code.

If it's other tanks you're hitting, assign a player to a tank at the start, and then send the player id when you have to. When everybody receives it, they should apply damage to the tank with the matching player id.

If it's other things, the answer is similar. At the start of the game you would need to find some way of assigning an id to each object and syncing it to everybody. Every time an object is added to the scene, the host player could tell everybody what it's id is.


Dasca

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Solution for bullet
« Reply #2 on: April 12, 2014, 05:25:19 PM »
Hi dyray2k

sorry if my example is not clear...

what I'm triyng to do is this:

In my "game" I've some clients tank, there are also structures that I would like to be destroyed by the tank bullet, I hope there's a way to just send which of those structures are hit by the collider of the tank bullet created with the TNManager.Create(tankShell, etc..

hope this could help you and others understand my issue :)

thx again for your reply

Dasca

djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: Solution for bullet
« Reply #3 on: April 12, 2014, 10:20:07 PM »
Yeah, you'd basically need a way to say this specific object or structure. One way would be to give each object an unique name, and then send that name over. You could also just send the sphere explosion to everybody and just have them destroy everything in that range. That would only work if the objects are static.

I understand want you want, but but idk if I'm helping enough.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Solution for bullet
« Reply #4 on: April 12, 2014, 11:22:41 PM »
Quote
Tank (TNObject) create a bullet via  TNManager.Create(tankShell, etc..
Don't do this. Don't create bullets via the server. There will be too many bullets, and every time you create something via TNet it has to allocate an ID for it only for something that will exist for less than a second... this is bad practice.

Don't bother making bullets server-side. Just send a non-saved RFC (Target.All or Target.Others) telling players to create their own bullet at the specified location, direction, with the set owner, etc.

Dasca

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Solution for bullet
« Reply #5 on: April 13, 2014, 12:31:48 PM »
thx djray2k for your help but still I'm unable to let it work  :-[

I'm trying to follow Aren suggestion but I found some problems.

after the tank send the fire command
[RFC]
void Fire (Vector3 dir, int weaponIndex)
{
    Instantiate(weapons[weaponIndex].weapon, firingPoint.position, Quaternion.LookRotation(dir));
}

all players in game do their own bullet and that's fine
in the bullet code there this send RFC

v.GetComponent<TNObject>().Send("SetDamage", tankPlayer, damageToApply);

but is executed by all players so the damage is twice if there are 2 players and triple if there are 3 players and so on

Who could I just apply the damage once, the bullet is not a tnobject and everyone own the one they created and also I could not say in the local instance "this is mine and is the right one to set damage to others"

thx

Dasca

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Solution for bullet
« Reply #6 on: April 14, 2014, 03:53:39 AM »
Check the owner, and only send if tno.isMine (only one player owns each object).
  1. TNObject tno = v.GetComponent<TNObject>();
  2. if (tno.isMine) tno.Send("SetDamage", Target.All, damageToApply);
Not sure why you'd want to send it only to the tank player. Maybe you want to send it as a parameter instead, to let everyone know who did the damage?
  1. if (tno.isMine) tno.Send("SetDamage", Target.All, damageToApply, tankPlayer.id);

Dasca

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Solution for bullet
« Reply #7 on: April 14, 2014, 08:33:19 AM »
Thx, Aren, I'm gonna try this solution

One more question about Send Target.AllSaved

In my game I've many buildings the players tank can destroy, if I not misunderstood the AllSaved is overwritten every time, I need a sort of "level manager" to handle all the buildings destroyed and remove them from the level when new player join?

thx again

Dasca

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Solution for bullet
« Reply #8 on: April 15, 2014, 10:30:08 AM »
AllSaved/OthersSaved should be used to keep the "latest state" of any RFC. Each subsequent message sent with those flags will overwrite the previous, and when the new player joins they will receive the latest saved version of that RFC.

If the RFC is not saved, newly joined players will not receive it at all.

Dasca

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Solution for bullet
« Reply #9 on: April 29, 2014, 03:11:03 AM »
Hi Aren,

thx for the reply, so if I understand corectly I need a sort of "level manager" to handle all the buildings destroyed and remove them from the level when new player join?


Dasca

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Solution for bullet
« Reply #10 on: April 29, 2014, 03:08:30 PM »
Objects destroyed via TNManager.Destroy will stay destroyed (assuming you have a TNObject on them).

Dasca

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 14
    • View Profile
Re: Solution for bullet
« Reply #11 on: April 30, 2014, 04:02:30 AM »
No Aren, they are static buildings, a lot of building, I thought put a TNObject was too demanding...


djray2k

  • Jr. Member
  • **
  • Thank You
  • -Given: 4
  • -Receive: 4
  • Posts: 74
  • Blockey Hockey!
    • View Profile
    • Walking Talking Grilling Bear Studios
Re: Solution for bullet
« Reply #12 on: April 30, 2014, 09:24:02 AM »
He means creating TNObjects for things that die really quick are bad. You can have many TNObjects, but creating many in a short, important time is no good.

TNObject is OK for buildings because they're being created at the start and they're not going to be sending any data while the game is running. When a building with TNObject is destroyed, everybody else is automatically told.

If you don't want to use TNObject, just give them some sort of ID and send a message to everybody that the building with this ID is destroyed. You can just manually assign an building id to each building. That's essentially what the TNObject does.
« Last Edit: April 30, 2014, 09:48:07 AM by djray2k »