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

GetControls() doesn't work (update issue?), localhost problems...

Discussion in 'Modding Help' started by LightTab2, Feb 18, 2015.

  1. LightTab2

    LightTab2 Drill Rusher
    1. Zen Laboratories

    Messages:
    83
    I don't know when it happend, but I realized that getControls() isn't working properly. I guess update has broken it. I mean isKeyPressed() works, but isKeyJustPressed() doesn't ;/.


    Code:
    int pages = 1;
    
    void onRender( CRules@ this )
    {
        CPlayer@ p = getLocalPlayer();
        if (p is null || !p.isMyPlayer()) { return; }
        CControls@ controls = getControls();
        if(controls.isKeyJustPressed(KEY_F1)) pages--;
        else if (controls.isKeyJustPressed(KEY_F2)) pages++;
    }
    Anyways, there's a bigger trouble. I got unusual problem - if a player had joined before another, who didn't die while the first one was on server, cannot kill him. I was using only one file, added in attachments.
    blackjoker77777 told me that it's localhost issue, is it?


    I hope you understand it, sorry for my English; thanks in advance.
     

    Attached Files:

  2. blackjoker77777

    blackjoker77777 Haxor Tester
    1. Zen Laboratories

    Messages:
    441
    @LightTab2 are you sure you uploaded the right file? (O_o) I mean seriously there is no isKeyJustPressed in the file you uploaded :huh?: here is what inside Roleplay_Rules.as you uploaded
    //CTF gamemode logic script

    #define SERVER_ONLY

    #include "CTF_Structs.as";
    #include "RulesCore.as";
    #include "RespawnSystem.as";

    //edit the variables in the config file below to change the basics
    // no scripting required!
    void Config(RoleplayCore@ this)
    {
    //how long for the game to play out?
    this.gameDuration = 0;
    getRules().set_bool("no timer", true);
    //whether to scramble each game or not

    //spawn after death time
    this.spawnTime = getTicksASecond() * 15;

    }

    //CTF spawn system

    const s32 spawnspam_limit_time = 10;

    shared class RoleplaySpawns : RespawnSystem
    {
    RoleplayCore@ Roleplay_core;

    bool force;
    s32 limit;

    void SetCore(RulesCore@ _core)
    {
    RespawnSystem::SetCore(_core);
    @Roleplay_core = cast<RoleplayCore@>(core);

    limit = spawnspam_limit_time;
    }

    void Update()
    {
    for (uint team_num = 0; team_num < Roleplay_core.teams.length; ++team_num )
    {
    CTFTeamInfo@ team = cast<CTFTeamInfo@>( Roleplay_core.teams[team_num] );

    for (uint i = 0; i < team.spawns.length; i++)
    {
    CTFPlayerInfo@ info = cast<CTFPlayerInfo@>(team.spawns);

    UpdateSpawnTime(info, i);

    DoSpawnPlayer( info );
    }
    }
    }

    void UpdateSpawnTime(CTFPlayerInfo@ info, int i)
    {
    if ( info !is null)
    {
    u8 spawn_property = 255;

    if(info.can_spawn_time > 0) {
    info.can_spawn_time--;
    spawn_property = u8(Maths::Min(250,(info.can_spawn_time / 30)));
    }

    string propname = "ctf spawn time "+info.username;

    Roleplay_core.rules.set_u8( propname, spawn_property );
    Roleplay_core.rules.SyncToPlayer( propname, getPlayerByUsername(info.username) );
    }

    }

    void DoSpawnPlayer( PlayerInfo@ p_info )
    {
    if (canSpawnPlayer(p_info))
    {
    //limit how many spawn per second
    if(limit > 0)
    {
    limit--;
    return;
    }
    else
    {
    limit = spawnspam_limit_time;
    }

    CPlayer@ player = getPlayerByUsername(p_info.username); // is still connected?

    if (player is null)
    {
    RemovePlayerFromSpawn(p_info);
    return;
    }

    // remove previous players blob
    if (player.getBlob() !is null)
    {
    CBlob @blob = player.getBlob();
    blob.server_SetPlayer( null );
    blob.server_Die();
    }
    p_info.blob_name = "builder";
    CBlob@ playerBlob = SpawnPlayerIntoWorld( getSpawnLocation(p_info), p_info);

    if (playerBlob !is null)
    {
    // spawn resources
    p_info.spawnsCount++;
    RemovePlayerFromSpawn(player);
    }
    }
    }

    bool canSpawnPlayer(PlayerInfo@ p_info)
    {
    CTFPlayerInfo@ info = cast<CTFPlayerInfo@>(p_info);

    if (info is null) { warn("CTF LOGIC: Couldn't get player info ( in bool canSpawnPlayer(PlayerInfo@ p_info) ) "); return false; }

    if (force) { return true; }

    return info.can_spawn_time <= 0;
    }

    Vec2f getSpawnLocation(PlayerInfo@ p_info)
    {
    CTFPlayerInfo@ c_info = cast<CTFPlayerInfo@>(p_info);
    if(c_info !is null)
    {
    CMap@ map = getMap();
    if(map !is null)
    {
    f32 x = XORRandom(map.tilemapwidth) * map.tilesize - 32.0f;
    return Vec2f(x, map.getLandYAtX(s32(x/map.tilesize))*map.tilesize - 16.0f);
    }
    }

    return Vec2f(0,0);
    }

    void RemovePlayerFromSpawn(CPlayer@ player)
    {
    RemovePlayerFromSpawn(core.getInfoFromPlayer(player));
    }

    void RemovePlayerFromSpawn(PlayerInfo@ p_info)
    {
    CTFPlayerInfo@ info = cast<CTFPlayerInfo@>(p_info);

    if (info is null) { warn("CTF LOGIC: Couldn't get player info ( in void RemovePlayerFromSpawn(PlayerInfo@ p_info) )"); return; }

    string propname = "ctf spawn time "+info.username;

    for (uint i = 0; i < Roleplay_core.teams.length; i++)
    {
    CTFTeamInfo@ team = cast<CTFTeamInfo@>(Roleplay_core.teams);
    int pos = team.spawns.find(info);

    if (pos != -1) {
    team.spawns.erase(pos);
    break;
    }
    }

    Roleplay_core.rules.set_u8( propname, 255 ); //not respawning
    Roleplay_core.rules.SyncToPlayer( propname, getPlayerByUsername(info.username) );

    //DONT set this zero - we can re-use it if we didn't actually spawn
    //info.can_spawn_time = 0;
    }

    void AddPlayerToSpawn( CPlayer@ player )
    {
    s32 tickspawndelay = s32(Roleplay_core.spawnTime);

    CTFPlayerInfo@ info = cast<CTFPlayerInfo@>(core.getInfoFromPlayer(player));

    if (info is null) { warn("CTF LOGIC: Couldn't get player info ( in void AddPlayerToSpawn(CPlayer@ player) )"); return; }

    //clamp it so old bad values don't get propagated
    s32 old_spawn_time = Maths::Max(0, Maths::Min(info.can_spawn_time, tickspawndelay));

    RemovePlayerFromSpawn(player);

    CTFTeamInfo@ team = cast<CTFTeamInfo@>(Roleplay_core.teams[0]);
    info.can_spawn_time = ((old_spawn_time > 30) ? old_spawn_time : tickspawndelay);

    info.spawn_point = player.getSpawnPoint();
    team.spawns.push_back(info);
    }

    bool isSpawning( CPlayer@ player )
    {
    CTFPlayerInfo@ info = cast<CTFPlayerInfo@>(core.getInfoFromPlayer(player));
    for (uint i = 0; i < Roleplay_core.teams.length; i++)
    {
    CTFTeamInfo@ team = cast<CTFTeamInfo@>(Roleplay_core.teams);
    int pos = team.spawns.find(info);

    if (pos != -1) {
    return true;
    }
    }
    return false;
    }

    };

    shared class RoleplayCore : RulesCore
    {
    s32 gameDuration;
    s32 spawnTime;

    RoleplaySpawns@ roleplay_spawns;

    RoleplayCore() {}

    RoleplayCore(CRules@ _rules, RespawnSystem@ _respawns )
    {
    super(_rules, _respawns );
    }


    int gamestart;
    void Setup(CRules@ _rules = null, RespawnSystem@ _respawns = null)
    {
    RulesCore::Setup(_rules, _respawns);
    gamestart = getGameTime();
    @roleplay_spawns = cast<RoleplaySpawns@>(_respawns);
    server_CreateBlob( "Entities/Meta/WARMusic.cfg" );
    rules.SetCurrentState(GAME);
    }

    void Update()
    {
    RulesCore::Update(); //update respawns
    }

    //team stuff

    void AddTeam(CTeam@ team)
    {
    CTFTeamInfo t(teams.length, team.getName());
    teams.push_back(t);
    }

    void AddPlayer(CPlayer@ player, u8 team = 0, string default_config = "")
    {
    team = player.getTeamNum();
    CTFPlayerInfo p(player.getUsername(), team, "builder" );
    players.push_back(p);
    ChangeTeamPlayerCount(p.team, 1);
    }

    void onPlayerDie(CPlayer@ victim, CPlayer@ killer, u8 customData)
    {
    if (!rules.isMatchRunning()) { return; }

    if (victim !is null )
    {
    if (killer !is null && killer.getTeamNum() != victim.getTeamNum())
    {
    addKill(killer.getTeamNum());
    }
    }
    }

    void onSetPlayer( CBlob@ blob, CPlayer@ player )
    {
    if (blob !is null && player !is null) {
    //GiveSpawnResources( blob, player );
    }
    }

    void addKill(int team)
    {
    if (team >= 0 && team < int(teams.length))
    {
    CTFTeamInfo@ team_info = cast<CTFTeamInfo@>( teams[team] );
    }
    }

    };

    //pass stuff to the core from each of the hooks

    void Reset( CRules@ this )
    {
    printf("Restarting rules script: " + getCurrentScriptName() );
    RoleplaySpawns spawns();
    RoleplayCore core(this, spawns);
    Config(core);
    this.set("core", @core);
    this.set("start_gametime", getGameTime());
    this.set_u32("game_end_time", getGameTime() + 612); //for TimeToEnd.as
    }

    void onRestart( CRules@ this )
    {
    Reset( this );
    }

    void onInit( CRules@ this )
    {
    Reset( this );
    }
     
  3. LightTab2

    LightTab2 Drill Rusher
    1. Zen Laboratories

    Messages:
    83
    Sorry, isKeyJustPressed is called in another file.

    EDIT: Previous file shows immortality bug.
     

    Attached Files:

    Last edited: Feb 21, 2015
    blackjoker77777 likes this.
  4. blackjoker77777

    blackjoker77777 Haxor Tester
    1. Zen Laboratories

    Messages:
    441
    well...@LightTab here is what I was able to do since 'isKeyJustPressed()' doesn't work. I know that what I edited isn't a big deal but at least it will fix the issue (temporary at least ). what I had done is to make F1 go to Page N1 , F2 go to Page N2 and F3 to hide both pages, I wish you will have a look on it and see if it helps at the mean time until we figure out a real fix for isKeyJustPressed()
    here is the modified Roleplay_Interface.as anyway.
    Hope I helped, Have a nice day. ::):
     

    Attached Files:

  5. LightTab2

    LightTab2 Drill Rusher
    1. Zen Laboratories

    Messages:
    83
    Thank you. Sorry that I haven't responded before, but as you probably know, I had computer in repair.
     
  6. blackjoker77777

    blackjoker77777 Haxor Tester
    1. Zen Laboratories

    Messages:
    441
    yeah I know about that , I wish your computer is fully working now ::):
     
  7. LightTab2

    LightTab2 Drill Rusher
    1. Zen Laboratories

    Messages:
    83
    I hope that KAGTeam will fix it in future.
    Code:
        if(controls.isKeyPressed(KEY_F1)) pages = 1;
        else if (controls.isKeyPressed(KEY_F2)) pages = 2;
        else if (controls.isKeyPressed(KEY_F3)) pages = 0;
        else if (controls.isKeyPressed(KEY_KEY_P))
    This code eats a lot more of memory. I'd be glad if someone tell me if it's fixed. Thank you again, blackjoker7777
     
  8. blackjoker77777

    blackjoker77777 Haxor Tester
    1. Zen Laboratories

    Messages:
    441
    np @LightTab2 Glad I helped you ::):
     
  9. LightTab2

    LightTab2 Drill Rusher
    1. Zen Laboratories

    Messages:
    83
    I know it looks horrible, but onRender is equal to onTick (local ofc), so it should work. And it worked before, so I didn't need to get localplayer anywhere else and sent the data to the rendering script. You have right, it works if we'd put code in another file. I'm about to use that knowledge, when I'll get my stuff back on pc. I wonder if the code, which I'm about the do, won't eat more memory than joker's suggested one.

    For me error lies in desynchornized onRender with onTick. They shouldn't be like that, so is it a bug? I don't know, but it repairing it'd make our work a lost easier. Although it isn't necessary needed.
     
    blackjoker77777 likes this.
  10. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    It's not safe to assume that rendering happens every tick, or a tick happens for each render. Some people play with uncapped frames, so they will get tens of frames for each tick. some people can hardly run the game and it skips frames for them.

    I'm not sure what you're talking about with "eats memory" - you're talking in the order of bytes at that point.
    If you want a "just pressed" thing to work, do the update stuff.. in an update hook. Do not change state in a render hook - it should ONLY be used for rendering.
     
    LightTab2, Fuzzle and blackjoker77777 like this.
  11. LightTab2

    LightTab2 Drill Rusher
    1. Zen Laboratories

    Messages:
    83
    Thank you Geti. I was thinking like you at the beginning, but it was a surprise when I introduced my code into onRender and it was working. I don't know why, and I don't realy want (my computer skips frames on kag, so it shouldn't work in any way).

    By "eats memory" I meant that every if() happen(maybe it isn't the best word) on EVERY tick, what may oveload CPU. but not memory - my mistake. I know that few more if() wouldn't make a big performance change. I'm perfectionis, so if I do anything I try do it as lighweight as it's possible. This "curse" makes me spending obviously more time on perharps simple things.

    The second problem is harder, because after many tests I couldn't find answer (ye, you can blame me why I post GetControls() problem without initial testing, but I hadn't got my computer with KAG and I knew the solution before, so it was only for curiosity)
    Anyways, there's a bigger trouble. I got unusual problem - if a player had joined before another, who didn't die while the first one was on server, cannot kill him. I was using only one file, added in attachments.
    blackjoker77777 told me that it's localhost issue, is it?
    I tried almost everything to get it working. Everything what I get after many tests is knowledge that the bug lies in file, which I added in attachments (1st post). I removed the non-important code and left only (as I think) bugged one. It was month ago, so I don't know properly more details about it. I'll be glad if someone'd take look on it.

    Thank you all and sorry for my English.
     
  12. Fernegulus

    Fernegulus Bison Rider

    Messages:
    400
    Well I once had the same problem with iskeyjustpressed and I was calling it ontick.
    didnt work either
     
  13. Geti

    Geti Please avoid PMing me (poke a mod instead) THD Team Administrator Global Moderator

    Messages:
    3,730
    Re: isKeyJustPressed - yup it's been broken for a while. isKeyPressed works fine.
     
  14. Fernegulus

    Fernegulus Bison Rider

    Messages:
    400
    First time I met this problem I was doing a mod for the Sandbox Survival project.
    I'm pretty sure it was way over a year ago.
    Surely way over a while.
     
  15. it might have been fixed a few builds ago
     
  16. LightTab2

    LightTab2 Drill Rusher
    1. Zen Laboratories

    Messages:
    83
    Thank you all for everything.

    I see second problem cannot be resolved, so (I guess) thread may be closed. Again thanks.

    EDIT: Figured out what was the problem, thank you anyways.
     
    Last edited: Apr 15, 2015