1. Hey Guest, is it this your first time on the forums?

    Visit the Beginner's Box

    Introduce yourself, read some of the ins and outs of the community, access to useful links and information.

    Dismiss Notice

if (getNet().isServer())

Discussion in 'Modding Help' started by Tsilliev, Mar 16, 2016.

  1. Tsilliev

    Tsilliev Haxor

    Messages:
    414
    Can someone explain me what does the if (getNet().isServer()) and if (getNet().isClient()) do?
    I am having a bit of trouble with my mod, I introduced biomes, where the grass and tree color is changed, randomly on each match, the number that is randomized is actually in the reset function of RedBarrier.as, because well I had trouble fetching that from CTF.as or creating a new script and attaching it in the gamemode.cfg.
    The problem is that anyone that joins the server have different biome, or fetch a different randomized number. I tried puting if (getNet().isServer()).... but it does nothing, nothing is happening then, hoped it would solve that issue and that anyone that joins will fetch the number that the server side generated not their client one.
    Code:
    void Reset(CRules@ this)
    {
        barrier_set = false;
        //this.set_s32("biomeR",XORRandom(3));
    
        getRules().set_s32("biome", XORRandom(12));
            print("RB Biome# "+ getRules().get_s32("biome"));
       
    
    
    
    }
    
    About another issue is that, I have consolidated the building workshops, I have workshops in the workshop itself, meaning expanding menues and such, like when you build the workshop, you can build 3 other workshops that have the different shops in them, mainly to sort everything out. When I click on a workshop the menu appears instantly without having to press the E button to access it because I send command to initialize it on click,
    Code:
    if (cmd == this.getCommandID("shop made item"))
        {
            CBlob@ caller = getBlobByNetworkID( params.read_netid() );
            CBlob@ item = getBlobByNetworkID( params.read_netid() );
            if (item !is null && caller !is null)
            {               
                this.getSprite().PlaySound("/select.ogg" );
                this.getSprite().getVars().gibbed = true;
               
    
                // open factory upgrade menu immediately
                if (item.getName() == "military")
                {
                    CBitStream factoryParams;
                    factoryParams.write_netid( caller.getNetworkID() );
                    item.SendCommand( item.getCommandID("shop menu"), factoryParams );
                }
           
            }
            this.server_Die();
        }
    thus the problem is that the old menu still hangs for a few seconds. Mind that it is perfectly fine when I am the only one in my team, I click on a workshop the old menu disappears and new one appears no problems, but when someone else joins and starts using the workshops, then this problem resurfaces. I tried putting if (getNet().isServer()) or if (getNet().isClient()) but it just breaks it.

    Any suggestions?
     
  2. Mazey

    Mazey Haxor Global Moderator Forum Moderator Staff Alumni Donator Official Server Admin

    Messages:
    1,914
    getNet().isServer() returns true if the code is executed on server. getNet().isClient() if the code is executed on client

    Example:
    Code:
    void onCommand(CBlob@ this, u8 cmd, CBitStream @params)
    {
        if (cmd == this.getCommandID("open") && getNet().isClient())
        {
            openDoor();
        }
    }
    When we receive the command open, we execute the function openDoor on client, so for people who sent the command the door will be open, and for people who didn't the door will be closed.

    I'm not sure if this works, but you could do something like this maybe

    Code:
    void Reset(CRules@ this)
    {
        if (getNet().isServer())
        {
            getRules().set_s32("biome", XORRandom(12));
        }
    
        for (int i = 0; i < getPlayersCount(); i++)
        {
            CPlayer@ player = getPlayer(i);
    
            player.set_s32("biome", getRules().get_s32("biome"));
        }
    }
    
    void onNewPlayerJoin(CRules@ this, CPlayer@ player)
    {
        player.set_s32("biome", getRules().get_s32("biome"));
    }
     
  3. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    Could it be that you need to synchronize with the clients?

    Code:
    void Reset(CRules@ this)
    {
        barrier_set = false;
    
        if(getNet().isServer()) {
           this.set_s32("biome", XORRandom(12)); //s32 might be overkill, but anyways
           this.Sync("biome", true);
        }
    
    }
    
     
    makmoud98 likes this.
  4. Mazey

    Mazey Haxor Global Moderator Forum Moderator Staff Alumni Donator Official Server Admin

    Messages:
    1,914
    Possibly, that might work. Try it out

    @Verrazano might be bigger help in this case, I mainly wanted to explain the getNet().isServer() and getNet().isClient().
     
  5. Tsilliev

    Tsilliev Haxor

    Messages:
    414
    oh i havent tried the sync before maybe it is my solution for both cases let me try it, thanks for the quick response guys.
    Before i try it let me mention that i tried wrapping the if getnet isserver around like you did and it didnt work, but now i will try it with the sync.
    if s32 is an overkill what should i use :D i see all other methods with set,get use the s32 so i use it as well.
    And how would i sync the workshops?
    Code:
    if (cmd == this.getCommandID("shop made item"))
        {
            CBlob@ caller = getBlobByNetworkID( params.read_netid() );
            CBlob@ item = getBlobByNetworkID( params.read_netid() );
            if (item !is null && caller !is null)
            {              
                this.getSprite().PlaySound("/select.ogg" );
                this.getSprite().getVars().gibbed = true;
              
          
                // open factory upgrade menu immediately
                if (item.getName() == "military2")
                {
                    CBitStream factoryParams;
                    factoryParams.write_netid( caller.getNetworkID() );
                  
                    item.SendCommand( item.getCommandID("shop menu"), factoryParams );
                }
                    if (item.getName() == "military")
                {
                    CBitStream factoryParams;
                    factoryParams.write_netid( caller.getNetworkID() );
                    item.SendCommand( item.getCommandID("shop menu"), factoryParams );
                }
                    if (item.getName() == "economy")
                {
                    CBitStream factoryParams;
                    factoryParams.write_netid( caller.getNetworkID() );
                    item.SendCommand( item.getCommandID("shop menu"), factoryParams );
                }
            }
          
            this.server_Die();
        }
    this.sync(???) maybe item?
    --- Double Post Merged, Mar 16, 2016, Original Post Date: Mar 16, 2016 ---
    edit: so far the number is being generated and not ignored like last time, seems the sync works, now what I have to do is wait for some players to join so i ask them if we have the same biome, lol it was kinda weird everyone being in his own world.
     
  6. makmoud98

    makmoud98 You are already DEAD Forum Moderator Staff Alumni Tester

    Messages:
    586
    ill join what server?
     
  7. Vermilicious

    Vermilicious Ballista Bolt Thrower

    Messages:
    232
    It's probably not very significant, but s32 is a 32-bit signed integer. It can hold values of some - 2 million to + 2 million (2^32 = 4m). You have a maxiumum of 12 biomes, so even as little as 4 bits (unsigned) would be sufficient (2^4 = 16). There's no 4-bit type available though. Types available:

    Signed integers (positive and negative whole numbers): s8, s16, s32
    Unsigned integers (positive whole numbers): u8, u16, u32
    Floating point numbers (positive and negative decimal numbers): f32

    In some cases, the size might matter. Typically in performance critical situations. Network communication might be one such situation. Again, probably not very significant, but I think it's best to at least have some idea of what it means.

    Ideally, one should pick a type which is enough to hold what you need, but preferably also with a little extra room to avoid problems later on. If you picked 4 bits, and forgot about it, and later you add new biomes, exceeding 16 variants, you could end up with a bug. At some point you would perhaps also want a way to disable the functionality altogether, and a common way to do that is to use a negative number. If you picked an unsigned type, you'll end up with problems.
    s32 is the "roomiest" whole number type available, so you are definitely safe.
     
    Last edited: Mar 17, 2016
  8. Tsilliev

    Tsilliev Haxor

    Messages:
    414
    Capture The Princess v0.94 (CTF Modded/Expanded) by Tsilliev/Miros, usually its the first one, full at peak times which are around 18:00+ (UTC +1)

    Thank you for the explanation.